FormEntityHelperTrait.php in Block Style Plugins 8.2
Namespace
Drupal\block_style_pluginsFile
src/FormEntityHelperTrait.phpView source
<?php
namespace Drupal\block_style_plugins;
use Drupal\block_style_plugins\Form\ConfigureStyles;
use Drupal\Core\Form\FormStateInterface;
use Drupal\layout_builder\Form\AddBlockForm;
use Drupal\layout_builder\Form\UpdateBlockForm;
/**
* Provides a helper for getting information from the entity being styled.
*/
trait FormEntityHelperTrait {
/**
* Instance of the Entity Repository service.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* Gets the Entity Repository service.
*
* @return \Drupal\Core\Entity\EntityRepositoryInterface
* The Entity Repository service.
*/
protected function getEntityRepository() {
if (!$this->entityRepository) {
$this->entityRepository = \Drupal::service('entity.repository');
}
return $this->entityRepository;
}
/**
* Get the block content bundle type.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current form state.
*
* @return string
* Name of the Block Content bundle.
*/
public function getBlockContentBundle(FormStateInterface $form_state) {
$bundle = NULL;
// Get the current block config entity.
$block_content = $this
->getBlockContent($form_state);
if ($block_content) {
$bundle = $block_content
->bundle();
}
return $bundle;
}
/**
* Get the Block Content entity.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current form state.
*
* @return \Drupal\block_content\Entity\BlockContent
* The Block Content entity.
*/
protected function getBlockContent(FormStateInterface $form_state) {
// Get the current block config entity.
$entity = $this
->getBlockConfigEntityFromFormState($form_state);
/** @var \Drupal\Core\Block\BlockPluginInterface */
$block_plugin = $entity
->getPlugin();
$base_id = $block_plugin
->getBaseId();
$uuid = $block_plugin
->getDerivativeId();
$block_content = NULL;
if ($base_id == 'block_content') {
$block_content = $this
->getEntityRepository()
->loadEntityByUuid('block_content', $uuid);
}
return $block_content;
}
/**
* Get the config entity of the entity being styled from the form state.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current form state.
*
* @return \Drupal\block\Entity\Block|\Drupal\layout_builder\SectionComponent|\Drupal\layout_builder\Section
* The entity for the current Block, SectionComponent, or Section.
*/
protected function getEntityFromFormState(FormStateInterface $form_state) {
// Try to get a block/component entity.
$entity = $this
->getBlockConfigEntityFromFormState($form_state);
// Try to get a section entity if this is not a block.
if (!$entity) {
$entity = $this
->getSectionFromFormState($form_state);
}
return $entity;
}
/**
* Get the Block config entity from the form state.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current form state.
*
* @return \Drupal\block\Entity\Block|\Drupal\layout_builder\SectionComponent
* The entity for the current Block or layout builder SectionComponent.
*/
protected function getBlockConfigEntityFromFormState(FormStateInterface $form_state) {
/** @var Drupal\block\BlockForm $form_object */
$form_object = $form_state
->getFormObject();
$entity = NULL;
// Get the current block config entity.
if ($form_object instanceof UpdateBlockForm) {
/** @var \Drupal\layout_builder\SectionComponent $entity */
$entity = $form_object
->getCurrentComponent();
}
elseif ($form_object instanceof AddBlockForm) {
/** @var \Drupal\layout_builder\SectionComponent $entity */
$entity = $form_state
->get('layout_builder__component');
}
elseif ($form_object instanceof ConfigureStyles) {
/** @var \Drupal\block_style_plugins\Form\ConfigureStyles $entity */
$entity = $form_object
->getComponent();
}
elseif (method_exists($form_object, 'getEntity')) {
/** @var \Drupal\block\Entity\Block $entity */
$entity = $form_object
->getEntity();
}
return $entity;
}
/**
* Get the Section config entity from the form state.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current form state.
*
* @return \Drupal\layout_builder\Section|null
* The entity for the current layout builder Section if it exists.
*
* @todo This can be further simplified once the following issue is committed
* https://www.drupal.org/project/drupal/issues/3044117.
*/
protected function getSectionFromFormState(FormStateInterface $form_state) {
/** @var \Drupal\layout_builder\Form\ConfigureSectionForm $form_object */
$form_object = $form_state
->getFormObject();
// If using the https://www.drupal.org/project/drupal/issues/3044117 patch,
// then this can use the built in method.
if (method_exists($form_object, 'getCurrentSection')) {
return $form_object
->getCurrentSection();
}
// Sad hack to try to get the current section since a getCurrentSection()
// does not exist.
$build_info = $form_state
->getBuildInfo();
$section_storage = $form_object
->getSectionStorage();
$sections = $section_storage
->getSections();
$delta = (int) $build_info['args'][1];
return array_key_exists($delta, $sections) ? $sections[$delta] : NULL;
}
}
Traits
Name![]() |
Description |
---|---|
FormEntityHelperTrait | Provides a helper for getting information from the entity being styled. |