This tutorial will show you how to build a simple blog in Elgg.
Contents |
Elgg has a /mod directory. Create a subdirectory there called "blog". In turn, create a few more subdirectories so we have:
/mod/blog/ /mod/blog/actions/ /mod/blog/views/default/
Objects in Elgg are a subclass of something called an "entity". (Users and sites are also subclasses of entity.) All entities can have a subtype, the meat of which is utterly up to you. Valid object subtypes include blog posts, calendar entries, loaves of bread, chocolate teapots and inflatable dartboards. Here, let's just opt for the word "blog" to identify a blog post.
In /mod/blog/views/default/, create object/blog.php.
Each blog post will be passed to this PHP file as $vars['entity']. ($vars is an array used in the views system to represent system variables.) Given this, the content of object/blog.php can just be something like:
<h1><?php echo $vars['entity']->title; ?></h1> <p><?php echo $vars['entity']->body; ?></p> <?php echo elgg_view('output/tags', array('tags' => $vars['entity']->tags)); ?>
The last line takes the tags on the blog post and automatically displays them as a series of clickable links. Search is handled completely automatically.
Incidentally, if you're wondering about the 'default' in /views/default/, you can create alternative views. RSS, OpenDD, FOAF, mobile and others are all valid view types.
Create a file called /mod/blog/views/default/blog/form.php that contains a form. The form should have input fields for the title, body and tags, and direct to /action/blog/save:
<form action="<?php echo $vars['url']; ?>action/blog/save" method="post"> <p><?php echo elgg_echo("title"); ?><br /> <?php echo elgg_view('input/text',array('internalname' => 'title')); ?></p> <p><?php echo elgg_echo("body"); ?><br /> <?php echo elgg_view('input/longtext',array('internalname' => 'body')); ?></p> <p><?php echo elgg_echo("tags"); ?><br /> <?php echo elgg_view('input/tags',array('internalname' => 'tags')); ?></p> <p><input type="submit" value="<?php echo elgg_echo('save'); ?>" /></p> </form>
Create a file at /mod/blog/actions/save.php. This will save the blog post.
First of all, we'll punt the user to another page if we're not logged in:
gatekeeper();
Then let's get the form input:
$title = get_input('title'); $body = get_input('body'); $tags = string_to_tag_array(get_input('tags'));
Save it as a new blog object entity:
$blogpost = new ElggObject(); $blogpost->title = $title; $blogpost->description = $description;
A few fields are built into Elgg objects. Title and description are two of these; as a result it makes sense to use description to contain the blog text.
$blogpost->access_id = ACCESS_PUBLIC;
We won't deal with access restrictions just now, but they're there. Let's just make our blog post public.
$blogpost->owner_guid = $_SESSION['user']->getGUID();
$_SESSION['user'] contains the user entity for the currently logged in user.
$blogpost->save(); $blogpost->tags = $tags;
We currently need to save the post before we can begin adding custom metadata, although this demand may be removed before release.
And that's it! Finally, we'll forward to the blog post page:
forward($blogpost->getURL());
Every object in Elgg has a built-in URL automatically, although you can override this if you wish.
To delete a blog post, we would need to create a corresponding delete action that takes in the GUID of the blog post. This would look something like:
gatekeeper(); $guid = get_input('blogpost_guid'); if ($blogpost = get_entity($guid) && $blogpost->delete()) { system_message("Your blog post was deleted."); }
Our blog will be accessible through http://yoursite/mod/blog/index.php. If you prefer pretty URLs like http://yoursite/pg/blog/user_name, view the tutorial on creating friendly URLs using the page handler.
Create /mod/blog/index.php.
First of all, we need to reference the Elgg engine:
require_once(dirname(dirname(dirname(__FILE__))) . "engine/start.php");
Then we'll grab the latest blog posts. Note that this function returns only the posts that the user can see, so access restrictions are handled transparently:
$body = list_entities('object','blog',0,10,false);
The <code>list_entities function (and its cousins) also transparently handles pagination, and even creates an RSS and OpenDD feed for your blog.
Finally, we'll draw the page:
$body = elgg_view_layout('one_column', $body); page_draw("Our blog page",$body);
If we grab the Global Unique IDentifier (GUID) of the user to view the blog for, eg by taking it from the currently logged in user, we can limit the blog posts to those posted by a particular user by replacing the list function above with:
list_user_objects($user_guid,'blog',10,false);
Or by their friends by replacing it with:
list_user_friends_objects($user_guid,'blog',10,false);
Every module has a start.php that defines it. For this example, we just need to register the action file we created earlier:
global $CONFIG; register_action("blog/save", false, $CONFIG->pluginspath . "blog/actions/save.php");
The action will now be available as /action/blog/save, although the false in the second parameter means that it will only be available to logged in users. The third parameter just defines where the source file is.
Finally, it's a good idea to add a manifest file, which stores basic information about the plugin. See this tutorial for the template.
Done!