diff --git a/core/core.services.yml b/core/core.services.yml index d2aa505..39b0a04 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -569,8 +569,8 @@ services: entity.autocomplete_matcher: class: Drupal\Core\Entity\EntityAutocompleteMatcher arguments: ['@plugin.manager.entity_reference_selection'] - block_form.manager: - class: Drupal\Core\Block\BlockFormManager + plugin_form.manager: + class: Drupal\Core\Plugin\PluginFormManager arguments: ['@class_resolver'] plugin.manager.entity_reference_selection: class: Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager diff --git a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php index 06365fa..8e9e77a 100644 --- a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php +++ b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php @@ -239,9 +239,20 @@ public function useCaches($use_caches = FALSE) { * 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['form']['default']) && isset($definition['class']) && is_subclass_of($definition['class'], PluginFormInterface::class)) { + $definition['form']['default'] = $definition['class']; + } } /** diff --git a/core/lib/Drupal/Core/Block/BlockFormManager.php b/core/lib/Drupal/Core/Plugin/PluginFormManager.php similarity index 84% rename from core/lib/Drupal/Core/Block/BlockFormManager.php rename to core/lib/Drupal/Core/Plugin/PluginFormManager.php index f866706..dec70ba 100644 --- a/core/lib/Drupal/Core/Block/BlockFormManager.php +++ b/core/lib/Drupal/Core/Plugin/PluginFormManager.php @@ -1,17 +1,16 @@ getPluginDefinition(); if (!isset($definition['form'][$operation])) { // Use the default form class if no form is specified for this operation. - if (isset($definition['form']['default'])) { - $operation = 'default'; + if ($fallback_operation && isset($definition['form'][$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)); diff --git a/core/lib/Drupal/Core/Block/BlockFormManagerInterface.php b/core/lib/Drupal/Core/Plugin/PluginFormManagerInterface.php similarity index 59% rename from core/lib/Drupal/Core/Block/BlockFormManagerInterface.php rename to core/lib/Drupal/Core/Plugin/PluginFormManagerInterface.php index 2135128..6038d61 100644 --- a/core/lib/Drupal/Core/Block/BlockFormManagerInterface.php +++ b/core/lib/Drupal/Core/Plugin/PluginFormManagerInterface.php @@ -1,13 +1,13 @@ storage = $entity_manager->getStorage('block'); $this->manager = $manager; $this->contextRepository = $context_repository; $this->language = $language; $this->themeHandler = $theme_handler; - $this->blockFormManager = $block_form_manager; + $this->pluginFormManager = $plugin_form_manager; } /** @@ -112,7 +112,7 @@ public static function create(ContainerInterface $container) { $container->get('context.repository'), $container->get('language_manager'), $container->get('theme_handler'), - $container->get('block_form.manager') + $container->get('plugin_form.manager') ); } @@ -435,7 +435,7 @@ public function getUniqueMachineName(BlockInterface $block) { * The plugin form for the block. */ protected function getPluginForm(BlockPluginInterface $block) { - return $this->blockFormManager->getFormObject($block, $this->operation); + return $this->pluginFormManager->getFormObject($block, 'default'); } } diff --git a/core/modules/block/tests/modules/block_test/src/Form/SecondaryBlockForm.php b/core/modules/block/tests/modules/block_test/src/Form/SecondaryBlockForm.php index 3095963..c5aac97 100644 --- a/core/modules/block/tests/modules/block_test/src/Form/SecondaryBlockForm.php +++ b/core/modules/block/tests/modules/block_test/src/Form/SecondaryBlockForm.php @@ -7,7 +7,7 @@ use Drupal\Core\Plugin\PluginFormInterface; /** - * @todo. + * Provides a form that is used as a secondary form for a block. */ class SecondaryBlockForm implements PluginFormInterface, OperationAwareFormInterface { 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 index ff5ced3..01a8fe2 100644 --- 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 @@ -5,7 +5,7 @@ use Drupal\Core\Block\BlockBase; /** - * @todo. + * Provides a block with multiple forms. * * @Block( * id = "test_multiple_forms_block", diff --git a/core/modules/block/tests/src/Unit/BlockFormTest.php b/core/modules/block/tests/src/Unit/BlockFormTest.php index a27014a..07b3780 100644 --- a/core/modules/block/tests/src/Unit/BlockFormTest.php +++ b/core/modules/block/tests/src/Unit/BlockFormTest.php @@ -3,7 +3,7 @@ namespace Drupal\Tests\block\Unit; use Drupal\block\BlockForm; -use Drupal\Core\Block\BlockFormManagerInterface; +use Drupal\Core\Plugin\PluginFormManagerInterface; use Drupal\Tests\UnitTestCase; /** @@ -56,11 +56,11 @@ class BlockFormTest extends UnitTestCase { protected $contextRepository; /** - * The block form manager. + * The plugin form manager. * - * @var \Drupal\Core\Block\BlockFormManagerInterface|\Prophecy\Prophecy\ProphecyInterface + * @var \Drupal\Core\Plugin\PluginFormManagerInterface|\Prophecy\Prophecy\ProphecyInterface */ - protected $blockFormManager; + protected $pluginFormManager; /** * {@inheritdoc} @@ -79,7 +79,7 @@ protected function setUp() { ->method('getStorage') ->will($this->returnValue($this->storage)); - $this->blockFormManager = $this->prophesize(BlockFormManagerInterface::class); + $this->pluginFormManager = $this->prophesize(PluginFormManagerInterface::class); } /** @@ -108,7 +108,7 @@ public function testGetUniqueMachineName() { ->method('getQuery') ->will($this->returnValue($query)); - $block_form_controller = new BlockForm($this->entityManager, $this->conditionManager, $this->contextRepository, $this->language, $this->themeHandler, $this->blockFormManager->reveal()); + $block_form_controller = new BlockForm($this->entityManager, $this->conditionManager, $this->contextRepository, $this->language, $this->themeHandler, $this->pluginFormManager->reveal()); // Ensure that the block with just one other instance gets the next available // name suggestion. diff --git a/core/modules/outside_in/src/Block/BlockEntityOffCanvasForm.php b/core/modules/outside_in/src/Block/BlockEntityOffCanvasForm.php index a49df93..a77a6a6 100644 --- a/core/modules/outside_in/src/Block/BlockEntityOffCanvasForm.php +++ b/core/modules/outside_in/src/Block/BlockEntityOffCanvasForm.php @@ -3,6 +3,7 @@ namespace Drupal\outside_in\Block; use Drupal\block\BlockForm; +use Drupal\Core\Block\BlockPluginInterface; use Drupal\Core\Form\FormStateInterface; /** @@ -43,4 +44,11 @@ protected function submitVisibility(array $form, FormStateInterface $form_state) // Intentionally empty. } + /** + * {@inheritdoc} + */ + protected function getPluginForm(BlockPluginInterface $block) { + return $this->pluginFormManager->getFormObject($block, 'offcanvas', 'default'); + } + } diff --git a/core/tests/Drupal/KernelTests/Core/Block/MultipleBlockFormTest.php b/core/tests/Drupal/KernelTests/Core/Block/MultipleBlockFormTest.php index ad13628..117db9d 100644 --- a/core/tests/Drupal/KernelTests/Core/Block/MultipleBlockFormTest.php +++ b/core/tests/Drupal/KernelTests/Core/Block/MultipleBlockFormTest.php @@ -13,7 +13,7 @@ class MultipleBlockFormTest extends KernelTestBase { /** - * @var array + * {@inheritdoc} */ public static $modules = ['system', 'block', 'block_test']; @@ -23,8 +23,8 @@ class MultipleBlockFormTest extends KernelTestBase { public function testMultipleForms() { $block = \Drupal::service('plugin.manager.block')->createInstance('test_multiple_forms_block'); - $form_object1 = \Drupal::service('block_form.manager')->getFormObject($block, 'default'); - $form_object2 = \Drupal::service('block_form.manager')->getFormObject($block, 'secondary'); + $form_object1 = \Drupal::service('plugin_form.manager')->getFormObject($block, 'default'); + $form_object2 = \Drupal::service('plugin_form.manager')->getFormObject($block, 'secondary'); // Assert that the block itself is used for the default form. $this->assertSame($block, $form_object1); diff --git a/core/tests/Drupal/Tests/Core/Block/BlockFormManagerTest.php b/core/tests/Drupal/Tests/Core/Plugin/PluginFormManagerTest.php similarity index 91% rename from core/tests/Drupal/Tests/Core/Block/BlockFormManagerTest.php rename to core/tests/Drupal/Tests/Core/Plugin/PluginFormManagerTest.php index 7ea0e06..193899c 100644 --- a/core/tests/Drupal/Tests/Core/Block/BlockFormManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Plugin/PluginFormManagerTest.php @@ -1,21 +1,21 @@ classResolver = $this->prophesize(ClassResolverInterface::class); - $this->manager = new BlockFormManager($this->classResolver->reveal()); + $this->manager = new PluginFormManager($this->classResolver->reveal()); } /** @@ -87,11 +87,11 @@ public function testGetFormObjectDefaultFallback() { $plugin = $this->prophesize(PluginInspectionInterface::class)->willImplement(PluginFormInterface::class); $plugin->getPluginDefinition()->willReturn([ 'form' => [ - 'default' => get_class($plugin->reveal()), + 'fallback' => get_class($plugin->reveal()), ], ]); - $form_object = $this->manager->getFormObject($plugin->reveal(), 'missing'); + $form_object = $this->manager->getFormObject($plugin->reveal(), 'missing', 'fallback'); $this->assertSame($plugin->reveal(), $form_object); }