You are here

protected function FieldLayoutEntityDisplayFormTrait::getLayout in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field_layout/src/Form/FieldLayoutEntityDisplayFormTrait.php \Drupal\field_layout\Form\FieldLayoutEntityDisplayFormTrait::getLayout()
  2. 10 core/modules/field_layout/src/Form/FieldLayoutEntityDisplayFormTrait.php \Drupal\field_layout\Form\FieldLayoutEntityDisplayFormTrait::getLayout()

Gets the layout plugin for the currently selected field layout.

Parameters

\Drupal\field_layout\Display\EntityDisplayWithLayoutInterface $entity: The current form entity.

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

Return value

\Drupal\Core\Layout\LayoutInterface The layout plugin.

3 calls to FieldLayoutEntityDisplayFormTrait::getLayout()
FieldLayoutEntityDisplayFormTrait::form in core/modules/field_layout/src/Form/FieldLayoutEntityDisplayFormTrait.php
Overrides \Drupal\field_ui\Form\EntityDisplayFormBase::form().
FieldLayoutEntityDisplayFormTrait::submitForm in core/modules/field_layout/src/Form/FieldLayoutEntityDisplayFormTrait.php
Overrides \Drupal\field_ui\Form\EntityDisplayFormBase::submitForm().
FieldLayoutEntityDisplayFormTrait::validateForm in core/modules/field_layout/src/Form/FieldLayoutEntityDisplayFormTrait.php
Overrides \Drupal\field_ui\Form\EntityDisplayFormBase::validateForm().

File

core/modules/field_layout/src/Form/FieldLayoutEntityDisplayFormTrait.php, line 112

Class

FieldLayoutEntityDisplayFormTrait
Provides shared code for entity display forms.

Namespace

Drupal\field_layout\Form

Code

protected function getLayout(EntityDisplayWithLayoutInterface $entity, FormStateInterface $form_state) {
  if (!($layout_plugin = $form_state
    ->get('layout_plugin'))) {
    $stored_layout_id = $entity
      ->getLayoutId();

    // Use selected layout if it exists, falling back to the stored layout.
    $layout_id = $form_state
      ->getValue('field_layout', $stored_layout_id);

    // If the current layout is the stored layout, use the stored layout
    // settings. Otherwise leave the settings empty.
    $layout_settings = $layout_id === $stored_layout_id ? $entity
      ->getLayoutSettings() : [];
    $layout_plugin = $this->layoutPluginManager
      ->createInstance($layout_id, $layout_settings);
    $form_state
      ->set('layout_plugin', $layout_plugin);
  }
  return $layout_plugin;
}