Commit 9ca03e71 authored by catch's avatar catch

Issue #3003666 by tim.plunkett, segovia94: Provide access to a Section or...

Issue #3003666 by tim.plunkett, segovia94: Provide access to a Section or SectionComponent from within a $form_state
parent a22498f0
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
use Drupal\Core\Block\BlockManagerInterface; use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Block\BlockPluginInterface; use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\DependencyInjection\ClassResolverInterface; use Drupal\Core\DependencyInjection\ClassResolverInterface;
use Drupal\Core\Form\BaseFormIdInterface;
use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformState; use Drupal\Core\Form\SubformState;
...@@ -27,7 +28,7 @@ ...@@ -27,7 +28,7 @@
* *
* @internal * @internal
*/ */
abstract class ConfigureBlockFormBase extends FormBase { abstract class ConfigureBlockFormBase extends FormBase implements BaseFormIdInterface {
use AjaxFormHelperTrait; use AjaxFormHelperTrait;
use ContextAwarePluginAssignmentTrait; use ContextAwarePluginAssignmentTrait;
...@@ -136,6 +137,13 @@ public static function create(ContainerInterface $container) { ...@@ -136,6 +137,13 @@ public static function create(ContainerInterface $container) {
); );
} }
/**
* {@inheritdoc}
*/
public function getBaseFormId() {
return 'layout_builder_configure_block';
}
/** /**
* Builds the form for the block. * Builds the form for the block.
* *
...@@ -242,4 +250,34 @@ protected function getPluginForm(BlockPluginInterface $block) { ...@@ -242,4 +250,34 @@ protected function getPluginForm(BlockPluginInterface $block) {
return $block; return $block;
} }
/**
* Retrieves the section storage object.
*
* @return \Drupal\layout_builder\SectionStorageInterface
* The section storage for the current form.
*/
public function getSectionStorage() {
return $this->sectionStorage;
}
/**
* Retrieves the current layout section being edited by the form.
*
* @return \Drupal\layout_builder\Section
* The current layout section.
*/
public function getCurrentSection() {
return $this->sectionStorage->getSection($this->delta);
}
/**
* Retrieves the current component being edited by the form.
*
* @return \Drupal\layout_builder\SectionComponent
* The current section component.
*/
public function getCurrentComponent() {
return $this->getCurrentSection()->getComponent($this->uuid);
}
} }
...@@ -194,4 +194,14 @@ protected function getPluginForm(LayoutInterface $layout) { ...@@ -194,4 +194,14 @@ protected function getPluginForm(LayoutInterface $layout) {
throw new \InvalidArgumentException(sprintf('The "%s" layout does not provide a configuration form', $layout->getPluginId())); throw new \InvalidArgumentException(sprintf('The "%s" layout does not provide a configuration form', $layout->getPluginId()));
} }
/**
* Retrieve the section storage property.
*
* @return \Drupal\layout_builder\SectionStorageInterface
* The section storage for the current form.
*/
public function getSectionStorage() {
return $this->sectionStorage;
}
} }
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
/** /**
* Implements hook_plugin_filter_TYPE__CONSUMER_alter(). * Implements hook_plugin_filter_TYPE__CONSUMER_alter().
...@@ -58,3 +59,24 @@ function layout_builder_test_node_view(array &$build, EntityInterface $entity, E ...@@ -58,3 +59,24 @@ function layout_builder_test_node_view(array &$build, EntityInterface $entity, E
]; ];
} }
} }
/**
* Implements hook_form_BASE_FORM_ID_alter() for layout_builder_configure_block.
*/
function layout_builder_test_form_layout_builder_configure_block_alter(&$form, FormStateInterface $form_state, $form_id) {
/** @var \Drupal\layout_builder\Form\ConfigureBlockFormBase $form_object */
$form_object = $form_state->getFormObject();
$form['layout_builder_test']['storage'] = [
'#type' => 'item',
'#title' => 'Layout Builder Storage: ' . $form_object->getSectionStorage()->getStorageId(),
];
$form['layout_builder_test']['section'] = [
'#type' => 'item',
'#title' => 'Layout Builder Section: ' . $form_object->getCurrentSection()->getLayoutId(),
];
$form['layout_builder_test']['component'] = [
'#type' => 'item',
'#title' => 'Layout Builder Component: ' . $form_object->getCurrentComponent()->getPluginId(),
];
}
...@@ -557,6 +557,32 @@ public function testDeletedView() { ...@@ -557,6 +557,32 @@ public function testDeletedView() {
$assert_session->pageTextNotContains('Test Block View'); $assert_session->pageTextNotContains('Test Block View');
} }
/**
* Tests that hook_form_alter() has access to the Layout Builder info.
*/
public function testFormAlter() {
$assert_session = $this->assertSession();
$page = $this->getSession()->getPage();
$this->drupalLogin($this->drupalCreateUser([
'configure any layout',
'administer node display',
'administer node fields',
]));
$field_ui_prefix = 'admin/structure/types/manage/bundle_with_section_field';
$this->drupalGet("$field_ui_prefix/display/default");
$page->checkField('layout[enabled]');
$page->pressButton('Save');
$page->clickLink('Manage layout');
$page->clickLink('Add Block');
$page->clickLink('Powered by Drupal');
$assert_session->pageTextContains('Layout Builder Storage: node.bundle_with_section_field.default');
$assert_session->pageTextContains('Layout Builder Section: layout_onecol');
$assert_session->pageTextContains('Layout Builder Component: system_powered_by_block');
}
/** /**
* Tests the usage of placeholders for empty blocks. * Tests the usage of placeholders for empty blocks.
* *
......
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