widget.rst 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. Basic Widget
  2. ============
  3. Create a widget that will display “Hello, World!” and optionally any text the user wants.
  4. In Elgg, widgets are those components that you can drag onto your profile or admin dashboard.
  5. This tutorial assumes you are familiar with basic Elgg concepts such as:
  6. * :doc:`/guides/views`
  7. * :doc:`/admin/plugins`
  8. You should review those if you get confused along the way.
  9. .. contents:: Contents
  10. :local:
  11. :depth: 1
  12. Registering your plugin
  13. -----------------------
  14. Plugins are always placed in the ``/mod`` directory.
  15. Create a subdirectory there called ``hello``.
  16. This will be the name of your plugin
  17. and will show up in the Plugins Administration section of Elgg by this name.
  18. In ``/mod/hello``, create an empty file called ``start.php``.
  19. If this file exists, Elgg will load your plugin.
  20. Otherwise, you will see a misconfigured plugin error.
  21. Go to the admin section of your Elgg install and enable your plugin.
  22. Click on the “more info” link under your plugin name.
  23. You will notice that nothing happens.
  24. * Copy the ``manifest.xml`` file from one of the plugins in your elgg install into ``/mod/hello``.
  25. * Update its values so you are listed as the author and change the description to describe this new plugin.
  26. * Reload the Tools Administration page in your browser and check “more info” again.
  27. * It will now display the information that you've entered.
  28. Adding the widget view code
  29. ---------------------------
  30. Elgg automatically scans particular directories under plugins looking for particular files.
  31. :doc:`/guides/views` make it easy to add your display code or do other things like override default Elgg behavior.
  32. For now, we will just be adding the view code for your widget.
  33. Create a file at ``/mod/hello/views/default/widgets/helloworld/content.php``.
  34. “helloworld” will be the name of your widget within the hello plugin.
  35. In this file add the code:
  36. .. code:: php
  37. <?php
  38. echo "Hello, world!";
  39. This will add these words to the widget canvas when it is drawn.
  40. Elgg takes care of loading the widget.
  41. Registering your widget
  42. -----------------------
  43. Elgg needs to be told explicitly that the plugin contains a widget
  44. so that it will scan the widget views directory.
  45. This is done by calling the elgg\_register\_widget\_type() function.
  46. Edit ``/mod/hello/start.php``. In it add these lines:
  47. .. code:: php
  48. <?php
  49. function hello_init() {
  50. elgg_register_widget_type('helloworld', 'Hello, world!', 'The "Hello, world!" widget');
  51. }
  52. elgg_register_event_handler('init', 'system', 'hello_init');
  53. Now go to your profile page using a web browser and add the “hello, world” widget.
  54. It should display “Hello, world!”.
  55. .. note::
  56. For real widgets, it is always a good idea to support :doc:`/guides/i18n`.
  57. Allow user customization
  58. ------------------------
  59. Click on the edit link on the toolbar of the widget that you've created.
  60. You will notice that the only control it gives you by default is over
  61. access (over who can see the widget).
  62. Suppose you want to allow the user to control what greeting is displayed in the widget.
  63. Just as Elgg automatically loads ``content.php`` when viewing a widget,
  64. it loads ``edit.php`` when a user attempts to edit a widget.
  65. In ``/mod/hello/views/default/widgets/helloworld/``, create a file named ``edit.php``.
  66. In this file, add the following code:
  67. .. code:: php
  68. <div>
  69. <label>Message:</label>
  70. <?php
  71. //This is an instance of the ElggWidget class that represents our widget.
  72. $widget = $vars['entity'];
  73. // Give the user a plain text box to input a message
  74. echo elgg_view('input/text', array(
  75. 'name' => 'params[message]',
  76. 'value' => $widget->message,
  77. 'class' => 'hello-input-text',
  78. ));
  79. ?>
  80. </div>
  81. Notice the relationship between the values passed to the 'name' and the
  82. 'value' fields of input/text.
  83. The name of the input text box is ``params[message]``
  84. because Elgg will automatically handle widget variables put in the array ``params``.
  85. The actual php variable name will be ``message``.
  86. If we wanted to use the field ``greeting`` instead of ``message``
  87. we would pass the values ``params[greeting]`` and ``$widget->greeting`` respectively.
  88. The reason we set the 'value' option of the array is so that the edit
  89. view remembers what the user typed in the previous time he changed the
  90. value of his message text.
  91. Now to display the user's message we need to modify content.php to use this *message* variable.
  92. Edit content.php and change it to:
  93. .. code:: php
  94. <?php
  95. $widget = $vars['entity'];
  96. // Always use the corresponding output/* view for security!
  97. echo elgg_view('output/text', array('value' => $widget->message));
  98. You should now be able to enter a message in the text box and see it appear in the widget.