You are here

public function PanelsIPEBlockPluginForm::buildForm in Panels 8.3

Same name and namespace in other branches
  1. 8.4 panels_ipe/src/Form/PanelsIPEBlockPluginForm.php \Drupal\panels_ipe\Form\PanelsIPEBlockPluginForm::buildForm()

Builds a form that constructs a unsaved instance of a Block for the IPE.

Parameters

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

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

string $plugin_id: The requested Block Plugin ID.

\Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $panels_display: The current PageVariant ID.

string $uuid: An optional Block UUID, if this is an existing Block.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

panels_ipe/src/Form/PanelsIPEBlockPluginForm.php, line 109

Class

PanelsIPEBlockPluginForm
Provides a form for adding a block plugin temporarily using AJAX.

Namespace

Drupal\panels_ipe\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $plugin_id = NULL, PanelsDisplayVariant $panels_display = NULL, $uuid = NULL) {

  // We require these default arguments.
  if (!$plugin_id || !$panels_display) {
    return FALSE;
  }

  // Save the panels display for later.
  $this->panelsDisplay = $panels_display;

  // Grab the current layout's regions.
  $regions = $panels_display
    ->getRegionNames();

  // If $uuid is present, a block should exist.
  if ($uuid) {

    /** @var \Drupal\Core\Block\BlockBase $block_instance */
    $block_instance = $panels_display
      ->getBlock($uuid);
  }
  else {

    // Create an instance of this Block plugin.

    /** @var \Drupal\Core\Block\BlockBase $block_instance */
    $block_instance = $this->blockManager
      ->createInstance($plugin_id);
  }

  // Determine the current region.
  $block_config = $block_instance
    ->getConfiguration();
  if (isset($block_config['region']) && isset($regions[$block_config['region']])) {
    $region = $block_config['region'];
  }
  else {
    $region = reset($regions);
  }

  // Some Block Plugins rely on the block_theme value to load theme settings.
  // @see \Drupal\system\Plugin\Block\SystemBrandingBlock::blockForm().
  $form_state
    ->set('block_theme', $this
    ->config('system.theme')
    ->get('default'));

  // Wrap the form so that our AJAX submit can replace its contents.
  $form['#prefix'] = '<div id="panels-ipe-block-plugin-form-wrapper">';
  $form['#suffix'] = '</div>';

  // Add our various card wrappers.
  $form['flipper'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => 'flipper',
    ],
  ];
  $form['flipper']['front'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => 'front',
    ],
  ];
  $form['flipper']['back'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => 'back',
    ],
  ];
  $form['#attributes']['class'][] = 'flip-container';

  // Get the base configuration form for this block.
  $form['flipper']['front']['settings'] = $block_instance
    ->buildConfigurationForm([], $form_state);
  $form['flipper']['front']['settings']['context_mapping'] = $this
    ->addContextAssignmentElement($block_instance, $this->panelsDisplay
    ->getContexts());
  $form['flipper']['front']['settings']['#tree'] = TRUE;

  // Add the block ID, variant ID to the form as values.
  $form['plugin_id'] = [
    '#type' => 'value',
    '#value' => $plugin_id,
  ];
  $form['variant_id'] = [
    '#type' => 'value',
    '#value' => $panels_display
      ->id(),
  ];
  $form['uuid'] = [
    '#type' => 'value',
    '#value' => $uuid,
  ];

  // Add a select list for region assignment.
  $form['flipper']['front']['settings']['region'] = [
    '#title' => $this
      ->t('Region'),
    '#type' => 'select',
    '#options' => $regions,
    '#required' => TRUE,
    '#default_value' => $region,
  ];

  // Add an add button, which is only used by our App.
  $form['submit'] = [
    '#type' => 'button',
    '#value' => $uuid ? $this
      ->t('Update') : $this
      ->t('Add'),
    '#ajax' => [
      'callback' => '::submitForm',
      'wrapper' => 'panels-ipe-block-plugin-form-wrapper',
      'method' => 'replace',
      'progress' => [
        'type' => 'throbber',
        'message' => '',
      ],
    ],
  ];

  // Add a preview button.
  $form['preview'] = [
    '#type' => 'button',
    '#value' => $this
      ->t('Toggle Preview'),
    '#ajax' => [
      'callback' => '::submitPreview',
      'wrapper' => 'panels-ipe-block-plugin-form-wrapper',
      'method' => 'replace',
      'progress' => [
        'type' => 'throbber',
        'message' => '',
      ],
    ],
  ];
  return $form;
}