You are here

trait FormEntityHelperTrait in Block Style Plugins 8.2

Provides a helper for getting information from the entity being styled.

Hierarchy

File

src/FormEntityHelperTrait.php, line 13

Namespace

Drupal\block_style_plugins
View source
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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FormEntityHelperTrait::$entityRepository protected property Instance of the Entity Repository service.
FormEntityHelperTrait::getBlockConfigEntityFromFormState protected function Get the Block config entity from the form state.
FormEntityHelperTrait::getBlockContent protected function Get the Block Content entity.
FormEntityHelperTrait::getBlockContentBundle public function Get the block content bundle type.
FormEntityHelperTrait::getEntityFromFormState protected function Get the config entity of the entity being styled from the form state.
FormEntityHelperTrait::getEntityRepository protected function Gets the Entity Repository service.
FormEntityHelperTrait::getSectionFromFormState protected function Get the Section config entity from the form state.