You are here

public function BlockStyleForm::buildForm in Block Style Plugins 8.2

Form constructor.

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 The form structure.

Overrides FormInterface::buildForm

File

src/Form/BlockStyleForm.php, line 108

Class

BlockStyleForm
Provides a form for applying styles to a block.

Namespace

Drupal\block_style_plugins\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL, $delta = NULL, $uuid = NULL) {
  $this->sectionStorage = $section_storage;
  $this->delta = $delta;
  $this->uuid = $uuid;
  $component = $section_storage
    ->getSection($delta)
    ->getComponent($uuid);
  $block_styles = $component
    ->getThirdPartySettings('block_style_plugins');

  // Get the component/block ID and then replace it with a block_content_type
  // if this is a reusable "block_content" block.
  $block_id = $component
    ->getPluginId();
  preg_match('/^block_content:(.+)/', $block_id, $matches);
  if ($matches) {
    $plugin = $this->entityRepository
      ->loadEntityByUuid('block_content', $matches[1]);
    if ($plugin) {
      $block_id = $plugin
        ->bundle();
    }
  }

  // Retrieve a list of style plugin definitions.
  $style_plugins = [];
  foreach ($this->blockStyleManager
    ->getBlockDefinitions() as $plugin_id => $definition) {

    // Check to see if this should only apply to includes or if it has been
    // excluded.
    if ($this
      ->allowStyles($block_id, $definition)) {
      $style_plugins[$plugin_id] = $definition['label'];
    }
  }

  // Create a list of applied styles with operation links.
  $items = [];
  foreach ($block_styles as $style_id => $configuration) {
    $options = [
      'attributes' => [
        'class' => [
          'use-ajax',
        ],
        'data-dialog-type' => 'dialog',
        'data-dialog-renderer' => 'off_canvas',
        'data-outside-in-edit' => TRUE,
      ],
    ];

    // Create links to edit and delete.
    $links = [
      'edit' => [
        '#title' => $this
          ->t('Edit'),
        '#type' => 'link',
        '#url' => Url::fromRoute('block_style_plugins.layout_builder.add_styles', $this
          ->getParameters($style_id), $options),
      ],
      'delete' => [
        '#title' => $this
          ->t('Delete'),
        '#type' => 'link',
        '#url' => Url::fromRoute('block_style_plugins.layout_builder.delete_styles', $this
          ->getParameters($style_id), $options),
      ],
      '#attributes' => [
        'class' => 'operations',
      ],
    ];

    // If there is no plugin for the set block style then we should only allow
    // deleting. This could be due to a plugin being removed.
    if (!isset($style_plugins[$style_id])) {
      unset($links['edit']);
    }
    $plugin_label = !empty($style_plugins[$style_id]) ? $style_plugins[$style_id] : $this
      ->t('Missing Style Plugin');
    $items[] = [
      [
        '#markup' => $plugin_label,
      ],
      $links,
    ];
  }
  if ($items) {
    $form['applied_styles_title'] = [
      '#markup' => '<h3>' . $this
        ->t('Applied Styles') . '</h3>',
    ];
    $form['applied_styles'] = [
      '#prefix' => '<div id="applied-styles">',
      '#suffix' => '</div>',
      '#theme' => 'item_list',
      '#items' => $items,
      '#empty' => $this
        ->t('No styles have been set.'),
      '#attached' => [
        'library' => [
          'block_style_plugins/off_canvas',
        ],
      ],
    ];
  }

  // Dropdown for adding styles.
  $form['block_styles'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Add a style'),
    '#options' => $style_plugins,
    '#empty_value' => '',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Add Styles'),
  ];
  if ($this
    ->isAjax()) {
    $form['actions']['submit']['#ajax']['callback'] = '::ajaxSubmit';
    $form['actions']['submit']['#ajax']['event'] = 'click';
  }
  return $form;
}