This tutorial will teach you how to create a simple blog plugin.
The basic functions of the blog will be creating posts,
saving them and viewing them.
The plugin duplicates features that are found in the
bundled blog plugin.
You can disable the bundled blog plugin if you wish,
but it is not necessary since the features do not conflict
each other.

First, choose a simple and descriptive name for your plugin.
In this tutorial, the name will be my_blog.
Then, create a directory for your plugin in the /mod/ directory
found in your Elgg installation directory. Other plugins are also located
in /mod/. In this case, the name of the directory should
be /mod/my_blog/. This directory is the root of your plugin and all the
files that you create for the new plugin will go somewhere under it.

Next, in the root of the plugin, create the plugin’s manifest file,
manifest.xml:

Create a file at /mod/my_blog/views/default/forms/my_blog/save.php
that contains the form body. The form should have input fields for the title,
body and tags of the my_blog post. It does not need form tag markup.

Notice how the form is calling elgg_view_field() to render inputs. This helper
function maintains consistency in field markup, and is used as a shortcut for
rendering field elements, such as label, help text, and input. See Forms + Actions.

You can see a complete list of input views in the
/vendor/elgg/elgg/views/default/input/ directory.

It is recommended that you make your plugin translatable by using elgg_echo()
whenever there is a string of text that will be shown to the user. Read more at
Internationalization.

Create the file /mod/my_blog/views/default/resources/my_blog/add.php.
This page will view the form you created in the above section.

<?php// make sure only logged in users can see this pagegatekeeper();// set the title$title="Create a new my_blog post";// start building the main column of the page$content=elgg_view_title($title);// add the form to the main column$content.=elgg_view_form("my_blog/save");// optionally, add the content for the sidebar$sidebar="";// layout the page$body=elgg_view_layout('one_sidebar',array('content'=>$content,'sidebar'=>$sidebar));// draw the page, including the HTML wrapper and basic page layoutechoelgg_view_page($title,$body);

The function elgg_view_form("my_blog/save") views the form that
you created in the previous section. It also automatically wraps
the form with a <form> tag and the necessary attributes as well
as anti-csrf tokens.

The form’s action will be "<?=elgg_get_site_url()?>action/my_blog/save".

The action file will save the my_blog post to the database.
Create the file /mod/my_blog/actions/my_blog/save.php:

<?php// get the form inputs$title=get_input('title');$body=get_input('body');$tags=string_to_tag_array(get_input('tags'));// create a new my_blog object and put the content in it$blog=newElggObject();$blog->title=$title;$blog->description=$body;$blog->tags=$tags;// the object can and should have a subtype$blog->subtype='my_blog';// for now, make all my_blog posts public$blog->access_id=ACCESS_PUBLIC;// owner is logged in user$blog->owner_guid=elgg_get_logged_in_user_guid();// save to database and get id of the new my_blog$blog_guid=$blog->save();// if the my_blog was saved, we want to display the new post// otherwise, we want to register an error and forward back to the formif($blog_guid){system_message("Your blog post was saved.");forward($blog->getURL());}else{register_error("The blog post could not be saved.");forward(REFERER);// REFERER is a global variable that defines the previous page}

As you can see in the above code, Elgg objects have several fields built
into them. The title of the my_blog post is stored
in the title field while the body is stored in the
description field. There is also a field for tags which are stored as
metadata.

Objects in Elgg are a subclass of something called an “entity”.
Users, sites, and groups are also subclasses of entity.
An entity’s subtype allows granular control for listing and displaying,
which is why every entity should have a subtype.
In this tutorial, the subtype “my_blog” identifies a my_blog post,
but any alphanumeric string can be a valid subtype.
When picking subtypes, be sure to pick ones that make sense for your plugin.

The getURL method fetches the URL of the new post. It is recommended
that you override this method. The overriding will be done in the
start.php file.

Registering the save action will make it available as /action/my_blog/save.
By default, all actions are available only to logged in users.
If you want to make an action available to only admins or open it up to unauthenticated users,
you can pass ‘admin’ or ‘public’ as the third parameter of elgg_register_action.

The URL overriding function will extract the ID of the given entity and use it to make
a simple URL for the page that is supposed to view the entity. In this case
the entity should of course be a my_blog post. Add this function to your
start.php file:

The page handler makes it possible to serve the page that generates the form
and the page that views the post. The next section will show how to create
the page that views the post. Add this function to your start.php file:

The $segments variable contains the different parts of the URL as separated by /.

Page handling functions need to return true or false. true
means the page exists and has been handled by the page handler.
false means that the page does not exist and the user will be
forwarded to the site’s 404 page (requested page does not exist or not found).
In this particular example, the URL must contain either /my_blog/add or
/my_blog/view/id where id is a valid ID of an entity with the my_blog subtype.
More information about page handling is at
Page handler.

To be able to view a my_blog post on its own page, you need to make a view page.
Create the file /mod/my_blog/views/default/resources/my_blog/view.php:

<?php// get the entity$guid=elgg_extract('guid',$vars);$my_blog=get_entity($guid);// get the content of the post$content=elgg_view_entity($my_blog,array('full_view'=>true));$params=array('title'=>$my_blog->title,'content'=>$content,'filter'=>'',);$body=elgg_view_layout('content',$params);echoelgg_view_page($my_blog->title,$body);

This page has much in common with the add.php page. The biggest differences
are that some information is extracted from the my_blog entity, and instead of
viewing a form, the function elgg_view_entity is called. This function
gives the information of the entity to something called the object view.

Let’s also create a page that lists my_blog entries that have been created.

Create /mod/my_blog/views/default/resources/my_blog/all.php:

<?php$titlebar="All Site My_Blogs";$pagetitle="List of all my_blogs";$body=elgg_list_entities(array('type'=>'object','subtype'=>'my_blog',));$body=elgg_view_title($pagetitle).elgg_view_layout('one_column',array('content'=>$body));echoelgg_view_page($titlebar,$body);

The elgg_list_entities function grabs the latest my_blog posts and
passes them to the object view file.
Note that this function returns only the posts that the user can see,
so access restrictions are handled transparently.
The function (and its cousins) also
transparently handles pagination and even creates an RSS feed for your
my_blogs if you have defined that view.

The list function can also limit the my_blog posts to those of a specified user.
For example, the function elgg_get_logged_in_user_guid grabs the Global Unique
IDentifier (GUID) of the logged in user, and by giving that to
elgg_list_entities, the list only displays the posts of the current user:

Now, if the URL contains /my_blog/all, the user will see an
“All Site My_Blogs” page. Because of the default case, the list of all my_blogs
will also be shown if the URL is something invalid,
like /my_blog or /my_blog/xyz.

You might also want to update the object view to handle different kinds of viewing,
because otherwise the list of all my_blogs will also show the full content of all my_blogs.
Change /mod/my_blog/views/default/object/my_blog.php to look like this:

<?php$full=elgg_extract('full_view',$vars,FALSE);// full viewif($full){echoelgg_view('output/longtext',array('value'=>$vars['entity']->description));echoelgg_view('output/tags',array('tags'=>$vars['entity']->tags));// list view or short view}else{// make a link out of the post's titleechoelgg_view_title(elgg_view('output/url',array('href'=>$vars['entity']->getURL(),'text'=>$vars['entity']->title,'is_trusted'=>true)));echoelgg_view('output/tags',array('tags'=>$vars['entity']->tags));}

Now, if full_view is true (as it was pre-emptively set to be in
this section), the object view will show
the post’s content and tags (the title is shown by view.php).
Otherwise the object view will render just the title and
tags of the post.