Issue #2987208 by akalata, alexpott, sime, tim.plunkett, Charlie ChX Negyesi: ElementInfoManager does not provide an alter hook, cannot alter element plugin definitions
parent
ce189a3f75
commit
d68cb0766b
|
@ -65,6 +65,7 @@ class ElementInfoManager extends DefaultPluginManager implements ElementInfoMana
|
|||
$this->cacheTagInvalidator = $cache_tag_invalidator;
|
||||
|
||||
parent::__construct('Element', $namespaces, $module_handler, 'Drupal\Core\Render\Element\ElementInterface', 'Drupal\Core\Render\Annotation\RenderElement');
|
||||
$this->alterInfo('element_plugin');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -810,6 +810,24 @@ function hook_element_info_alter(array &$info) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Alter Element plugin definitions.
|
||||
*
|
||||
* Whenever possible, hook_element_info_alter() should be used to alter the
|
||||
* default properties of an element type. Use this hook only when the plugin
|
||||
* definition itself needs to be altered.
|
||||
*
|
||||
* @param array $definitions
|
||||
* An array of Element plugin definitions.
|
||||
*
|
||||
* @see \Drupal\Core\Render\ElementInfoManager
|
||||
* @see \Drupal\Core\Render\Element\ElementInterface
|
||||
*/
|
||||
function hook_element_plugin_alter(array &$definitions) {
|
||||
// Use a custom class for the LayoutBuilder element.
|
||||
$definitions['layout_builder']['class'] = '\Drupal\mymodule\Element\MyLayoutBuilderElement';
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform necessary alterations to the JavaScript before it is presented on
|
||||
* the page.
|
||||
|
|
|
@ -14,6 +14,15 @@ function element_info_test_element_info_alter(array &$info) {
|
|||
$info['number']['#pre_render'][] = 'element_info_test_element_pre_render';
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_element_plugin_alter().
|
||||
*/
|
||||
function element_info_test_element_plugin_alter(array &$definitions) {
|
||||
if (\Drupal::state()->get('hook_element_plugin_alter:remove_weight', FALSE)) {
|
||||
unset($definitions['weight']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\KernelTests\Core\Render\Element;
|
||||
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* @group Render
|
||||
*/
|
||||
class PluginAlterTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $modules = ['element_info_test'];
|
||||
|
||||
/**
|
||||
* Tests hook_element_plugin_alter().
|
||||
*/
|
||||
public function testPluginAlter() {
|
||||
$info_manager = $this->container->get('plugin.manager.element_info');
|
||||
$this->assertArrayHasKey('weight', $info_manager->getDefinitions());
|
||||
|
||||
// @see element_info_test_element_plugin_alter()
|
||||
$this->container->get('state')->set('hook_element_plugin_alter:remove_weight', TRUE);
|
||||
// The definition will be cached.
|
||||
$this->assertArrayHasKey('weight', $info_manager->getDefinitions());
|
||||
|
||||
// Clearing the caches removes the definition.
|
||||
$info_manager->clearCachedDefinitions();
|
||||
$this->assertArrayNotHasKey('weight', $info_manager->getDefinitions());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue