You are here

protected function ComponentFormBase::buildComponentForm in Layout Paragraphs 2.0.x

Builds a component (paragraph) edit form.

Parameters

array $form: The form array.

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

2 calls to ComponentFormBase::buildComponentForm()
EditComponentForm::buildForm in src/Form/EditComponentForm.php
Form constructor.
InsertComponentForm::buildForm in src/Form/InsertComponentForm.php

File

src/Form/ComponentFormBase.php, line 133

Class

ComponentFormBase
Class LayoutParagraphsComponentFormBase.

Namespace

Drupal\layout_paragraphs\Form

Code

protected function buildComponentForm(array $form, FormStateInterface $form_state) {
  $this
    ->initFormLangcodes($form_state);
  $display = EntityFormDisplay::collectRenderDisplay($this->paragraph, 'default');
  $display
    ->buildForm($this->paragraph, $form, $form_state);
  $this->paragraphType = $this->paragraph
    ->getParagraphType();
  $lp_config = $this
    ->config('layout_paragraphs.settings');
  $form += [
    '#title' => $this
      ->formTitle(),
    '#paragraph' => $this->paragraph,
    '#display' => $display,
    '#tree' => TRUE,
    '#after_build' => [
      [
        $this,
        'afterBuild',
      ],
    ],
    'actions' => [
      '#weight' => 100,
      '#type' => 'actions',
      'submit' => [
        '#type' => 'submit',
        '#value' => $this
          ->t('Save'),
        '#ajax' => [
          'callback' => '::ajaxSubmit',
          'progress' => 'none',
        ],
        '#attributes' => [
          'class' => [
            'lpb-btn--save',
          ],
          'data-disable-refocus' => 'true',
        ],
      ],
      'cancel' => [
        '#type' => 'button',
        '#value' => $this
          ->t('Cancel'),
        '#ajax' => [
          'callback' => '::cancel',
          'progress' => 'none',
        ],
        '#attributes' => [
          'class' => [
            'dialog-cancel',
            'lpb-btn--cancel',
          ],
        ],
      ],
    ],
  ];
  if ($this->paragraphType
    ->hasEnabledBehaviorPlugin('layout_paragraphs')) {
    $form['layout_paragraphs'] = [
      '#process' => [
        [
          $this,
          'layoutParagraphsBehaviorForm',
        ],
      ],
    ];
  }
  if (count($this
    ->getEnabledBehaviorPlugins())) {
    $form['behavior_plugins'] = [
      '#weight' => $lp_config
        ->get('paragraph_behaviors_position') ?? -99,
      '#type' => 'details',
      '#title' => $lp_config
        ->get('paragraph_behaviors_label') ?? $this
        ->t('Behaviors'),
      '#process' => [
        [
          $this,
          'behaviorPluginsForm',
        ],
      ],
    ];
  }

  // Support for Field Group module based on Paragraphs module.
  // @todo Remove as part of https://www.drupal.org/node/2640056
  if ($this->moduleHandler
    ->moduleExists('field_group')) {
    $context = [
      'entity_type' => $this->paragraph
        ->getEntityTypeId(),
      'bundle' => $this->paragraph
        ->bundle(),
      'entity' => $this->paragraph,
      'context' => 'form',
      'display_context' => 'form',
      'mode' => $display
        ->getMode(),
    ];

    // phpcs:ignore
    field_group_attach_groups($form, $context);
    if (method_exists(FormatterHelper::class, 'formProcess')) {
      $form['#process'][] = [
        FormatterHelper::class,
        'formProcess',
      ];
    }
    elseif (function_exists('field_group_form_pre_render')) {
      $form['#pre_render'][] = 'field_group_form_pre_render';
    }
    elseif (function_exists('field_group_form_process')) {
      $form['#process'][] = 'field_group_form_process';
    }
  }
  return $form;
}