Book Image

Elgg 1.8 Social Networking

By : Cash Costello
Book Image

Elgg 1.8 Social Networking

By: Cash Costello

Overview of this book

As an updated version of the first ever Elgg book, this is an excellent resource for those interested in Elgg development due to its attention to detail, clearly written style and knowledgeable author. - Dave Tosh, Elgg co-founder. In his book, Cash Costello makes full use of his skills in development and communication to tackle the complex subject of Elgg social networking. This easy-to-read guide gives end users, new developers, and old pros alike a solid base to start their venture into social media with Elgg. I highly recommend it as a useful and enjoyable read. - Brett Profitt, Elgg Lead Developer The web is becoming increasingly social as millions of people use it to blog, share, post, 'friend', 'unfriend' (which was made the Oxford word of the year in 2009), comment, and chat. Elgg ñ your award-winning open source social networking engine ñ is tailor-made to create any social networking or social media website you can imagine. If you want to create a social networking website from scratch using Elgg, then this book is exactly what you need.Elgg 1.8 Social Networking covers everything you need to know about building a social networking site with Elgg. It begins with instructions for installing Elgg, continues with a guided tour of its capabilities, and finishes with practical advice on deploying Elgg on a production server. And in between, it is packed with information on configuring and customizing Elgg through plugins and themes.This book is a learn-by-doing guide to creating your own social networking site. It includes three sample case studies on how Elgg could be used as an e-learning tool, an intranet application for organizations, and a niche social networking site. Step by step it takes you through the installation, configuration, and customization of Elgg. Valuable advice is sprinkled throughout the book to enable you to build your site like an expert. For developers, this book provides a multitude of options. First, there is a tutorial-based section that systematically teaches you how to build plugins. Soon you will have ten plugins for use on your site in addition to all the knowledge you have gained. Second, if you prefer a quick overview, this book has an appendix that describes Elgg using the terminology and design patterns common in web development. Third, if you are interested in creating a theme, it also includes a design tutorial and a catalog of Elgg's view templates. The book then goes on to describe what is involved in running a production website. It has sections on selecting a hosting provider, configuring and tuning the server, backing up the site, and dealing with spammers.
Table of Contents (21 chapters)
Elgg 1.8 Social Networking
Credits
Foreword
About the Author
About the Author of 1st edition
About the Reviewers
www.PacktPub.com
Preface
Index

Views


Elgg's view system creates the response sent to the requester. It could be an HTML page, RSS feed, XML document or any other format. The generation of the response is divided into two parts: rendering the data related to the request and including the rendered data into the full response. Elgg uses a template system to create the response.

View templates

Templates allow dynamic content to be included in a static structure. Consider the following simple example of an Elgg view:

<h2><?php echo $vars['title']; ?></h2>

This view creates the HTML for displaying a title. The h2 tag is static while the value between the tags is dynamic.

The $vars[] associative array contains the variables passed into the view. The function elgg_view() is used to generate the output of a view (a string):

echo elgg_view('page/elements/title', array('title' => 'Hi'));

The name of a view is determined by its path. A core view with the name 'input/button' is located at /views/default/input/button.php.

Views can be nested. The 'page/elements/header' view includes other views:

<?php
/**
 * Elgg page header
 * The header lives between the topbar and main content area.
 */

// link back to main site.
echo elgg_view('page/elements/header_logo', $vars);

// drop-down login
echo elgg_view('core/account/login_dropdown');

// insert site-wide navigation
echo elgg_view_menu('site');

Views are not limited to the variables in the $vars[] array. They can pull information from the data model or helper functions.

Page shells and layout

Rendering the response is divided into two steps. The first is rendering the data specific to a request and the second step is laying out the page. In code, it looks like the following:

// render the latest bookmarks as a list
$options = array(
  'type' => 'object',
  'subtype' => 'bookmarks'
);
$content = elgg_list_entities($options);

// render the response
$title = elgg_echo('bookmarks:latest');
$body = elgg_view_layout('one_sidebar', array('content' => $content));
echo elgg_view_page($title, $body);

The function elgg_view_layout() lays out the central portion of the page identified as 'layout' in the preceding layout diagram. The output of the layout is passed to elgg_view_page() and is inserted into the final response.

Elgg includes generic layouts for one, two, and three columns in addition to a layout specifically for content pages (viewing blogs, bookmarks, files, and similar types of content). The views for these layouts are located in /views/default/page/layouts/. Additional layouts can be added through plugins.

The elgg_view_page() function uses a page shell view to create the complete response. The default page shell for HTML is located at /views/default/page/default.php. This view includes the HTML head, topbar, header, and footer.

View type

The view type parameter determines the response format. A request for http://example.org/blog/ owner/johndoe returns HTML formatted for a web browser. A request for http://example.org/blog/owner/joh ndoe?view=rss returns an RSS feed. The standard HTML view type uses the default view, which is why those views are located in /view/default/. Likewise, the RSS views are in /views/rss/. Each top level directory in /views/ is a different view type.

Overriding and extending views

Overriding a view replaces a core view with a plugin's view. When a view exists in the same relative location in a plugin as it does in the core views, Elgg uses the plugin's view. A plugin that has a file at /mod/<plugin name>/views/default/page/elements/header.php overrides the 'page/elements/header' view of the core. In addition to overriding core views, plugins can override views from other plugins.

The content from one view can be added to that of another view by extending it. The syntax for this is as follows:

elgg_extend_view('page/elements/header', 'mytheme/site_menu');

Special views

There are certain views that Elgg automatically checks for and uses, if they exist. This is one instance of convention over configuration in Elgg. A primary example of this is that each entity has a special view used to display it. The name of the view is based on the entity's type and subtype. The view for displaying a blog post is 'object/blog' and the view for a user is 'user/default'. Rendering an entity is performed by passing it to the elgg_view_entity() function, as follows:

$html = elgg_view_entity($blog);

Other special views include the following:

  • Plugin settings: /mod/<plugin name>/views/default/plugins/<plugin name>/settings.php

  • User settings: /mod/<plugin name>/views/default/plugins/<plugin name>/usersettings.php

  • Widget settings: Widget settings: /mod/<plugin name>/views/default/widgets/<widget name>/edit.php

  • Widget content: /mod/<plugin name>/views/default/widgets/<widget name>/content.php

Code location

Functions: /engine/lib/views.php

Core views: /views/