Add widgets on Elgg conditionally

It may happen sometimes that need to add or remove widgets conditionally. A simple example is if need to allow widgets only for some members, depending on certain criteria like type of user (if profile manager plugin is used).


In such cases, the following should be done:

1. Register hook in your plugin

elgg_register_plugin_hook_handler(

     'handlers',

     'widgets',

     'myplugin_conditional_widgets_hook'

);

2. Create a function for this hook

function myplugin_conditional_widgets_hook($hook, $type, $return, $params) {

      if ( {put here your custom conditions} ) {
           $return[] = \Elgg\WidgetDefinition::factory([
                'id' => 'my_widget' // the ID of the widget
           ]);
      }

      return $return;
}

3. Add the lines belows in language files

'widgets:my_widget:name' => 'Widget name',
'widgets:my_widget:description' => "Widget description",

Shopping Cart

Your cart is empty.