Elgg extend view

Posted on Dec 21, 2011 in News, Tips & Tricks, Tutorials | 5 comments

Elgg extend view

In the previous tutorial, we used myplugin to override views. Let us now look at how to extend a view through myplugin.

In some situations, you do not want to override the entire view, you just want to add more content to it. In Elgg this is called extending a view. Let’s say you want to add some sort of content to the sidebar, an informational text for example. This can be done by calling elgg_extend_view() in start.php.

Open engine/lib/views.php and find the function elgg_extend_view,

elgg_extend_view($view, $view_extension, $priority = 501, $viewtype = '')

As you’ll notice the function takes three parameters ($viewtype, not used), the view to extend, the view you add and the priority. The default priority is 501, and priorities less than 500 are displayed before the primary view and greater than 500 after. Now call elgg_extend_view in myplugin/start.php by adding this line,

elgg_extend_view('page/elements/sidebar', 'page/elements/info', '1');

This will prepare the sidebar to be extended with the view we want to create right now, info. Priority 1 will put it right at the top of the sidebar.

Create a new php file in the directory page/elements/ and name it info.php. In this file, you can add almost any content and the content will be displayed in the sidebar. Let us add a module,

$title = elgg_echo('myplugin:title');
$content = elgg_echo('myplugin:text');
echo elgg_view_module('featured', $title, $content);

Create a new directory in myplugin/ called language, and add a new php file called en.php. Open en.php and copy/paste the snippet below,

$english = array(
	'myplugin:title' 	=> 'Information',
	'myplugin:text'		=> 'This is some information from your admin!',
);
add_translation("en", $english);

Save your files and refresh your page and you will see a new module added at the top of the sidebar.

5 Comments

  1. 2-17-2012

    Thanks! I hope there would be tutorials in the future to make the mobile views!

  2. 7-17-2013

    hello i have a small query about this information box.
    how can we change the background of the blue txt box?

    • 7-17-2013

      It’s a default elgg module. You can change color in,

      ../css/elements/modules.php
    • 7-23-2013

      hello mr per jensen , thanks you for the hint but can you help me a bit more by specifying to me the line that i have to change in order to change the background inside the information box. thnx in advance i am a novice in elgg.

    • 7-23-2013

      Go to views/default/ and create a new folder in this directory, name it css. Inside this folder add another folder and name it elements.

      Go to the similar folder in elgg core and copy modules.php and paste it into your own elements folder.

      In modules.php find,

      .elgg-module-featured > .elgg-body {
      	padding: 10px;
      }

      Below add,

      .elgg-sidebar .elgg-module-featured > .elgg-body {
      	background-color: #FFF;
      }

Leave a Comment

Your email address will not be published. Required fields are marked *

Anti-Spam Quiz: