Building a Blog Plugin ###################### Build a simple blogging plugin using Elgg. This duplicates features in the bundled blog plugin, so be sure to disable that while working on your own version. .. contents:: Contents :local: :depth: 1 Create the plugin skeleton ========================== The name of the directory under "mod" becomes the id of your plugin:: /mod/my_blog/pages/my_blog/ /mod/my_blog/actions/my_blog/ /mod/my_blog/views/default/my_blog/ You'll need to add a manifest file in ``/mod/my_blog/manifest.xml``. This file stores basic information about the plugin. See :doc:`/guides/plugins` for the template. You can also just copy the manifest file from another plugin and then change the values to fit your new plugin. Be sure to change the author and website, and remove the “bundled” category! Create a page for composing the blogs ===================================== Create the file ``add.php`` in ``/mod/my_blog/pages/my_blog/``. .. code:: php $content, 'sidebar' => $sidebar )); // draw the page echo elgg_view_page($title, $body); Create the form for creating a new my\_blog post ================================================ Create a file at ``/mod/my_blog/views/default/forms/my_blog/save.php`` that contains the form body. This corresponds to view that is called above: ``elgg_view_form("my_blog/save")``. The form should have input fields for the title, body and tags. Because you used ``elgg_view_form()``, you do not need to include form tag markup. The view will be automatically wrapped with: * a ``
`` tag and the necessary attributes * anti-csrf tokens The form's action will be ``"action/my_blog/save"``, which we will create in a moment. Here is the content of ``/mod/my_blog/views/default/forms/my_blog/save.php``: .. code:: php

'title')); ?>

'body')); ?>

'tags')); ?>
elgg_echo('save'))); ?>
Notice how the form is calling input views like ``input/longtext``. These are built into Elgg and make it easy to add form components. You can see a complete list of input views in the ``/views/default/input/`` directory. .. warning:: The above code is not accessibility-friendly. The action file =============== Create the file ``/mod/my_blog/actions/my_blog/save.php``. This will save the blog post to the database. .. code:: php subtype = "my_blog"; $blog->title = $title; $blog->description = $body; // 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 tags as metadata $blog->tags = $tags; // 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 form if ($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 } A few fields are built into Elgg objects. Title and description are two of these. It makes sense to use description to contain the my\_blog text. Every entity can have a subtype and in this we are using ``"my_blog"``. The tags are stored as metadata. Every object in Elgg has a built-in URL automatically, although you can override this if you wish. The ``getURL()`` method is called to get that unique URL. The object view =============== Elgg will automatically call the ``object/my_blog`` view to view the my\_blog post so we need to create the object view. Objects in Elgg are a subclass of something called an “entity”. Users, sites, and groups are also subclasses of entity. All entities can (and should) have a subtype, which allows granular control for listing and displaying. Here, we have used the subtype "``my_blog``\ " to identify 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. In ``/mod/my_blog/views/default/``, create a folder ``/object/`` and then create a file ``my_blog.php`` in it. Each my\_blog post will be passed to this PHP file as ``$vars['entity']``. (``$vars`` is an array used in the views system to pass variables to a view.) The content of ``object/my_blog.php`` can just be something like: .. code:: php title); echo elgg_view('output/longtext', array('value' => $vars['entity']->description)); echo elgg_view('output/tags', array('tags' => $vars['entity']->tags)); The last line takes the tags on the my\_blog post and automatically displays them as a series of clickable links. Search is handled automatically. (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.) Plugin start.php ================ Every plugin has a ``start.php`` that initializes it. For this example, we just need to register the action file we created earlier: Also see a related guide about :doc:`/guides/actions`. .. code:: php Configure => Plugins => Advanced. Scroll to the bottom until you see your plugin. Click the Enable button. The page to create a new my\_blog post is accessible at http://yoursite/my_blog/add. Try it out. Displaying list of my\_blogs ============================ Let's also create a page that lists my\_blog entries that have been created. Create ``/mod/my_blog/pages/my_blog/all.php``. To grab the latest my\_blog posts, we'll use ``elgg_list_entities``. Note that this function returns only the posts that the user can see, so access restrictions are handled transparently: .. code:: php $body = elgg_list_entities(array( 'type' => 'object', 'subtype' => 'my_blog', )); The function \`elgg\_list\_entities\` (and its cousins) also transparently handle pagination, and even create an RSS feeds for your my\_blogs if you have defined these views. Finally, we'll draw the page: .. code:: php $body = elgg_view_layout('one_column', array('content' => $body)); echo elgg_view_page("All Site Blogs", $body); We will then need to modify our my\_blog page handler to grab the new page when the URL is set to ``/my_blog/all``. So, your new ``my_blog_page_handler()`` function in start.php should look like: .. code:: php function my_blog_page_handler($segments) { switch ($segments[0]) { case 'add': include elgg_get_plugins_path() . 'my_blog/pages/my_blog/add.php'; break; case 'all': default: include elgg_get_plugins_path() . 'my_blog/pages/my_blog/all.php'; break; } return true; } Now, if the URL contains just ``/my_blog`` or ``/my_blog/all``, the user will see an "All Site Blogs" page. A user's blog page ================== If we grab the Global Unique IDentifier (GUID) of the logged in user, we can limit the my\_blog posts to those posted by specifying the owner\_guid argument in the list function above. .. code:: php echo elgg_list_entities(array( 'type' => 'object', 'subtype' => 'my_blog', 'owner_guid' => elgg_get_logged_in_user_guid() )); The end ======= There's much more that could be done for this plugin, but hopefully this gives you a good idea of how to get started with your own.