You are here

public function LayoutParagraphsBehavior::buildBehaviorForm in Layout Paragraphs 2.0.x

File

src/Plugin/paragraphs/Behavior/LayoutParagraphsBehavior.php, line 114

Class

LayoutParagraphsBehavior
Provides a way to define grid based layouts.

Namespace

Drupal\layout_paragraphs\Plugin\paragraphs\Behavior

Code

public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
  $layout_paragraphs_section = new LayoutParagraphsSection($paragraph);
  $layout_settings = $layout_paragraphs_section
    ->getSetting('config');
  $available_layouts = $this->configuration['available_layouts'];
  $path = array_merge($form['#parents'], [
    'layout',
  ]);
  $input_layout_id = NestedArray::getValue($form_state
    ->getUserInput(), $path);
  $layout_id = $input_layout_id ?? $layout_paragraphs_section
    ->getLayoutId();
  $layout_id = Html::escape($layout_id);
  $default_value = !empty($layout_id) ? $layout_id : key($available_layouts);

  // @todo - Throw an error if plugin instance cannot be loaded.
  $plugin_instance = $this->layoutPluginManager
    ->createInstance($default_value, $layout_settings);
  $plugin_form = $this
    ->getLayoutPluginForm($plugin_instance);
  $wrapper_id = Html::getId(implode('-', array_merge($form['#parents'], [
    'layout-options',
  ])));
  $form['layout'] = [
    '#title' => $this
      ->t('Choose a layout:'),
    '#type' => 'layout_select',
    '#options' => $available_layouts,
    '#default_value' => $default_value,
    '#ajax' => [
      'wrapper' => $wrapper_id,
      'callback' => [
        $this,
        'ajaxUpdateOptions',
      ],
      'progress' => [
        'type' => 'none',
        'message' => NULL,
      ],
    ],
    '#weight' => 0,
  ];
  if ($plugin_form) {
    $form['config'] = [
      '#type' => 'details',
      '#id' => $wrapper_id,
      '#title' => $this
        ->t('Layout Options'),
      '#weight' => 10,
    ];
    $form['config'] += $plugin_form
      ->buildConfigurationForm([], $form_state);
  }
  return $form;
}