Helper class for defining a widget
Exemple #1
0
/**
 * Register a widget type
 *
 * This should be called by plugins in their init function.
 *
 * @param string|array $handler     An array of options or the identifier for the widget handler
 * @param string       $name        The name of the widget type
 * @param string       $description A description for the widget type
 * @param array        $context     An array of contexts where this widget is allowed
 * @param bool         $multiple    Whether or not multiple instances of this widget
 *                                  are allowed in a single layout (default: false)
 *
 * @return bool
 * @since 1.8.0
 */
function elgg_register_widget_type($handler, $name = null, $description = null, $context = [], $multiple = false)
{
    if (is_array($handler)) {
        $definition = \Elgg\WidgetDefinition::factory($handler);
    } else {
        $definition = \Elgg\WidgetDefinition::factory(['id' => $handler, 'name' => $name, 'description' => $description, 'context' => $context, 'multiple' => $multiple]);
    }
    return _elgg_services()->widgets->registerType($definition);
}
Exemple #2
0
 /**
  * get registered widgets
  *
  * @param string $hook         the name of the hook
  * @param string $type         the type of the hook
  * @param string $return_value current return value
  * @param array  $params       supplied params
  *
  * @return void|\Elgg\WidgetDefinition[]
  */
 public static function getHandlers($hook, $type, $return_value, $params)
 {
     $page_owner = elgg_get_page_owner_entity();
     $context = elgg_extract('context', $params);
     switch ($context) {
         case 'index':
             $return_value[] = \Elgg\WidgetDefinition::factory(['id' => 'index_file', 'name' => elgg_echo('file'), 'description' => elgg_echo('widgets:index_file:description'), 'context' => [$context], 'multiple' => true]);
             break;
         case 'groups':
             if ($page_owner instanceof \ElggGroup && $page_owner->files_enable === 'no') {
                 // no files for this group
                 break;
             }
             $return_value[] = \Elgg\WidgetDefinition::factory(['id' => 'file_tree', 'name' => elgg_echo('widgets:file_tree:title'), 'description' => elgg_echo('widgets:file_tree:description'), 'context' => [$context], 'multiple' => true]);
             $return_value[] = \Elgg\WidgetDefinition::factory(['id' => 'group_files', 'name' => elgg_echo('file:group'), 'description' => elgg_echo('widgets:group_files:description'), 'context' => [$context]]);
             break;
         case 'profile':
         case 'dashboard':
             $return_value[] = \Elgg\WidgetDefinition::factory(['id' => 'file_tree', 'name' => elgg_echo('widgets:file_tree:title'), 'description' => elgg_echo('widgets:file_tree:description'), 'context' => [$context], 'multiple' => true]);
             break;
     }
     return $return_value;
 }
Exemple #3
0
 /**
  * Register a widget
  *
  * @param string $hook
  * @param string $type
  * @param array  $value
  * @param array $params
  *
  * @return \Elgg\WidgetDefinition[]
  */
 public function registerWidgetsHookHandler($hook, $type, $value, $params)
 {
     $value[] = \Elgg\WidgetDefinition::factory(['id' => 'hook_widget', 'name' => 'hook_widget name', 'description' => 'hook_widget description', 'context' => 'from_hook']);
     return $value;
 }
Exemple #4
0
 /**
  * Registers the plugin's widgets provided in the plugin config file
  *
  * @throws PluginException
  * @return void
  */
 protected function registerWidgets()
 {
     $widgets = _elgg_services()->widgets;
     $spec = (array) $this->getStaticConfig('widgets', []);
     foreach ($spec as $widget_id => $widget_definition) {
         if (!is_array($widget_definition)) {
             continue;
         }
         if (!isset($widget_definition['id'])) {
             $widget_definition['id'] = $widget_id;
         }
         $definition = \Elgg\WidgetDefinition::factory($widget_definition);
         $widgets->registerType($definition);
     }
 }