You are here

class PanelsIPEBlockPluginForm 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

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

Unlike most forms, this never saves a block plugin instance or persists it from state to state. This is only for the initial addition to the Layout.

Hierarchy

Expanded class hierarchy of PanelsIPEBlockPluginForm

File

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

Namespace

Drupal\panels_ipe\Form
View source
class PanelsIPEBlockPluginForm extends FormBase {
  use ContextAwarePluginAssignmentTrait;
  use PanelsIPEBlockRendererTrait;

  /**
   * @var \Drupal\Component\Plugin\PluginManagerInterface $blockManager
   */
  protected $blockManager;

  /**
   * @var \Drupal\Core\Render\RendererInterface $renderer
   */
  protected $renderer;

  /**
   * @var \Drupal\user\SharedTempStore
   */
  protected $tempStore;

  /**
   * The Panels storage manager.
   *
   * @var \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant
   */
  protected $panelsDisplay;

  /**
   * Constructs a new PanelsIPEBlockPluginForm.
   *
   * @param \Drupal\Component\Plugin\PluginManagerInterface $block_manager
   * @param \Drupal\Core\Plugin\Context\ContextHandlerInterface $context_handler
   * @param \Drupal\Core\Render\RendererInterface $renderer
   * @param \Drupal\user\SharedTempStoreFactory $temp_store_factory
   */
  public function __construct(PluginManagerInterface $block_manager, ContextHandlerInterface $context_handler, RendererInterface $renderer, SharedTempStoreFactory $temp_store_factory) {
    $this->blockManager = $block_manager;
    $this->contextHandler = $context_handler;
    $this->renderer = $renderer;
    $this->tempStore = $temp_store_factory
      ->get('panels_ipe');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('plugin.manager.block'), $container
      ->get('context.handler'), $container
      ->get('renderer'), $container
      ->get('user.shared_tempstore'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'panels_ipe_block_plugin_form';
  }

  /**
   * Builds a form that constructs a unsaved instance of a Block for the IPE.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   * @param string $plugin_id
   *   The requested Block Plugin ID.
   * @param \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $panels_display
   *   The current PageVariant ID.
   * @param string $uuid
   *   An optional Block UUID, if this is an existing Block.
   *
   * @return array
   *   The form structure.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $block_instance = $this
      ->getBlockInstance($form_state);

    // Validate the block configuration form.
    $block_form_state = (new FormState())
      ->setValues($form_state
      ->getValue('settings'));
    $block_instance
      ->validateConfigurationForm($form, $block_form_state);

    // Update the original form values.
    $form_state
      ->setValue('settings', $block_form_state
      ->getValues());
  }

  /**
   * Executes the block plugin's submit handlers.
   *
   * @param \Drupal\Core\Block\BlockPluginInterface $block_instance
   *   The block instance.
   * @param array $form
   *   The full form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The full form state.
   */
  protected function submitBlock(BlockPluginInterface $block_instance, array $form, FormStateInterface $form_state) {
    $block_form_state = (new FormState())
      ->setValues($form_state
      ->getValue('settings'));
    $block_instance
      ->submitConfigurationForm($form['flipper']['front']['settings'], $block_form_state);
    if ($block_instance instanceof ContextAwarePluginInterface) {
      $block_instance
        ->setContextMapping($block_form_state
        ->getValue('context_mapping', []));
    }

    // Update the original form values.
    $form_state
      ->setValue('settings', $block_form_state
      ->getValues());
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

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

    // If a temporary configuration for this variant exists, use it.
    $temp_store_key = $this->panelsDisplay
      ->id();
    if ($variant_config = $this->tempStore
      ->get($temp_store_key)) {
      $this->panelsDisplay
        ->setConfiguration($variant_config);
    }
    $block_instance = $this
      ->getBlockInstance($form_state);

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

    // Set the block region appropriately.
    $block_config = $block_instance
      ->getConfiguration();
    $block_config['region'] = $form_state
      ->getValue(array(
      'settings',
      'region',
    ));

    // Determine if we need to update or add this block.
    if ($uuid = $form_state
      ->getValue('uuid')) {
      $this->panelsDisplay
        ->updateBlock($uuid, $block_config);
    }
    else {
      $uuid = $this->panelsDisplay
        ->addBlock($block_config);
    }

    // Set the tempstore value.
    $this->tempStore
      ->set($this->panelsDisplay
      ->id(), $this->panelsDisplay
      ->getConfiguration());

    // Assemble data required for our App.
    $build = $this
      ->buildBlockInstance($block_instance, $this->panelsDisplay);

    // Bubble Block attributes to fix bugs with the Quickedit and Contextual
    // modules.
    $this
      ->bubbleBlockAttributes($build);

    // Add our data attribute for the Backbone app.
    $build['#attributes']['data-block-id'] = $uuid;
    $plugin_definition = $block_instance
      ->getPluginDefinition();
    $block_model = [
      'uuid' => $uuid,
      'label' => $block_instance
        ->label(),
      'id' => $block_instance
        ->getPluginId(),
      'region' => $block_config['region'],
      'provider' => $block_config['provider'],
      'plugin_id' => $plugin_definition['id'],
      'html' => $this->renderer
        ->render($build),
    ];
    $form['build'] = $build;

    // Add Block metadata and HTML as a drupalSetting.
    $form['#attached']['drupalSettings']['panels_ipe']['updated_block'] = $block_model;
    return $form;
  }

  /**
   * Previews our current Block configuration.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @return array $form
   *   The form structure.
   */
  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;
  }

