Building a Blog Plugin ###################### 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. .. contents:: Contents :local: :depth: 1 Prerequisites: - :doc:`Install Elgg` Create the plugin's directory and composer file =============================================== 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 composer file, ``composer.json``. See :doc:`Plugins` for more information about the composer file. Create the form for creating a new blog post ============================================ 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. .. code-block:: php echo elgg_view_field([ '#type' => 'text', '#label' => elgg_echo('title'), 'name' => 'title', 'required' => true, ]); echo elgg_view_field([ '#type' => 'longtext', '#label' => elgg_echo('body'), 'name' => 'body', 'required' => true, ]); echo elgg_view_field([ '#type' => 'tags', '#label' => elgg_echo('tags'), '#help' => elgg_echo('tags:help'), 'name' => 'tags', ]); $submit = elgg_view_field(array( '#type' => 'submit', '#class' => 'elgg-foot', 'value' => elgg_echo('save'), )); elgg_set_form_footer($submit); 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 :doc:`/guides/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 :doc:`Internationalization`. Create a page for composing the blogs ===================================== 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. .. code-block:: php $content, 'sidebar' => $sidebar ]); 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 ``