Commit 48025f92 authored by alexpott's avatar alexpott

Issue #2168159 by dawehner: Plugin Derivatives don't work with Objects returned from Annotations.

parent c53879f0
......@@ -27,7 +27,7 @@ abstract class DerivativeBase implements DerivativeInterface {
/**
* {@inheritdoc}
*/
public function getDerivativeDefinition($derivative_id, array $base_plugin_definition) {
public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) {
return $this->derivatives[$derivative_id];
}
......@@ -38,7 +38,7 @@ public function getDerivativeDefinition($derivative_id, array $base_plugin_defin
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
return $this->derivatives;
}
}
......@@ -18,16 +18,17 @@ interface DerivativeInterface {
* @param string $derivative_id
* The derivative id. The id must uniquely identify the derivative within a
* given base plugin, but derivative ids can be reused across base plugins.
* @param array $base_plugin_definition
* The definition array of the base plugin from which the derivative plugin
* is derived.
* @param mixed $base_plugin_definition
* The definition of the base plugin from which the derivative plugin
* is derived. It is maybe an entire object or just some array, depending
* on the discovery mechanism.
*
* @return array
* The full definition array of the derivative plugin, typically a merge of
* $base_plugin_definition with extra derivative-specific information. NULL
* if the derivative doesn't exist.
*/
public function getDerivativeDefinition($derivative_id, array $base_plugin_definition);
public function getDerivativeDefinition($derivative_id, $base_plugin_definition);
/**
* Returns the definition of all derivatives of a base plugin.
......@@ -39,6 +40,6 @@ public function getDerivativeDefinition($derivative_id, array $base_plugin_defin
*
* @see getDerivativeDefinition()
*/
public function getDerivativeDefinitions(array $base_plugin_definition);
public function getDerivativeDefinitions($base_plugin_definition);
}
......@@ -155,17 +155,17 @@ protected function encodePluginId($base_plugin_id, $derivative_id) {
*
* @param string $base_plugin_id
* The base plugin id of the plugin.
* @param array $base_definition
* @param mixed $base_definition
* The base plugin definition to build derivatives.
*
* @return \Drupal\Component\Plugin\Derivative\DerivativeInterface|null
+ * A DerivativeInterface or NULL if none exists for the plugin.
+ *
+ * @throws \Drupal\Component\Plugin\Exception\InvalidDerivativeClassException
+ * Thrown if the 'derivative' class specified in the plugin definition does
+ * not implement \Drupal\Component\Plugin\Derivative\DerivativeInterface.
* A DerivativeInterface or NULL if none exists for the plugin.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidDerivativeClassException
* Thrown if the 'derivative' class specified in the plugin definition does
* not implement \Drupal\Component\Plugin\Derivative\DerivativeInterface.
*/
protected function getDerivativeFetcher($base_plugin_id, array $base_definition) {
protected function getDerivativeFetcher($base_plugin_id, $base_definition) {
if (!isset($this->derivativeFetchers[$base_plugin_id])) {
$this->derivativeFetchers[$base_plugin_id] = FALSE;
$class = $this->getDerivativeClass($base_definition);
......@@ -191,8 +191,7 @@ protected function getDerivativeFetcher($base_plugin_id, array $base_definition)
*/
protected function getDerivativeClass($base_definition) {
$class = NULL;
if (isset($base_definition['derivative'])) {
$class = $base_definition['derivative'];
if ((is_array($base_definition) || ($base_definition = (array) $base_definition)) && (isset($base_definition['derivative']) && $class = $base_definition['derivative'])) {
if (!is_subclass_of($class, '\Drupal\Component\Plugin\Derivative\DerivativeInterface')) {
throw new InvalidDerivativeClassException(sprintf('Plugin (%s) derivative class "%s" has to implement interface \Drupal\Component\Plugin\Derivative\DerivativeInterface', $base_definition['id'], $class));
}
......
......@@ -63,7 +63,7 @@ public static function create(ContainerInterface $container, $base_plugin_id) {
/**
* {@inheritdoc}
*/
public function getDerivativeDefinition($derivative_id, array $base_plugin_definition) {
public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) {
return $this->derivatives[$derivative_id];
}
......@@ -76,7 +76,7 @@ public function getDerivativeDefinition($derivative_id, array $base_plugin_defin
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
// Also keep the 'entity' defined as is.
$this->derivatives[''] = $base_plugin_definition;
// Add definitions for each entity type and bundle.
......
......@@ -63,7 +63,7 @@ public static function create(ContainerInterface $container, $base_plugin_id) {
/**
* {@inheritdoc}
*/
public function getDerivativeDefinition($derivative_id, array $base_plugin_definition) {
public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
if (!isset($this->derivatives)) {
$this->getDerivativeDefinitions($base_plugin_definition);
}
......@@ -75,7 +75,7 @@ public function getDerivativeDefinition($derivative_id, array $base_plugin_defin
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
foreach ($this->fieldTypePluginManager->getDefinitions() as $plugin_id => $definition) {
$definition['definition_class'] = '\Drupal\Core\Field\TypedData\FieldItemDataDefinition';
$definition['list_definition_class'] = '\Drupal\Core\Field\FieldDefinition';
......
......@@ -14,7 +14,7 @@ class ContainerDerivativeDiscoveryDecorator extends DerivativeDiscoveryDecorator
/**
* {@inheritdoc}
*/
protected function getDerivativeFetcher($base_plugin_id, array $base_definition) {
protected function getDerivativeFetcher($base_plugin_id, $base_definition) {
if (!isset($this->derivativeFetchers[$base_plugin_id])) {
$this->derivativeFetchers[$base_plugin_id] = FALSE;
$class = $this->getDerivativeClass($base_definition);
......
......@@ -17,7 +17,7 @@ class CustomBlock extends DerivativeBase {
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
$custom_blocks = entity_load_multiple('custom_block');
foreach ($custom_blocks as $custom_block) {
$this->derivatives[$custom_block->uuid()] = $base_plugin_definition;
......
......@@ -58,7 +58,7 @@ public static function create(ContainerInterface $container, $base_plugin_id) {
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
$default_theme = $this->config->get('default');
foreach ($this->themeHandler->listInfo() as $theme_name => $theme) {
......
......@@ -47,7 +47,7 @@ public static function create(ContainerInterface $container, $base_plugin_id) {
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
// Create contextual links for all mappers.
$mappers = $this->mapperManager->getMappers();
foreach ($mappers as $plugin_id => $mapper) {
......
......@@ -57,7 +57,7 @@ public static function create(ContainerInterface $container, $base_plugin_id) {
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
$mappers = $this->mapperManager->getMappers();
foreach ($mappers as $plugin_id => $mapper) {
/** @var \Drupal\config_translation\ConfigMapperInterface $mapper */
......
......@@ -49,7 +49,7 @@ public static function create(ContainerInterface $container, $base_plugin_id) {
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
// Create contextual links for translatable entity types.
foreach ($this->contentTranslationManager->getSupportedEntityTypes() as $entity_type_id => $entity_type) {
$this->derivatives[$entity_type_id]['title'] = t('Translate');
......
......@@ -57,7 +57,7 @@ public static function create(ContainerInterface $container, $base_plugin_id) {
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
// Create tabs for all possible entity types.
foreach ($this->contentTranslationManager->getSupportedEntityTypes() as $entity_type_id => $entity_type) {
// Find the route name for the translation overview.
......
......@@ -16,7 +16,7 @@ class SelectionBase extends DerivativeBase {
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
$supported_entities = array(
'comment',
'file',
......
......@@ -70,7 +70,7 @@ public static function create(ContainerInterface $container, $base_plugin_id) {
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
$this->derivatives = array();
foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) {
......
......@@ -18,7 +18,7 @@ class LanguageBlock extends DerivativeBase {
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
$language_manager = \Drupal::languageManager();
if ($language_manager instanceof ConfigurableLanguageManagerInterface) {
......
......@@ -52,7 +52,7 @@ public static function create(ContainerInterface $container, $base_plugin_id) {
/**
* Implements DerivativeInterface::getDerivativeDefinition().
*/
public function getDerivativeDefinition($derivative_id, array $base_plugin_definition) {
public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
if (!isset($this->derivatives)) {
$this->getDerivativeDefinitions($base_plugin_definition);
}
......@@ -64,7 +64,7 @@ public function getDerivativeDefinition($derivative_id, array $base_plugin_defin
/**
* Implements DerivativeInterface::getDerivativeDefinitions().
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
if (!isset($this->derivatives)) {
// Add in the default plugin configuration and the resource type.
foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) {
......
......@@ -46,7 +46,7 @@ public static function create(ContainerInterface $container, $base_plugin_id) {
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
$this->derivatives = array();
if ($default = $this->searchPageRepository->getDefaultSearchPage()) {
......
......@@ -48,7 +48,7 @@ public static function create(ContainerInterface $container, $base_plugin_id) {
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
foreach ($this->menuStorage->loadMultiple() as $menu => $entity) {
$this->derivatives[$menu] = $base_plugin_definition;
$this->derivatives[$menu]['admin_label'] = $entity->label();
......
......@@ -17,7 +17,7 @@ class ThemeLocalTask extends DerivativeBase {
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
foreach (list_themes() as $theme_name => $theme) {
if ($theme->status) {
$this->derivatives[$theme_name] = $base_plugin_definition;
......
......@@ -17,7 +17,7 @@ class EntityTestLocalTasks extends DerivativeBase {
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
$this->derivatives = array();
$types = entity_test_entity_types();
......
......@@ -13,7 +13,7 @@ class LocalTaskTest extends DerivativeBase {
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
$weight = $base_plugin_definition['weight'];
foreach (array('derive1' => 'Derive 1', 'derive2' => 'Derive 2') as $key => $title) {
$this->derivatives[$key] = $base_plugin_definition;
......
......@@ -19,7 +19,7 @@ class MockLayoutBlockDeriver implements DerivativeInterface {
/**
* Implements Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinition().
*/
public function getDerivativeDefinition($derivative_id, array $base_plugin_definition) {
public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
$derivatives = $this->getDerivativeDefinitions($base_plugin_definition);
if (isset($derivatives[$derivative_id])) {
return $derivatives[$derivative_id];
......@@ -29,7 +29,7 @@ public function getDerivativeDefinition($derivative_id, array $base_plugin_defin
/**
* Implements Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinitions().
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
// This isn't strictly necessary, but it helps reduce clutter in
// DerivativePluginTest::testDerivativeDecorator()'s $expected variable.
// Since derivative definitions don't need further deriving, we remove this
......
......@@ -19,7 +19,7 @@ class MockMenuBlockDeriver implements DerivativeInterface {
/**
* Implements Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinition().
*/
public function getDerivativeDefinition($derivative_id, array $base_plugin_definition) {
public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
$derivatives = $this->getDerivativeDefinitions($base_plugin_definition);
if (isset($derivatives[$derivative_id])) {
return $derivatives[$derivative_id];
......@@ -29,7 +29,7 @@ public function getDerivativeDefinition($derivative_id, array $base_plugin_defin
/**
* Implements Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinitions().
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
// This isn't strictly necessary, but it helps reduce clutter in
// DerivativePluginTest::testDerivativeDecorator()'s $expected variable.
// Since derivative definitions don't need further deriving, we remove this
......
......@@ -19,7 +19,7 @@ class DefaultWizardDeriver extends DerivativeBase {
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
$views_data = Views::viewsData();
$base_tables = array_keys($views_data->fetchBaseTables());
$this->derivatives = array();
......
......@@ -65,7 +65,7 @@ public function __construct($base_plugin_id, EntityStorageControllerInterface $v
/**
* Implements \Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinition().
*/
public function getDerivativeDefinition($derivative_id, array $base_plugin_definition) {
public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) {
return $this->derivatives[$derivative_id];
}
......@@ -76,7 +76,7 @@ public function getDerivativeDefinition($derivative_id, array $base_plugin_defin
/**
* Implements \Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinitions().
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
// Check all Views for block displays.
foreach ($this->viewStorageController->loadMultiple() as $view) {
// Do not return results for disabled views.
......
......@@ -79,7 +79,7 @@ public static function create(ContainerInterface $container, $base_plugin_id) {
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
$entity_types = $this->entityManager->getDefinitions();
$this->derivatives = array();
foreach ($entity_types as $entity_type_id => $entity_type) {
......
......@@ -79,7 +79,7 @@ public static function create(ContainerInterface $container, $base_plugin_id) {
/**
* {@inheritdoc}
*/
public function getDerivativeDefinition($derivative_id, array $base_plugin_definition) {
public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) {
return $this->derivatives[$derivative_id];
}
......@@ -90,7 +90,7 @@ public function getDerivativeDefinition($derivative_id, array $base_plugin_defin
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) {
// Just add support for entity types which have a views integration.
if (($base_table = $entity_type->getBaseTable()) && $this->viewsData->get($base_table) && $this->entityManager->hasController($entity_type_id, 'view_builder')) {
......
......@@ -65,7 +65,7 @@ public static function create(ContainerInterface $container, $base_plugin_id) {
/**
* Implements \Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinition().
*/
public function getDerivativeDefinition($derivative_id, array $base_plugin_definition) {
public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) {
return $this->derivatives[$derivative_id];
}
......@@ -76,7 +76,7 @@ public function getDerivativeDefinition($derivative_id, array $base_plugin_defin
/**
* Implements \Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinitions().
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
// Check all Views for displays with an exposed filter block.
foreach ($this->viewStorageController->loadMultiple() as $view) {
// Do not return results for disabled views.
......
......@@ -59,7 +59,7 @@ public static function create(ContainerInterface $container, $base_plugin_id) {
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
$this->derivatives = array();
$view_route_names = $this->state->get('views.view_route_names');
......
......@@ -49,6 +49,40 @@ public function testGetDerivativeFetcher() {
// Ensure that both test derivatives got added.
$this->assertEquals(2, count($definitions));
$this->assertEquals('non_container_aware_discovery', $definitions['non_container_aware_discovery:test_discovery_0']['id']);
$this->assertEquals('\Drupal\Tests\Core\Plugin\Discovery\TestDerivativeDiscovery', $definitions['non_container_aware_discovery:test_discovery_0']['derivative']);
$this->assertEquals('non_container_aware_discovery', $definitions['non_container_aware_discovery:test_discovery_1']['id']);
$this->assertEquals('\Drupal\Tests\Core\Plugin\Discovery\TestDerivativeDiscovery', $definitions['non_container_aware_discovery:test_discovery_1']['derivative']);
}
/**
* Tests the getDerivativeFetcher method with objects instead of arrays.
*/
public function testGetDerivativeFetcherWithAnnotationObjects() {
$definitions = array();
$definitions['non_container_aware_discovery'] = (object) array(
'id' => 'non_container_aware_discovery',
'derivative' => '\Drupal\Tests\Core\Plugin\Discovery\TestDerivativeDiscoveryWithObject',
);
$discovery_main = $this->getMock('Drupal\Component\Plugin\Discovery\DiscoveryInterface');
$discovery_main->expects($this->any())
->method('getDefinitions')
->will($this->returnValue($definitions));
$discovery = new DerivativeDiscoveryDecorator($discovery_main);
$definitions = $discovery->getDefinitions();
// Ensure that both test derivatives got added.
$this->assertEquals(2, count($definitions));
$this->assertInstanceOf('\stdClass', $definitions['non_container_aware_discovery:test_discovery_0']);
$this->assertEquals('non_container_aware_discovery', $definitions['non_container_aware_discovery:test_discovery_0']->id);
$this->assertEquals('\Drupal\Tests\Core\Plugin\Discovery\TestDerivativeDiscoveryWithObject', $definitions['non_container_aware_discovery:test_discovery_0']->derivative);
$this->assertInstanceOf('\stdClass', $definitions['non_container_aware_discovery:test_discovery_1']);
$this->assertEquals('non_container_aware_discovery', $definitions['non_container_aware_discovery:test_discovery_1']->id);
$this->assertEquals('\Drupal\Tests\Core\Plugin\Discovery\TestDerivativeDiscoveryWithObject', $definitions['non_container_aware_discovery:test_discovery_1']->derivative);
}
/**
......@@ -56,7 +90,7 @@ public function testGetDerivativeFetcher() {
*
* @see \Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator::getDerivativeFetcher().\
*
* @expectedException Drupal\Component\Plugin\Exception\InvalidDerivativeClassException
* @expectedException \Drupal\Component\Plugin\Exception\InvalidDerivativeClassException
*/
public function testInvalidDerivativeFetcher() {
$definitions = array();
......@@ -71,6 +105,6 @@ public function testInvalidDerivativeFetcher() {
->will($this->returnValue($definitions));
$discovery = new DerivativeDiscoveryDecorator($discovery_main);
$definitions = $discovery->getDefinitions();
$discovery->getDefinitions();
}
}
......@@ -17,7 +17,7 @@ class TestDerivativeDiscovery implements DerivativeInterface {
/**
* {@inheritdoc}
*/
public function getDerivativeDefinition($derivative_id, array $base_plugin_definition) {
public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
$definitions = $this->getDerivativeDefinitions($base_plugin_definition);
return $definitions[$derivative_id];
}
......@@ -25,7 +25,7 @@ public function getDerivativeDefinition($derivative_id, array $base_plugin_defin
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
public function getDerivativeDefinitions($base_plugin_definition) {
$plugins = array();
for ($i = 0; $i < 2; $i++) {
$plugins['test_discovery_' . $i] = $base_plugin_definition;
......
<?php
/**
* @file
* Contains \Drupal\Tests\Core\Plugin\Discovery\TestDerivativeDiscoveryWithObject.
*/
namespace Drupal\Tests\Core\Plugin\Discovery;
use Drupal\Component\Plugin\Derivative\DerivativeInterface;
/**
* Defines test derivative discovery using an object..
*/
class TestDerivativeDiscoveryWithObject implements DerivativeInterface {
/**
* {@inheritdoc}
* @param string $derivative_id
* @param array $base_plugin_definition
* @return array
*/
public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
$definitions = $this->getDerivativeDefinitions($base_plugin_definition);
return $definitions[$derivative_id];
}
/**
* {@inheritdoc}
* @param array $base_plugin_definition
* @return array
*/
public function getDerivativeDefinitions($base_plugin_definition) {
$plugins = array();
for ($i = 0; $i < 2; $i++) {
$plugins['test_discovery_' . $i] = $base_plugin_definition;
}
return $plugins;
}
}
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