diff --git a/core/lib/Drupal/Core/Plugin/Mapper/ConfigMapper.php b/core/lib/Drupal/Core/Plugin/Mapper/ConfigMapper.php index 6c8d0b0..bb9a2ec 100644 --- a/core/lib/Drupal/Core/Plugin/Mapper/ConfigMapper.php +++ b/core/lib/Drupal/Core/Plugin/Mapper/ConfigMapper.php @@ -43,7 +43,7 @@ public function getInstance(array $options) { $settings['config_id'] = $options['config']; return $this->manager->createInstance($plugin_id, $settings); } - // @todo throw an exception. + return FALSE; } } diff --git a/core/modules/block/block.module b/core/modules/block/block.module index 5ce5a21..9179332 100644 --- a/core/modules/block/block.module +++ b/core/modules/block/block.module @@ -474,10 +474,7 @@ function block_list($region) { */ function block_load($plugin_id, array $conf = array()) { $manager = drupal_container()->get('plugin.manager.block'); - try { - $block = $manager->getInstance(array('config' => $plugin_id)); - } - catch (Drupal\Component\Plugin\Exception\PluginException $e) { + if (!$block = $manager->getInstance(array('config' => $plugin_id))) { $block = $manager->createInstance($plugin_id, $conf); } return $block; @@ -546,7 +543,10 @@ function _block_get_renderable_block($element) { function block_build($block) { // Allow modules to modify the block before it is viewed, via either // hook_block_view_alter() or hook_block_view_ID_alter(). - $id = $block->getPluginID(); + $config = $block->getConfig(); + $config_id = explode('.', $config['config_id']); + $config_id = array_slice($config_id, 3); + $id = implode('_', $config_id); $build = $block->build(); drupal_alter(array('block_view', "block_view_{$id}"), $build, $block); return $build; diff --git a/core/modules/block/lib/Drupal/block/Plugin/system/plugin_ui/BlockPluginUI.php b/core/modules/block/lib/Drupal/block/Plugin/system/plugin_ui/BlockPluginUI.php index 16358d5..9fc2534 100644 --- a/core/modules/block/lib/Drupal/block/Plugin/system/plugin_ui/BlockPluginUI.php +++ b/core/modules/block/lib/Drupal/block/Plugin/system/plugin_ui/BlockPluginUI.php @@ -11,7 +11,9 @@ use Drupal\Core\Annotation\Translation; /** - * @todo. + * This plugin provides modifications to the system provided base class. Its + * purpose is to provide an overrideable user interface for block selection, + * configuration and placement. * * @Plugin( * id = "block_plugin_ui", @@ -40,13 +42,15 @@ class BlockPluginUI extends PluginUIBase { /** * Overrides \Drupal\system\Plugin\PluginUIBase::form(). */ - public function form($form, &$form_state, $plugin, $facet) { - list($plugin_id, $theme) = explode(':', $plugin); + public function form($form, &$form_state) { + $args = func_get_args(); + $facet = isset($args[2]) ? $args[2] : NULL; + list($plugin_id, $theme) = explode(':', $this->getPluginId()); $form['theme'] = array( '#type' => 'value', '#value' => $theme, ); - return parent::form($form, $form_state, $plugin, $facet); + return parent::form($form, $form_state, $facet); } /** @@ -68,8 +72,8 @@ public function formSubmit($form, &$form_state) { /** * Overrides \Drupal\system\Plugin\PluginUIBase::access(). */ - public function access($plugin_id) { - list($plugin, $theme) = explode(':', $plugin_id); + public function access() { + list($plugin, $theme) = explode(':', $this->getPluginId()); return _block_themes_access($theme); } @@ -83,16 +87,17 @@ public function tableHeader() { /** * Overrides \Drupal\system\Plugin\PluginUIBase::row(). */ - public function row($plugin, $plugin_definition, $plugin_id, $config) { - list($plugin, $theme) = explode(':', $plugin); + public function row($display_plugin_id, $display_plugin_definition) { + $plugin_definition = $this->getDefinition(); + list($plugin, $theme) = explode(':', $this->getPluginId()); $row = array(); - $row[] = $config['subject']; + $row[] = $display_plugin_definition['subject']; $row[] = array('data' => array( '#type' => 'operations', '#links' => array( 'configure' => array( 'title' => $plugin_definition['link_title'], - 'href' => $plugin_definition['config_path'] . '/' . $plugin_id . '/' . $theme, + 'href' => $plugin_definition['config_path'] . '/' . $display_plugin_id . '/' . $theme, ), ), )); diff --git a/core/modules/system/lib/Drupal/system/Plugin/PluginUIBase.php b/core/modules/system/lib/Drupal/system/Plugin/PluginUIBase.php index ce84dec..cd9a7bf 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/PluginUIBase.php +++ b/core/modules/system/lib/Drupal/system/Plugin/PluginUIBase.php @@ -10,14 +10,17 @@ use Drupal\Component\Plugin\PluginBase; /** - * @todo. + * An abstract base class for use in creating sane default user interfaces for + * plugins of a particular type. */ abstract class PluginUIBase extends PluginBase implements PluginUIInterface { /** - * @todo. + * Implements PluginUIInterface::form(). */ - public function form($form, &$form_state, $plugin, $facet) { + public function form($form, &$form_state) { + $args = func_get_args(); + $facet = isset($args[2]) ? $args[2] : NULL; $plugin_definition = $this->getDefinition(); $manager = new $plugin_definition['manager'](); $plugins = $this->excludeDefinitions($manager->getDefinitions()); @@ -36,17 +39,17 @@ public function form($form, &$form_state, $plugin, $facet) { '#value' => t('Next'), ); $rows = array(); - foreach ($plugins as $plugin_id => $config) { - if (empty($facet) || $this->facetCompare($facet, $config)) { - $row = $this->row($plugin, $plugin_definition, $plugin_id, $config); + foreach ($plugins as $plugin_id => $display_plugin_definition) { + if (empty($facet) || $this->facetCompare($facet, $display_plugin_definition)) { + $row = $this->row($plugin_id, $display_plugin_definition); $rows[] = $row; } foreach ($plugin_definition['facets'] as $key => $title) { - $facets[$key][$config[$key]] = $this->facetLink($key, $plugin, $plugin_definition, $plugin_id, $config, $key); + $facets[$key][$display_plugin_definition[$key]] = $this->facetLink($key, $plugin_id, $display_plugin_definition); } $form['right']['all_plugins'] = array( '#type' => 'markup', - '#markup' => l($plugin_definition['all_plugins'], $this->allPluginsUrl($plugin, $plugin_definition, $plugin_id, $config)), + '#markup' => l($plugin_definition['all_plugins'], $this->allPluginsUrl($plugin_id, $display_plugin_definition)), ); foreach ($facets as $group => $values) { $form['right'][$group] = array( @@ -64,65 +67,127 @@ public function form($form, &$form_state, $plugin, $facet) { } /** - * @todo. + * Implements PluginUIInterface::formValidate(). */ public function formValidate($form, &$form_state) { } /** - * @todo. + * Implements PluginUIInterface::formSumbit(). */ public function formSubmit($form, &$form_state) { } /** - * @todo. + * Allows a given plugin to exclude various defintions form the user + * interface via whatever criteria make sense for that plugin. + * + * @param array $definitions + * The plugin definitions provided by the plugin manager that this user + * interface is exposing. + * + * @return array + * A modified array of what was passed into the method. */ public function excludeDefinitions($definitions) { return $definitions; } /** - * @todo. + * Generic access check for use with plugins of this type. + * + * @return bool */ - public function access($plugin_id) { - $definition = $this->getDefinition($plugin_id); - return call_user_func_array('user_access', $definition['access_arguments']); + public function access() { + $definition = $this->getDefinition(); + return call_user_func_array($definition['access_callback'], $definition['access_arguments']); } /** - * @todo. + * A customized row method for displaying plugins for configuration within + * the user interface. + * + * @param $display_plugin_id + * The id of the specific plugin definition being passed to us. + * + * @param $display_plugin_definition + * The plugin definition associated with the passed $plugin_id. + * + * @return array + * An array that represents a table row in the final user interface output. */ - public function row($plugin, $plugin_definition, $plugin_id, $config) { - return array($config['title'], l($plugin_definition['link_title'], $plugin_definition['config_path'] . '/' . $plugin_id)); + public function row($display_plugin_id, $display_plugin_definition) { + $plugin_definition = $this->getDefinition(); + return array($display_plugin_definition['title'], l($plugin_definition['link_title'], $plugin_definition['config_path'] . '/' . $display_plugin_id)); } /** - * @todo. + * Provides individually formatted links for the faceting that happens within + * the user interface. Since this is a faceting style procedure, each plugin + * may be parsed multiple times in order to extract all facets and their + * appropriate labels. + * + * @param $facet + * A simple string indicating what element of the $display_plugin_definition + * to utilize for faceting. + * + * @param $display_plugin_id + * The plugin id of the plugin we are currently parsing a facet link from. + * + * @param $display_plugin_definition + * The plugin definition we are parsing. + * + * @return array + * Returns a theme_links compatible row array. */ - public function facetLink($facet, $plugin, $plugin_definition, $plugin_id, $config, $key) { - return array('title' => $config[$key], 'href' => $plugin_definition['path'] . '/' . $plugin . '/' . $facet . ':' . $config[$key]); + public function facetLink($facet, $display_plugin_id, $display_plugin_definition) { + $plugin_definition = $this->getDefinition(); + return array('title' => $display_plugin_definition[$facet], 'href' => $plugin_definition['path'] . '/' . $this->getPluginId() . '/' . $facet . ':' . $display_plugin_definition[$facet]); } /** - * @todo. + * Provides an "all" style link to reset the facets. the $display_plugin_id + * and $display_plugin_definition are provided for convenience. + * + * @param $display_plugin_id + * The plugin id of the plugin we are currently parsing a facet link from. + * + * @param $display_plugin_definition + * The plugin definition we are parsing. + * + * @return string + * returns a simple url string for use within l(). */ - public function allPluginsUrl($plugin, $plugin_definition, $plugin_id, $config) { - return $plugin_definition['path'] . '/' . $plugin . '/add'; + public function allPluginsUrl($display_plugin_id, $display_plugin_definition) { + $plugin_definition = $this->getDefinition(); + return $plugin_definition['path'] . '/' . $this->getPluginId() . '/add'; } /** - * @todo. + * Provides a theme_table compatible array of headers. + * + * @return array + * A theme_table compatible array of headers. */ public function tableHeader() { return array(t('Title'), t('Operations')); } /** - * @todo. + * Compares a given plugin definition with the selected facet to determine if + * the plugin should be displayed in the user interface. + * + * @param $facet + * A colon separated string representing the key/value paring of a selected + * facet. + * + * @param $display_plugin_definition + * The plugin definition to be compared. + * + * @return bool */ - public function facetCompare($facet, $config) { + public function facetCompare($facet, $display_plugin_definition) { list($facet_type, $option) = explode(':', $facet); - return $option == $config[$facet_type]; + return $option == $display_plugin_definition[$facet_type]; } } diff --git a/core/modules/system/lib/Drupal/system/Plugin/PluginUIInterface.php b/core/modules/system/lib/Drupal/system/Plugin/PluginUIInterface.php index f90388c..5a557f6 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/PluginUIInterface.php +++ b/core/modules/system/lib/Drupal/system/Plugin/PluginUIInterface.php @@ -19,14 +19,29 @@ * An associative array containing the structure of the form. * @param array $form_state * An associative array containing the current state of the form. - * @param string $plugin - * @todo. - * @param string|null $facet - * @todo. * * @return array * Returns the form structure as an array. */ - public function form($form, &$form_state, $plugin, $facet); + public function form($form, &$form_state); + /** + * Validates form values from the form() method. + * + * @param array $form + * An associative array containing the structure of the form. + * @param array $form_state + * An associative array containing the current state of the form. + */ + public function formValidate($form, &$form_state); + + /** + * Submits form values from the form() method. + * + * @param array $form + * An associative array containing the structure of the form. + * @param array $form_state + * An associative array containing the current state of the form. + */ + public function formSubmit($form, &$form_state); } diff --git a/core/modules/system/lib/Drupal/system/Plugin/Type/PluginUIManager.php b/core/modules/system/lib/Drupal/system/Plugin/Type/PluginUIManager.php index a4f3f8f..abbe63d 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/Type/PluginUIManager.php +++ b/core/modules/system/lib/Drupal/system/Plugin/Type/PluginUIManager.php @@ -10,7 +10,9 @@ use Drupal\Component\Plugin\PluginManagerBase; use Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator; use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery; -use Drupal\Component\Plugin\Factory\ReflectionFactory; +use Drupal\Core\Plugin\Discovery\AlterDecorator; +use Drupal\Core\Plugin\Discovery\CacheDecorator; +use Drupal\Component\Plugin\Factory\DefaultFactory; /** * Manages discovery and instantiation of Plugin UI plugins. @@ -21,8 +23,8 @@ class PluginUIManager extends PluginManagerBase { * Constructs a \Drupal\system\Plugin\Type\PluginUIManager object. */ public function __construct() { - $this->discovery = new DerivativeDiscoveryDecorator(new AnnotatedClassDiscovery('system', 'plugin_ui')); - $this->factory = new ReflectionFactory($this); + $this->discovery = new CacheDecorator(new AlterDecorator(new DerivativeDiscoveryDecorator(new AnnotatedClassDiscovery('system', 'plugin_ui')), 'plugin_ui'), 'plugin_ui'); + $this->factory = new DefaultFactory($this); } /** @@ -33,7 +35,7 @@ public function processDefinition(&$definition, $plugin_id) { 'default_task' => TRUE, 'task_title' => t('View'), 'task_suffix' => 'view', + 'access_callback' => 'user_access', ); } - } diff --git a/core/modules/system/system.module b/core/modules/system/system.module index 08304f1..2e5e17c 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -1121,7 +1121,7 @@ function system_menu() { */ function system_plugin_ui_form($form, &$form_state, $plugin, $facet = NULL) { $plugin_ui = drupal_container()->get('plugin.manager.system.plugin_ui')->createInstance($plugin); - $form = $plugin_ui->form($form, $form_state, $plugin, $facet); + $form = $plugin_ui->form($form, $form_state, $facet); $form['#validate'][] = array($plugin_ui, 'formValidate'); $form['#submit'][] = array($plugin_ui, 'formSubmit'); return $form; @@ -1150,7 +1150,7 @@ function system_plugin_autocomplete($plugin_id, $string = '') { */ function system_plugin_ui_access($plugin, $facet = NULL) { $plugin_ui = drupal_container()->get('plugin.manager.system.plugin_ui')->createInstance($plugin); - return $plugin_ui->access($plugin, $facet); + return $plugin_ui->access($facet); } /**