Permalink
Browse files

feature(widgets): added a helper class and factory for defining widgets

  • Loading branch information...
jdalsem committed Mar 22, 2016
1 parent d41772c commit bc56fafd81c362964ac0764ec4641023d06560aa
Showing with 90 additions and 25 deletions.
  1. +10 −11 engine/classes/Elgg/WidgetsService.php
  2. +54 −0 engine/classes/ElggWidgetDefinition.php
  3. +26 −14 engine/lib/widgets.php
@@ -135,25 +135,24 @@ public function canEditLayout($context, $user_guid = 0) {
* @access private
* @since 1.9.0
*/
public function registerType($handler, $name, $description, array $context = array('all'), $multiple = false) {
if (!$handler || !$name) {
public function registerType(\ElggWidgetDefinition $definition) {
if (!$definition) {
return false;
}
$handler = $definition->handler;
if (!$handler) {
return false;
}
if (!isset($this->widgets)) {
$this->widgets = new \stdClass;
}
if (!isset($this->widgets->handlers)) {
$this->widgets->handlers = array();
$this->widgets->handlers = [];
}
$handlerobj = new \stdClass;
$handlerobj->name = $name;
$handlerobj->description = $description;
$handlerobj->context = $context;
$handlerobj->multiple = $multiple;
$this->widgets->handlers[$handler] = $handlerobj;
$this->widgets->handlers[$handler] = $definition;
return true;
}
@@ -0,0 +1,54 @@
<?php
/**
* \ElggWidgetDefinition
*
* Helper class for defining a widget
*
* @package Elgg.Core
* @subpackage Widgets
*
*/
class ElggWidgetDefinition {
/**
* @var string Handler of the widget
*/
public $handler;
/**
* \ElggWidgetDefinition constructor
*
* @param string $handler Handler of the widget
*/
public function __construct($handler) {
if (empty($handler)) {
$msg = elgg_echo('Handler missing for \ElggWidgetDefinition');
throw new \InvalidParameterException($msg);
}
$this->handler = $handler;
}
/**
* Create an \ElggWidgetDefinition from an associative array. Required key is handler.
*
* @param array $options Option array of key value pairs
*
* handler => STR Widget handler/identifier (required)
*
* @throws InvalidParameterException
* @return \ElggWidgetDefinition
*/
public static function factory(array $options) {
$definition = new \ElggWidgetDefinition($options['handler']);
unset($options['handler']);
foreach ($options as $key => $value) {
$definition->$key = $value;
}
return $definition;
}
}
View
@@ -59,26 +59,38 @@ function elgg_can_edit_widget_layout($context, $user_guid = 0) {
*
* This should be called by plugins in their init function.
*
* @param string $handler 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 (default: array('all'))
* @param bool $multiple Whether or not multiple instances of this widget
* are allowed in a single layout (default: false)
* @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 (default: array('all'))
* @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, $description, $context = array('all'), $multiple = false) {
if (is_string($context)) {
elgg_deprecated_notice('context parameters for elgg_register_widget_type() should be passed as an array())', 1.9);
$context = explode(",", $context);
} elseif (empty($context)) {
$context = array('all');
function elgg_register_widget_type($handler, $name = null, $description = null, $context = array('all'), $multiple = false) {
if (is_array($handler)) {
$definition = \ElggWidgetDefinition::factory($handler);
} else {
if (is_string($context)) {
elgg_deprecated_notice('context parameters for elgg_register_widget_type() should be passed as an array())', 1.9);
$context = explode(",", $context);
} elseif (empty($context)) {
$context = array('all');
}
$definition = \ElggWidgetDefinition::factory([
'handler' => $handler,
'name' => $name,
'description' => $description,
'context' => $context,
'multiple' => $multiple,
]);
}
return _elgg_services()->widgets->registerType($handler, $name, $description, $context, $multiple);
return _elgg_services()->widgets->registerType($definition);
}
/**

0 comments on commit bc56faf

Please sign in to comment.