Issue #2824655 by tim.plunkett: Plugin definitions should store their class in a consistent manner (without leading slashes)

8.4.x
effulgentsia 2017-02-07 17:10:28 -08:00
parent 9088257709
commit ed4ad77a12
6 changed files with 34 additions and 10 deletions

View File

@ -63,6 +63,17 @@ class TypedConfigManager extends TypedDataManager implements TypedConfigManagerI
return $this->discovery;
}
/**
* {@inheritdoc}
*/
public function processDefinition(&$definition, $plugin_id) {
parent::processDefinition($definition, $plugin_id);
// Typed config definitions assume a leading slash, see ::hasConfigSchema().
if (is_array($definition) && isset($definition['class'])) {
$definition['class'] = '\\' . $definition['class'];
}
}
/**
* {@inheritdoc}

View File

@ -80,10 +80,6 @@ class LayoutPluginManager extends DefaultPluginManager implements LayoutPluginMa
throw new InvalidPluginDefinitionException($plugin_id, sprintf('The "%s" layout definition must extend %s', $plugin_id, LayoutDefinition::class));
}
// Keep class definitions standard with no leading slash.
// @todo Remove this once https://www.drupal.org/node/2824655 is resolved.
$definition->setClass(ltrim($definition->getClass(), '\\'));
// Add the module or theme path to the 'path'.
$provider = $definition->getProvider();
if ($this->moduleHandler->moduleExists($provider)) {

View File

@ -240,13 +240,17 @@ class DefaultPluginManager extends PluginManagerBase implements PluginManagerInt
* method.
*/
public function processDefinition(&$definition, $plugin_id) {
// Only arrays can be operated on.
if (!is_array($definition)) {
return;
// Only array-based definitions can have defaults merged in.
if (is_array($definition) && !empty($this->defaults) && is_array($this->defaults)) {
$definition = NestedArray::mergeDeep($this->defaults, $definition);
}
if (!empty($this->defaults) && is_array($this->defaults)) {
$definition = NestedArray::mergeDeep($this->defaults, $definition);
// Keep class definitions standard with no leading slash.
if ($definition instanceof PluginDefinitionInterface) {
$definition->setClass(ltrim($definition->getClass(), '\\'));
}
elseif (is_array($definition) && isset($definition['class'])) {
$definition['class'] = ltrim($definition['class'], '\\');
}
}

View File

@ -98,6 +98,7 @@ class EntityTypeManagerTest extends UnitTestCase {
// Give the entity type a legitimate class to return.
$entity_type->getClass()->willReturn($class);
$entity_type->setClass($class)->willReturn($entity_type->reveal());
$definitions[$key] = $entity_type->reveal();
}

View File

@ -4,6 +4,7 @@ namespace Drupal\Tests\Core\Menu;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Language\Language;
use Drupal\Core\Menu\ContextualLinkDefault;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\RequestStack;
@ -384,7 +385,7 @@ class ContextualLinkManagerTest extends UnitTestCase {
public function testPluginDefinitionAlter() {
$definitions['test_plugin'] = array(
'id' => 'test_plugin',
'class' => '\Drupal\Core\Menu\ContextualLinkDefault',
'class' => ContextualLinkDefault::class,
'title' => 'Plugin',
'weight' => 2,
'group' => 'group1',

View File

@ -438,6 +438,17 @@ class DefaultPluginManagerTest extends UnitTestCase {
'forms' => ['configure' => 'stdClass'],
'foo' => ['bar' => ['baz']],
];
$data['class_with_slashes'][] = [
'class' => '\Drupal\Tests\Core\Plugin\TestPluginForm',
];
$data['class_with_slashes'][] = [
'class' => 'Drupal\Tests\Core\Plugin\TestPluginForm',
'foo' => ['bar' => ['baz']],
];
$data['object_with_class_with_slashes'][] = (new PluginDefinition())->setClass('\Drupal\Tests\Core\Plugin\TestPluginForm');
$data['object_with_class_with_slashes'][] = (new PluginDefinition())->setClass('Drupal\Tests\Core\Plugin\TestPluginForm');
return $data;
}