You are here

public function LayoutBuilderUpdateBlockForm::buildForm in Panopoly 8.2

Builds the block form.

Parameters

array $form: An associative array containing the structure of the form.

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

\Drupal\layout_builder\SectionStorageInterface $section_storage: The section storage being configured.

int $delta: The delta of the section.

string $region: The region of the block.

string $uuid: The UUID of the block being updated.

Return value

array The form array.

Overrides UpdateBlockForm::buildForm

File

modules/panopoly/panopoly_magic/src/Form/LayoutBuilderUpdateBlockForm.php, line 18

Class

LayoutBuilderUpdateBlockForm
Enhances the update block form with live preview.

Namespace

Drupal\panopoly_magic\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL, $delta = NULL, $region = NULL, $uuid = NULL) {
  $form = parent::buildForm($form, $form_state, $section_storage, $delta, $region, $uuid);

  // Add a preview and cancel buttons.
  if ($this
    ->isAjax()) {
    $plugin_id = $section_storage
      ->getSection($delta)
      ->getComponent($uuid)
      ->getPluginId();
    list($plugin_base_id, ) = explode(':', $plugin_id);
    if ($plugin_base_id !== 'block_content') {
      $form['actions']['preview'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Preview'),
        '#attributes' => [
          'class' => [
            'panopoly-magic-live-preview',
          ],
        ],
        '#ajax' => [
          'callback' => '::ajaxSubmit',
          'disable-refocus' => TRUE,
        ],
      ];
      $form['actions']['cancel'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Cancel'),
        '#ajax' => [
          'callback' => '::ajaxSubmit',
        ],
      ];

      // Attach preview library.
      $form['#attached']['library'][] = 'panopoly_magic/preview';
    }
  }
  return $form;
}