  /**
   * Loads or creates a Block Plugin instance suitable for rendering or testing.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @return \Drupal\Core\Block\BlockPluginInterface
   *   The Block Plugin instance.
   */
  protected function getBlockInstance(FormStateInterface $form_state) {

    // If a UUID is provided, the Block should already exist.
    if ($uuid = $form_state
      ->getValue('uuid')) {

      // If a temporary configuration for this variant exists, use it.
      $temp_store_key = $this->panelsDisplay
        ->id();
      if ($variant_config = $this->tempStore
        ->get($temp_store_key)) {
        $this->panelsDisplay
          ->setConfiguration($variant_config);
      }

      // Load the existing Block instance.
      $block_instance = $this->panelsDisplay
        ->getBlock($uuid);
    }
    else {

      // Create an instance of this Block plugin.

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

}

Members

Namesort descending Modifiers Type Description Overrides
ContextAwarePluginAssignmentTrait::addContextAssignmentElement protected function Builds a form element for assigning a context to a given slot.
ContextAwarePluginAssignmentTrait::contextHandler protected function Wraps the context handler.
ContextAwarePluginAssignmentTrait::t abstract protected function Ensures the t() method is available.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PanelsIPEBlockPluginForm::$blockManager protected property
PanelsIPEBlockPluginForm::$panelsDisplay protected property The Panels storage manager.
PanelsIPEBlockPluginForm::$renderer protected property
PanelsIPEBlockPluginForm::$tempStore protected property
PanelsIPEBlockPluginForm::buildForm public function Builds a form that constructs a unsaved instance of a Block for the IPE. Overrides FormInterface::buildForm
PanelsIPEBlockPluginForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
PanelsIPEBlockPluginForm::getBlockInstance protected function Loads or creates a Block Plugin instance suitable for rendering or testing.
PanelsIPEBlockPluginForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
PanelsIPEBlockPluginForm::submitBlock protected function Executes the block plugin's submit handlers.
PanelsIPEBlockPluginForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
PanelsIPEBlockPluginForm::submitPreview public function Previews our current Block configuration.
PanelsIPEBlockPluginForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
PanelsIPEBlockPluginForm::__construct public function Constructs a new PanelsIPEBlockPluginForm.
PanelsIPEBlockRendererTrait::$contextHandler protected property
PanelsIPEBlockRendererTrait::bubbleBlockAttributes protected function Bubble block attributes up if possible. This allows modules like Quickedit to function.
PanelsIPEBlockRendererTrait::buildBlockInstance protected function Compiles a render array for the given Block instance based on the form.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.