You are here

public function PanelsIPEBlockPluginForm::submitPreview in Panels 8.4

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

Previews our current Block configuration.

Parameters

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

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

Return value

array $form The form structure.

File

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

Class

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

Namespace

Drupal\panels_ipe\Form

Code

public function submitPreview(array &$form, FormStateInterface $form_state) {

  // Return early if there are any errors.
  if ($form_state
    ->hasAnyErrors()) {
    return $form;
  }

  // Get the Block instance.
  $block_instance = $this
    ->getBlockInstance($form_state);

  // Submit the block configuration form.
  $this
    ->submitBlock($block_instance, $form, $form_state);

  // Gather a render array for the block.
  $build = $this
    ->buildBlockInstance($block_instance, $this->panelsDisplay);

  // Replace any nested form tags from the render array.
  $build['content']['#post_render'][] = function ($html, array $elements) {
    $search = [
      '<form',
      '</form>',
    ];
    $replace = [
      '<div',
      '</div>',
    ];
    return str_replace($search, $replace, $html);
  };

  // Add the preview to the backside of the card and inform JS that we need to
  // be flipped.
  $form['flipper']['back']['preview'] = $build;

  // Add a cleafix element to the end of the preview. This prevents overlaps
  // with nested float elements.
  $build['clearfix'] = [
    '#markup' => '<div class="clearfix"></div>',
  ];
  $form['#attached']['drupalSettings']['panels_ipe']['toggle_preview'] = TRUE;
  return $form;
}