You are here

protected function FormEntityHelperTrait::getSectionFromFormState in Block Style Plugins 8.2

Get the Section config entity from the form state.

@todo This can be further simplified once the following issue is committed https://www.drupal.org/project/drupal/issues/3044117.

Parameters

\Drupal\Core\Form\FormStateInterface $form_state: The current form state.

Return value

\Drupal\layout_builder\Section|null The entity for the current layout builder Section if it exists.

3 calls to FormEntityHelperTrait::getSectionFromFormState()
FormEntityHelperTrait::getEntityFromFormState in src/FormEntityHelperTrait.php
Get the config entity of the entity being styled from the form state.
SectionFormAlter::alterForm in src/SectionFormAlter.php
Alter a form and set Layout Builder Section configuration.
SectionFormAlter::submitForm in src/SectionFormAlter.php
Submit the section form and save configuration.

File

src/FormEntityHelperTrait.php, line 151

Class

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

Namespace

Drupal\block_style_plugins

Code

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;
}