Commit 0333c019 authored by webchick's avatar webchick

Issue #2184951 by not_chx: Allow plugins to declare themselves a derivative.

parent e4e74fcf
......@@ -40,13 +40,21 @@ public function __construct(DiscoveryInterface $decorated) {
* not implement \Drupal\Component\Plugin\Derivative\DerivativeInterface.
*/
public function getDefinition($plugin_id) {
$plugin_definition = $this->decorated->getDefinition($plugin_id);
list($base_plugin_id, $derivative_id) = $this->decodePluginId($plugin_id);
$plugin_definition = $this->decorated->getDefinition($base_plugin_id);
if (isset($plugin_definition)) {
$derivative_fetcher = $this->getDerivativeFetcher($base_plugin_id, $plugin_definition);
$base_plugin_definition = $this->decorated->getDefinition($base_plugin_id);
if ($base_plugin_definition) {
$derivative_fetcher = $this->getDerivativeFetcher($base_plugin_id, $base_plugin_definition);
if ($derivative_fetcher) {
$plugin_definition = $derivative_fetcher->getDerivativeDefinition($derivative_id, $plugin_definition);
$derivative_plugin_definition = $derivative_fetcher->getDerivativeDefinition($derivative_id, $base_plugin_definition);
// If a plugin defined itself as a derivative, merge in possible
// defaults from the derivative.
if ($derivative_id && isset($plugin_definition)) {
$plugin_definition += $derivative_plugin_definition ?: array();
}
else {
$plugin_definition = $derivative_plugin_definition;
}
}
}
......@@ -79,10 +87,17 @@ protected function getDerivatives(array $base_plugin_definitions) {
$derivative_definitions = $derivative_fetcher->getDerivativeDefinitions($plugin_definition);
foreach ($derivative_definitions as $derivative_id => $derivative_definition) {
$plugin_id = $this->encodePluginId($base_plugin_id, $derivative_id);
// Use this definition as defaults if a plugin already defined
// itself as this derivative.
if ($derivative_id && isset($base_plugin_definitions[$plugin_id])) {
$derivative_definition = $base_plugin_definitions[$plugin_id] + ($derivative_definition ?: array());
}
$plugin_definitions[$plugin_id] = $derivative_definition;
}
}
else {
// If a plugin already defined itself as a derivative it might already
// be merged into the definitions.
elseif (!isset($plugin_definitions[$base_plugin_id])) {
$plugin_definitions[$base_plugin_id] = $plugin_definition;
}
}
......
......@@ -676,15 +676,3 @@ function comment_views_data_alter(&$data) {
}
}
}
/**
* Implements hook_views_plugins_row_alter().
*
* Replaces the generic row plugin by a custom one for comments.
*
* @see \Drupal\views\Plugin\views\row\EntityRow
*/
function comment_views_plugins_row_alter(array &$plugins) {
$plugins['entity:comment']['class'] = 'Drupal\comment\Plugin\views\row\CommentRow';
$plugins['entity:comment']['provider'] = 'comment';
}
......@@ -11,6 +11,10 @@
/**
* Plugin which performs a comment_view on the resulting object.
*
* @ViewsRow(
* id = "entity:comment",
* )
*/
class CommentRow extends EntityRow {
......
......@@ -15,6 +15,10 @@
* Most of the code on this object is in the theme function.
*
* @ingroup views_row_plugins
*
* @ViewsRow(
* id = "entity:node",
* )
*/
class NodeRow extends EntityRow {
......
......@@ -656,15 +656,3 @@ function node_views_wizard() {
}
}
/**
* Implements hook_views_plugins_row_alter().
*
* Replaces the generic row plugin by a custom one for nodes.
*
* @see \Drupal\views\Plugin\views\row\EntityRow
*/
function node_views_plugins_row_alter(array &$plugins) {
$plugins['entity:node']['class'] = 'Drupal\node\Plugin\views\row\NodeRow';
$plugins['entity:node']['provider'] = 'node';
}
......@@ -62,6 +62,11 @@ public function setUp() {
'label' => 'Navigation',
'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockMenuBlock',
),
'menu:foo' => array(
'label' => 'Base label',
'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockMenuBlock',
'setting' => 'default',
),
'layout' => array(
'label' => 'Layout',
'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockLayoutBlock',
......
......@@ -53,6 +53,11 @@ public function __construct() {
'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockMenuBlock',
'derivative' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockMenuBlockDeriver',
));
// A plugin defining itself as a derivative.
$this->discovery->setDefinition('menu:foo', array(
'label' => t('Base label'),
'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockMenuBlock',
));
// A block plugin that can optionally be derived: the layout block plugin.
// A layout is a special kind of block into which other blocks can be
......
......@@ -46,6 +46,12 @@ public function getDerivativeDefinitions(array $base_plugin_definition) {
'navigation' => array(
'label' => t('Navigation'),
) + $base_plugin_definition,
'foo' => array(
// Instead of the derivative label, the specific label will be used.
'label' => t('Derivative label'),
// This setting will be merged in.
'setting' => 'default'
) + $base_plugin_definition,
);
return $derivatives;
......
......@@ -13,6 +13,10 @@
* A row plugin which renders a user.
*
* @ingroup views_row_plugins
*
* @ViewsRow(
* id = "entity:user",
* )
*/
class UserRow extends EntityRow {
......
......@@ -378,16 +378,3 @@ function user_views_data_alter(&$data) {
),
);
}
/**
* Implements hook_views_plugins_row_alter().
*
* Replaces the generic row plugin by a custom one for users.
*
* @see \Drupal\views\Plugin\views\row\EntityRow
*/
function user_views_plugins_row_alter(array &$plugins) {
$plugins['entity:user']['class'] = 'Drupal\user\Plugin\views\row\UserRow';
$plugins['entity:user']['provider'] = 'user';
}
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