Elgg\WidgetDefinition::factory PHP Method

factory() public static method

Create an WidgetDefinition from an associative array. Required key is id.
public static factory ( array $options ) : WidgetDefinition
$options array Option array of key value pairs id => STR Widget identifier (required) name => STR Name of the widget description => STR Description of the widget context => ARRAY contexts in which the widget is available multiple => BOOL can the widget be added multiple times
return WidgetDefinition
    public static function factory(array $options)
    {
        $id = elgg_extract('id', $options);
        $definition = new WidgetDefinition($id);
        $name = elgg_extract('name', $options);
        if (empty($name)) {
            if (elgg_language_key_exists("widgets:{$id}:name")) {
                $definition->name = elgg_echo("widgets:{$id}:name");
            } elseif (elgg_language_key_exists($id)) {
                $definition->name = elgg_echo($id);
            } else {
                $definition->name = $id;
            }
        } else {
            $definition->name = $name;
        }
        $description = elgg_extract('description', $options);
        if (empty($description)) {
            if (elgg_language_key_exists("widgets:{$id}:description")) {
                $definition->description = elgg_echo("widgets:{$id}:description");
            }
        } else {
            $definition->description = $description;
        }
        $context = (array) elgg_extract('context', $options, ['all']);
        if (in_array('all', $context)) {
            $context[] = 'profile';
            $context[] = 'dashboard';
            _elgg_services()->logger->notice("The widget '{$id}' need to be registered for explicit contexts");
            $pos = array_search('all', $context);
            unset($context[$pos]);
            $context = array_unique($context);
        }
        $definition->context = $context;
        $definition->multiple = (bool) elgg_extract('multiple', $options, false);
        return $definition;
    }

Usage Example

Example #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);
}
All Usage Examples Of Elgg\WidgetDefinition::factory