diff --git a/core/core.services.yml b/core/core.services.yml index 70c6441f91e..0e7eb586e93 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -577,9 +577,6 @@ services: entity.autocomplete_matcher: class: Drupal\Core\Entity\EntityAutocompleteMatcher arguments: ['@plugin.manager.entity_reference_selection'] - plugin_form.factory: - class: Drupal\Core\Plugin\PluginFormFactory - arguments: ['@class_resolver'] plugin.manager.entity_reference_selection: class: Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager parent: default_plugin_manager diff --git a/core/lib/Drupal/Component/Plugin/PluginAwareInterface.php b/core/lib/Drupal/Component/Plugin/PluginAwareInterface.php deleted file mode 100644 index 5506c9f7ab9..00000000000 --- a/core/lib/Drupal/Component/Plugin/PluginAwareInterface.php +++ /dev/null @@ -1,18 +0,0 @@ -transliteration = $transliteration; } - /** - * {@inheritdoc} - */ - public function getFormClass($operation) { - if ($this->hasFormClass($operation)) { - return $this->getPluginDefinition()['forms'][$operation]; - } - } - - /** - * {@inheritdoc} - */ - public function hasFormClass($operation) { - return isset($this->getPluginDefinition()['forms'][$operation]); - } - } diff --git a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php index fa706a47adc..06365fa0da0 100644 --- a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php +++ b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php @@ -239,20 +239,9 @@ class DefaultPluginManager extends PluginManagerBase implements PluginManagerInt * method. */ public function processDefinition(&$definition, $plugin_id) { - // Only arrays can be operated on. - if (!is_array($definition)) { - return; - } - if (!empty($this->defaults) && is_array($this->defaults)) { $definition = NestedArray::mergeDeep($this->defaults, $definition); } - - // If no default form is defined and this plugin implements - // \Drupal\Core\Plugin\PluginFormInterface, use that for the default form. - if (!isset($definition['forms']['configure']) && isset($definition['class']) && is_subclass_of($definition['class'], PluginFormInterface::class)) { - $definition['forms']['configure'] = $definition['class']; - } } /** diff --git a/core/lib/Drupal/Core/Plugin/PluginFormBase.php b/core/lib/Drupal/Core/Plugin/PluginFormBase.php deleted file mode 100644 index d0fe1b4026e..00000000000 --- a/core/lib/Drupal/Core/Plugin/PluginFormBase.php +++ /dev/null @@ -1,38 +0,0 @@ -plugin = $plugin; - } - - /** - * {@inheritdoc} - */ - public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { - // Validation is optional. - } - -} diff --git a/core/lib/Drupal/Core/Plugin/PluginFormFactory.php b/core/lib/Drupal/Core/Plugin/PluginFormFactory.php deleted file mode 100644 index 6cbbc22c763..00000000000 --- a/core/lib/Drupal/Core/Plugin/PluginFormFactory.php +++ /dev/null @@ -1,67 +0,0 @@ -classResolver = $class_resolver; - } - - /** - * {@inheritdoc} - */ - public function createInstance(PluginWithFormsInterface $plugin, $operation, $fallback_operation = NULL) { - if (!$plugin->hasFormClass($operation)) { - // Use the default form class if no form is specified for this operation. - if ($fallback_operation && $plugin->hasFormClass($fallback_operation)) { - $operation = $fallback_operation; - } - else { - throw new InvalidPluginDefinitionException($plugin->getPluginId(), sprintf('The "%s" plugin did not specify a "%s" form class', $plugin->getPluginId(), $operation)); - } - } - - $form_class = $plugin->getFormClass($operation); - - // If the form specified is the plugin itself, use it directly. - if (ltrim(get_class($plugin), '\\') === ltrim($form_class, '\\')) { - $form_object = $plugin; - } - else { - $form_object = $this->classResolver->getInstanceFromDefinition($form_class); - } - - // Ensure the resulting object is a plugin form. - if (!$form_object instanceof PluginFormInterface) { - throw new InvalidPluginDefinitionException($plugin->getPluginId(), sprintf('The "%s" plugin did not specify a valid "%s" form class, must implement \Drupal\Core\Plugin\PluginFormInterface', $plugin->getPluginId(), $operation)); - } - - if ($form_object instanceof PluginAwareInterface) { - $form_object->setPlugin($plugin); - } - - return $form_object; - } - -} diff --git a/core/lib/Drupal/Core/Plugin/PluginFormFactoryInterface.php b/core/lib/Drupal/Core/Plugin/PluginFormFactoryInterface.php deleted file mode 100644 index 31679de2693..00000000000 --- a/core/lib/Drupal/Core/Plugin/PluginFormFactoryInterface.php +++ /dev/null @@ -1,34 +0,0 @@ -storage = $entity_manager->getStorage('block'); $this->manager = $manager; $this->contextRepository = $context_repository; $this->language = $language; $this->themeHandler = $theme_handler; - $this->pluginFormFactory = $plugin_form_manager; } /** @@ -111,8 +99,7 @@ class BlockForm extends EntityForm { $container->get('plugin.manager.condition'), $container->get('context.repository'), $container->get('language_manager'), - $container->get('theme_handler'), - $container->get('plugin_form.factory') + $container->get('theme_handler') ); } @@ -133,7 +120,7 @@ class BlockForm extends EntityForm { $form_state->setTemporaryValue('gathered_contexts', $this->contextRepository->getAvailableContexts()); $form['#tree'] = TRUE; - $form['settings'] = $this->getPluginForm($entity->getPlugin())->buildConfigurationForm(array(), $form_state); + $form['settings'] = $entity->getPlugin()->buildConfigurationForm(array(), $form_state); $form['visibility'] = $this->buildVisibilityInterface([], $form_state); // If creating a new block, calculate a safe default machine name. @@ -295,7 +282,7 @@ class BlockForm extends EntityForm { // settings form element, so just pass that to the block for validation. $settings = (new FormState())->setValues($form_state->getValue('settings')); // Call the plugin validate handler. - $this->getPluginForm($this->entity->getPlugin())->validateConfigurationForm($form, $settings); + $this->entity->getPlugin()->validateConfigurationForm($form, $settings); // Update the original form values. $form_state->setValue('settings', $settings->getValues()); $this->validateVisibility($form, $form_state); @@ -342,8 +329,8 @@ class BlockForm extends EntityForm { $settings = (new FormState())->setValues($form_state->getValue('settings')); // Call the plugin submit handler. + $entity->getPlugin()->submitConfigurationForm($form, $settings); $block = $entity->getPlugin(); - $this->getPluginForm($block)->submitConfigurationForm($form, $settings); // If this block is context-aware, set the context mapping. if ($block instanceof ContextAwarePluginInterface && $block->getContextDefinitions()) { $context_mapping = $settings->getValue('context_mapping', []); @@ -415,17 +402,4 @@ class BlockForm extends EntityForm { return $machine_default; } - /** - * Retrieves the plugin form for a given block and operation. - * - * @param \Drupal\Core\Block\BlockPluginInterface $block - * The block plugin. - * - * @return \Drupal\Core\Plugin\PluginFormInterface - * The plugin form for the block. - */ - protected function getPluginForm(BlockPluginInterface $block) { - return $this->pluginFormFactory->createInstance($block, 'configure'); - } - } diff --git a/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestMultipleFormsBlock.php b/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestMultipleFormsBlock.php deleted file mode 100644 index 64b70b22c04..00000000000 --- a/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestMultipleFormsBlock.php +++ /dev/null @@ -1,27 +0,0 @@ -method('getStorage') ->will($this->returnValue($this->storage)); - $this->pluginFormFactory = $this->prophesize(PluginFormFactoryInterface::class); } /** @@ -108,7 +99,7 @@ class BlockFormTest extends UnitTestCase { ->method('getQuery') ->will($this->returnValue($query)); - $block_form_controller = new BlockForm($this->entityManager, $this->conditionManager, $this->contextRepository, $this->language, $this->themeHandler, $this->pluginFormFactory->reveal()); + $block_form_controller = new BlockForm($this->entityManager, $this->conditionManager, $this->contextRepository, $this->language, $this->themeHandler); // Ensure that the block with just one other instance gets the next available // name suggestion. diff --git a/core/tests/Drupal/KernelTests/Core/Block/MultipleBlockFormTest.php b/core/tests/Drupal/KernelTests/Core/Block/MultipleBlockFormTest.php deleted file mode 100644 index 2fbe025acfe..00000000000 --- a/core/tests/Drupal/KernelTests/Core/Block/MultipleBlockFormTest.php +++ /dev/null @@ -1,38 +0,0 @@ - 'A very cool block']; - $block = \Drupal::service('plugin.manager.block')->createInstance('test_multiple_forms_block', $configuration); - - $form_object1 = \Drupal::service('plugin_form.factory')->createInstance($block, 'configure'); - $form_object2 = \Drupal::service('plugin_form.factory')->createInstance($block, 'secondary'); - - // Assert that the block itself is used for the default form. - $this->assertSame($block, $form_object1); - - // Ensure that EmptyBlockForm is used and the plugin is set. - $this->assertInstanceOf(EmptyBlockForm::class, $form_object2); - $this->assertAttributeEquals($block, 'plugin', $form_object2); - } - -} diff --git a/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php b/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php index feac00467c2..8cc0f81c1aa 100644 --- a/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php @@ -3,8 +3,6 @@ namespace Drupal\Tests\Core\Plugin; use Drupal\Core\Extension\ModuleHandlerInterface; -use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Plugin\PluginFormInterface; use Drupal\Tests\UnitTestCase; /** @@ -340,98 +338,4 @@ class DefaultPluginManagerTest extends UnitTestCase { $this->assertInternalType('int', $cache_max_age); } - /** - * @covers ::processDefinition - * @dataProvider providerTestProcessDefinition - */ - public function testProcessDefinition($definition, $expected) { - $module_handler = $this->prophesize(ModuleHandlerInterface::class); - $plugin_manager = new TestPluginManagerWithDefaults($this->namespaces, $this->expectedDefinitions, $module_handler->reveal(), NULL); - - $plugin_manager->processDefinition($definition, 'the_plugin_id'); - $this->assertEquals($expected, $definition); - } - - public function providerTestProcessDefinition() { - $data = []; - - $data['merge'][] = [ - 'foo' => [ - 'bar' => [ - 'asdf', - ], - ], - ]; - $data['merge'][] = [ - 'foo' => [ - 'bar' => [ - 'baz', - 'asdf', - ], - ], - ]; - - $object_definition = (object) [ - 'foo' => [ - 'bar' => [ - 'asdf', - ], - ], - ]; - $data['object_definition'] = [$object_definition, clone $object_definition]; - - $data['no_form'][] = ['class' => TestPluginForm::class]; - $data['no_form'][] = [ - 'class' => TestPluginForm::class, - 'forms' => ['configure' => TestPluginForm::class], - 'foo' => ['bar' => ['baz']], - ]; - - $data['default_form'][] = ['class' => TestPluginForm::class, 'forms' => ['configure' => 'stdClass']]; - $data['default_form'][] = [ - 'class' => TestPluginForm::class, - 'forms' => ['configure' => 'stdClass'], - 'foo' => ['bar' => ['baz']], - ]; - return $data; - } - -} - -class TestPluginManagerWithDefaults extends TestPluginManager { - - /** - * {@inheritdoc} - */ - protected $defaults = [ - 'foo' => [ - 'bar' => [ - 'baz', - ], - ], - ]; - -} - -class TestPluginForm implements PluginFormInterface { - - /** - * {@inheritdoc} - */ - public function buildConfigurationForm(array $form, FormStateInterface $form_state) { - return []; - } - - /** - * {@inheritdoc} - */ - public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { - } - - /** - * {@inheritdoc} - */ - public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { - } - } diff --git a/core/tests/Drupal/Tests/Core/Plugin/PluginFormFactoryTest.php b/core/tests/Drupal/Tests/Core/Plugin/PluginFormFactoryTest.php deleted file mode 100644 index fc4b77aebe1..00000000000 --- a/core/tests/Drupal/Tests/Core/Plugin/PluginFormFactoryTest.php +++ /dev/null @@ -1,156 +0,0 @@ -classResolver = $this->prophesize(ClassResolverInterface::class); - $this->manager = new PluginFormFactory($this->classResolver->reveal()); - } - - /** - * @covers ::createInstance - */ - public function testCreateInstance() { - $plugin_form = $this->prophesize(PluginFormInterface::class); - $expected = $plugin_form->reveal(); - - $this->classResolver->getInstanceFromDefinition(get_class($expected))->willReturn($expected); - - $plugin = $this->prophesize(PluginWithFormsInterface::class); - $plugin->hasFormClass('standard_class')->willReturn(TRUE); - $plugin->getFormClass('standard_class')->willReturn(get_class($expected)); - - $form_object = $this->manager->createInstance($plugin->reveal(), 'standard_class'); - $this->assertSame($expected, $form_object); - } - - /** - * @covers ::createInstance - */ - public function testCreateInstanceUsingPlugin() { - $this->classResolver->getInstanceFromDefinition(Argument::cetera())->shouldNotBeCalled(); - - $plugin = $this->prophesize(PluginWithFormsInterface::class)->willImplement(PluginFormInterface::class); - $plugin->hasFormClass('configure')->willReturn(TRUE); - $plugin->getFormClass('configure')->willReturn(get_class($plugin->reveal())); - - $form_object = $this->manager->createInstance($plugin->reveal(), 'configure'); - $this->assertSame($plugin->reveal(), $form_object); - } - - /** - * @covers ::createInstance - */ - public function testCreateInstanceUsingPluginWithSlashes() { - $this->classResolver->getInstanceFromDefinition(Argument::cetera())->shouldNotBeCalled(); - - $plugin = $this->prophesize(PluginWithFormsInterface::class)->willImplement(PluginFormInterface::class); - $plugin->hasFormClass('configure')->willReturn(TRUE); - $plugin->getFormClass('configure')->willReturn('\\' . get_class($plugin->reveal())); - - $form_object = $this->manager->createInstance($plugin->reveal(), 'configure'); - $this->assertSame($plugin->reveal(), $form_object); - } - - /** - * @covers ::createInstance - */ - public function testCreateInstanceDefaultFallback() { - $this->classResolver->getInstanceFromDefinition(Argument::cetera())->shouldNotBeCalled(); - - $plugin = $this->prophesize(PluginWithFormsInterface::class)->willImplement(PluginFormInterface::class); - $plugin->hasFormClass('missing')->willReturn(FALSE); - $plugin->hasFormClass('fallback')->willReturn(TRUE); - $plugin->getFormClass('fallback')->willReturn(get_class($plugin->reveal())); - - $form_object = $this->manager->createInstance($plugin->reveal(), 'missing', 'fallback'); - $this->assertSame($plugin->reveal(), $form_object); - } - - /** - * @covers ::createInstance - */ - public function testCreateInstancePluginAware() { - $plugin_form = $this->prophesize(PluginFormInterface::class)->willImplement(PluginAwareInterface::class); - - $expected = $plugin_form->reveal(); - - $this->classResolver->getInstanceFromDefinition(get_class($expected))->willReturn($expected); - - $plugin = $this->prophesize(PluginWithFormsInterface::class); - $plugin->hasFormClass('operation_aware')->willReturn(TRUE); - $plugin->getFormClass('operation_aware')->willReturn(get_class($expected)); - - $plugin_form->setPlugin($plugin->reveal())->shouldBeCalled(); - - $form_object = $this->manager->createInstance($plugin->reveal(), 'operation_aware'); - $this->assertSame($expected, $form_object); - } - - /** - * @covers ::createInstance - */ - public function testCreateInstanceDefinitionException() { - $this->setExpectedException(InvalidPluginDefinitionException::class, 'The "the_plugin_id" plugin did not specify a "anything" form class'); - - $plugin = $this->prophesize(PluginWithFormsInterface::class); - $plugin->getPluginId()->willReturn('the_plugin_id'); - $plugin->hasFormClass('anything')->willReturn(FALSE); - - $form_object = $this->manager->createInstance($plugin->reveal(), 'anything'); - $this->assertSame(NULL, $form_object); - } - - /** - * @covers ::createInstance - */ - public function testCreateInstanceInvalidException() { - $this->setExpectedException(InvalidPluginDefinitionException::class, 'The "the_plugin_id" plugin did not specify a valid "invalid" form class, must implement \Drupal\Core\Plugin\PluginFormInterface'); - - $expected = new \stdClass(); - $this->classResolver->getInstanceFromDefinition(get_class($expected))->willReturn($expected); - - $plugin = $this->prophesize(PluginWithFormsInterface::class); - $plugin->getPluginId()->willReturn('the_plugin_id'); - $plugin->hasFormClass('invalid')->willReturn(TRUE); - $plugin->getFormClass('invalid')->willReturn(get_class($expected)); - - $form_object = $this->manager->createInstance($plugin->reveal(), 'invalid'); - $this->assertSame(NULL, $form_object); - } - -}