Commit 5cc0a0ce authored by catch's avatar catch

Issue #1874498 by tim.plunkett, dawehner, jibran, benjy: Provide and use API...

Issue #1874498 by tim.plunkett, dawehner, jibran, benjy: Provide and use API methods for retrieving base plugin and derivative names from block plugin IDs.
parent 44ed771a
<?php
/**
* @file
* Contains Drupal\Component\Plugin\DerivativeInspectionInterface.
*/
namespace Drupal\Component\Plugin;
/**
* Provides a plugin interface for providing derivative metadata inspection.
*/
interface DerivativeInspectionInterface {
/**
* Returns the base_plugin_id of the plugin instance.
*
* @return string
* The base_plugin_id of the plugin instance.
*/
public function getBasePluginId();
/**
* Returns the derivative_id of the plugin instance.
*
* @return string|null
* The derivative_id of the plugin instance NULL otherwise.
*/
public function getDerivativeId();
}
......@@ -9,7 +9,12 @@
/**
* Base class for plugins wishing to support metadata inspection.
*/
abstract class PluginBase implements PluginInspectionInterface {
abstract class PluginBase implements PluginInspectionInterface, DerivativeInspectionInterface {
/**
* A string which is used to separate base plugin IDs from the derivative ID.
*/
const DERIVATIVE_SEPARATOR = ':';
/**
* The plugin_id.
......@@ -55,6 +60,29 @@ public function getPluginId() {
return $this->pluginId;
}
/**
* {@inheritdoc}
*/
public function getBasePluginId() {
$plugin_id = $this->getPluginId();
if (strpos($plugin_id, static::DERIVATIVE_SEPARATOR)) {
list($plugin_id) = explode(static::DERIVATIVE_SEPARATOR, $plugin_id, 2);
}
return $plugin_id;
}
/**
* {@inheritdoc}
*/
public function getDerivativeId() {
$plugin_id = $this->getPluginId();
$derivative_id = NULL;
if (strpos($plugin_id, static::DERIVATIVE_SEPARATOR)) {
list(, $derivative_id) = explode(static::DERIVATIVE_SEPARATOR, $plugin_id, 2);
}
return $derivative_id;
}
/**
* {@inheritdoc}
*/
......
......@@ -30,4 +30,5 @@ public function getPluginId();
* plugin manager.
*/
public function getPluginDefinition();
}
......@@ -479,6 +479,8 @@ function template_preprocess_block(&$variables) {
$variables['configuration'] = $variables['elements']['#configuration'];
$variables['plugin_id'] = $variables['elements']['#plugin_id'];
$variables['base_plugin_id'] = $variables['elements']['#base_plugin_id'];
$variables['derivative_plugin_id'] = $variables['elements']['#derivative_plugin_id'];
$variables['label'] = !empty($variables['configuration']['label_display']) ? $variables['configuration']['label'] : '';
$variables['content'] = $variables['elements']['content'];
......
......@@ -135,8 +135,7 @@ public function blockSubmit($form, &$form_state) {
* {@inheritdoc}
*/
public function build() {
// @todo Clean up when http://drupal.org/node/1874498 lands.
list(, $uuid) = explode(':', $this->getPluginId());
$uuid = $this->getDerivativeId();
if ($block = entity_load_by_uuid('custom_block', $uuid)) {
return entity_view($block, $this->configuration['view_mode']);
}
......
......@@ -38,6 +38,8 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la
foreach ($entities as $entity_id => $entity) {
$plugin = $entity->getPlugin();
$plugin_id = $plugin->getPluginId();
$base_id = $plugin->getBasePluginId();
$derivative_id = $plugin->getDerivativeId();
if ($content = $plugin->build()) {
$configuration = $plugin->getConfiguration();
......@@ -46,6 +48,8 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la
'content' => $content,
'#configuration' => $configuration,
'#plugin_id' => $plugin_id,
'#base_plugin_id' => $base_id,
'#derivative_plugin_id' => $derivative_id,
);
$build[$entity_id]['#configuration']['label'] = check_plain($configuration['label']);
}
......@@ -53,7 +57,6 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la
$build[$entity_id] = array();
}
list($base_id) = explode(':', $plugin_id);
drupal_alter(array('block_view', "block_view_$base_id"), $build[$entity_id], $plugin);
// @todo Remove after fixing http://drupal.org/node/1989568.
......
......@@ -10,7 +10,7 @@
use Drupal\simpletest\WebTestBase;
/**
* Unit tests for template_preprocess_block().
* Web tests for template_preprocess_block().
*/
class BlockPreprocessUnitTest extends WebTestBase {
......@@ -45,9 +45,13 @@ function testBlockClasses() {
$variables = array();
$variables['elements']['#block'] = $block;
$variables['elements']['#configuration'] = $block->getPlugin()->getConfiguration();
$variables['elements']['#plugin_id'] = $block->get('plugin');
$plugin = $block->getPlugin();
$variables['elements']['#configuration'] = $plugin->getConfiguration();
$variables['elements']['#plugin_id'] = $plugin->getPluginId();
$variables['elements']['#base_plugin_id'] = $plugin->getBasePluginId();
$variables['elements']['#derivative_plugin_id'] = $plugin->getDerivativeId();
$variables['elements']['content'] = array();
// Test adding a class to the block content.
$variables['content_attributes']['class'][] = 'test-class';
template_preprocess_block($variables);
......
......@@ -45,8 +45,11 @@ function testBlockThemeHookSuggestions() {
$variables = array();
$variables['elements']['#block'] = $block;
$variables['elements']['#configuration'] = $block->getPlugin()->getConfiguration();
$variables['elements']['#plugin_id'] = $block->get('plugin');
$plugin = $block->getPlugin();
$variables['elements']['#configuration'] = $plugin->getConfiguration();
$variables['elements']['#plugin_id'] = $plugin->getPluginId();
$variables['elements']['#base_plugin_id'] = $plugin->getBasePluginId();
$variables['elements']['#derivative_plugin_id'] = $plugin->getDerivativeId();
$variables['elements']['content'] = array();
$suggestions = block_theme_suggestions_block($variables);
$this->assertEqual($suggestions, array('block__system', 'block__system_menu_block', 'block__system_menu_block__admin', 'block__machinename'));
......
......@@ -37,7 +37,7 @@ function access(AccountInterface $account) {
public function build() {
$build = array();
$path = drupal_is_front_page() ? '<front>' : current_path();
list(, $type) = explode(':', $this->getPluginId());
$type = $this->getDerivativeId();
$links = language_negotiation_get_switch_links($type, $path);
if (isset($links->links)) {
......
......@@ -315,8 +315,7 @@ function _menu_parents_recurse($tree, $menu_name, $indent, &$options, $exclude,
function menu_block_view_system_menu_block_alter(array &$build, BlockPluginInterface $block) {
// Add contextual links for system menu blocks.
$menus = menu_list_system_menus();
// @todo Clean up when http://drupal.org/node/1874498 lands.
list(, $menu_name) = explode(':', $block->getPluginId());
$menu_name = $block->getDerivativeId();
if (isset($menus[$menu_name]) && isset($build['content'])) {
foreach (element_children($build['content']) as $key) {
$build['content']['#contextual_links']['menu'] = array(
......
......@@ -28,8 +28,7 @@ class SystemMenuBlock extends BlockBase {
* {@inheritdoc}
*/
public function access(AccountInterface $account) {
// @todo Clean up when http://drupal.org/node/1874498 lands.
list( , $derivative) = explode(':', $this->getPluginId());
$derivative = $this->getDerivativeId();
return ($account->isAuthenticated() || in_array($derivative, array('main', 'tools', 'footer')));
}
......@@ -37,8 +36,7 @@ public function access(AccountInterface $account) {
* {@inheritdoc}
*/
public function build() {
// @todo Clean up when http://drupal.org/node/1874498 lands.
list(, $menu) = explode(':', $this->getPluginId());
$menu = $this->getDerivativeId();
return menu_tree($menu);
}
......
......@@ -2181,10 +2181,7 @@ function system_user_timezone(&$form, &$form_state) {
* Implements hook_preprocess_HOOK() for block templates.
*/
function system_preprocess_block(&$variables) {
// Derive the base plugin ID.
// @todo Clean up when http://drupal.org/node/1874498 lands.
list($plugin_id, $derivative) = explode(':', $variables['plugin_id'] . ':');
switch ($plugin_id) {
switch ($variables['base_plugin_id']) {
case 'system_powered_by_block':
$variables['attributes']['role'] = 'complementary';
break;
......@@ -2195,7 +2192,7 @@ function system_preprocess_block(&$variables) {
case 'system_menu_block':
$menus = menu_list_system_menus();
if (isset($menus[$derivative])) {
if (isset($menus[$variables['derivative_plugin_id']])) {
$variables['attributes']['role'] = 'navigation';
$variables['attributes']['class'][] = 'block-menu';
}
......
......@@ -65,7 +65,7 @@ abstract class ViewsBlockBase extends BlockBase implements ContainerFactoryPlugi
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, ViewExecutableFactory $executable_factory, EntityStorageControllerInterface $storage_controller, AccountInterface $user) {
$this->pluginId = $plugin_id;
list($plugin, $delta) = explode(':', $this->getPluginId());
$delta = $this->getDerivativeId();
list($name, $this->displayID) = explode('-', $delta, 2);
// Load the view.
$view = $storage_controller->load($name);
......
<?php
/**
* @file
* Contains \Drupal\Tests\Component\Plugin\PluginBaseTest.
*/
namespace Drupal\Tests\Component\Plugin;
use Drupal\Component\PhpStorage\PhpStorageFactory;
use Drupal\Tests\UnitTestCase;
/**
* Tests the plugin base.
*
* @group Drupal
*
* @see \Drupal\Component\Plugin\PluginBase
*/
class PluginBaseTest extends UnitTestCase {
public static function getInfo() {
return array(
'name' => 'Plugin base test',
'description' => 'Tests the plugin base',
'group' => 'Plugin',
);
}
/**
* Tests the getPluginId method.
*
* @dataProvider providerTestGetPluginId
*
* @see \Drupal\Component\Plugin\PluginBase::getPluginId()
*
*/
public function testGetPluginId($plugin_id, $expected) {
$plugin_base = $this->getMockForAbstractClass('Drupal\Component\Plugin\PluginBase', array(
array(),
$plugin_id,
array(),
));
$this->assertEquals($expected, $plugin_base->getPluginId());
}
/**
* Returns test data for testGetPluginId().
*
* @return array
*/
public function providerTestGetPluginId() {
return array(
array('base_id', 'base_id'),
array('base_id:derivative', 'base_id:derivative'),
);
}
/**
* Tests the getBasePluginId method.
*
* @dataProvider providerTestGetBasePluginId
*
* @see \Drupal\Component\Plugin\PluginBase::getBasePluginId()
*/
public function testGetBasePluginId($plugin_id, $expected) {
$plugin_base = $this->getMockForAbstractClass('Drupal\Component\Plugin\PluginBase', array(
array(),
$plugin_id,
array(),
));
$this->assertEquals($expected, $plugin_base->getBasePluginId());
}
/**
* Returns test data for testGetBasePluginId().
*
* @return array
*/
public function providerTestGetBasePluginId() {
return array(
array('base_id', 'base_id'),
array('base_id:derivative', 'base_id'),
);
}
/**
* Tests the getBasePluginId method.
*
* @dataProvider providerTestGetDerivativeId
*
* @see \Drupal\Component\Plugin\PluginBase::getBasePluginId()
*/
public function testGetDerivativeId($plugin_id = NULL, $expected = NULL) {
$plugin_base = $this->getMockForAbstractClass('Drupal\Component\Plugin\PluginBase', array(
array(),
$plugin_id,
array(),
));
$this->assertEquals($expected, $plugin_base->getDerivativeId());
}
/**
* Returns test data for testGetBasePluginId().
*
* @return array
*/
public function providerTestGetDerivativeId() {
return array(
array('base_id', NULL),
array('base_id:derivative', 'derivative'),
);
}
/**
* Tests the getPluginDefinition method.
*
* @see \Drupal\Component\Plugin\PluginBase::getPluginDefinition()
*/
public function testGetPluginDefinition() {
$plugin_base = $this->getMockForAbstractClass('Drupal\Component\Plugin\PluginBase', array(
array(),
'plugin_id',
array('value', array('key' => 'value')),
));
$this->assertEquals(array('value', array('key' => 'value')), $plugin_base->getPluginDefinition());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment