You are here

public function Blocks::buildConfigurationForm in Context 8.0

Same name and namespace in other branches
  1. 8.4 src/Plugin/ContextReaction/Blocks.php \Drupal\context\Plugin\ContextReaction\Blocks::buildConfigurationForm()
  2. 8 src/Plugin/ContextReaction/Blocks.php \Drupal\context\Plugin\ContextReaction\Blocks::buildConfigurationForm()

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides PluginFormInterface::buildConfigurationForm

File

src/Plugin/ContextReaction/Blocks.php, line 364

Class

Blocks
Provides a content reaction that will let you place blocks in the current themes regions.

Namespace

Drupal\context\Plugin\ContextReaction

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state, ContextInterface $context = NULL) {
  $form['#attached']['library'][] = 'block/drupal.block';
  $themes = $this->themeHandler
    ->listInfo();
  $default_theme = $this->themeHandler
    ->getDefault();

  // Select list for changing themes.
  $form['theme'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Theme'),
    '#options' => [],
    '#description' => $this
      ->t('Select the theme you want to display regions for.'),
    '#default_value' => $form_state
      ->getValue('theme', $default_theme),
    '#ajax' => [
      'url' => Url::fromRoute('context.reaction.blocks.regions', [
        'context' => $context
          ->id(),
      ]),
    ],
  ];

  // Add each theme to the theme select.
  foreach ($themes as $theme_id => $theme) {
    if ($theme_id === $default_theme) {
      $form['theme']['#options'][$theme_id] = $this
        ->t('%theme (Default)', [
        '%theme' => $theme->info['name'],
      ]);
    }
    else {
      $form['theme']['#options'][$theme_id] = $theme->info['name'];
    }
  }
  $form['blocks'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'context-reaction-blocks-container',
    ],
  ];
  $form['blocks']['block_add'] = [
    '#type' => 'link',
    '#title' => $this
      ->t('Place block'),
    '#attributes' => [
      'id' => 'context-reaction-blocks-region-add',
    ] + $this
      ->getAjaxButtonAttributes(),
    '#url' => Url::fromRoute('context.reaction.blocks.library', [
      'context' => $context
        ->id(),
      'reaction_id' => $this
        ->getPluginId(),
    ], [
      'query' => [
        'theme' => $form_state
          ->getValue('theme', $default_theme),
      ],
    ]),
  ];
  $form['blocks']['blocks'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Block'),
      $this
        ->t('Category'),
      $this
        ->t('Unique'),
      $this
        ->t('Region'),
      $this
        ->t('Weight'),
      $this
        ->t('Operations'),
    ],
    '#empty' => $this
      ->t('No regions available to place blocks in.'),
    '#attributes' => [
      'id' => 'blocks',
    ],
  ];

  // If a theme has been selected use that to get the regions otherwise use
  // the default theme.
  $theme = $form_state
    ->getValue('theme', $default_theme);

  // Get all blocks by their regions.
  $blocks = $this
    ->getBlocks()
    ->getAllByRegion($theme);

  // Get regions of the selected theme.
  $regions = $this
    ->getSystemRegionList($theme);

  // Add each region.
  foreach ($regions as $region => $title) {

    // Add the tabledrag details for this region.
    $form['blocks']['blocks']['#tabledrag'][] = [
      'action' => 'match',
      'relationship' => 'sibling',
      'group' => 'block-region-select',
      'subgroup' => 'block-region-' . $region,
      'hidden' => FALSE,
    ];
    $form['blocks']['blocks']['#tabledrag'][] = [
      'action' => 'order',
      'relationship' => 'sibling',
      'group' => 'block-weight',
      'subgroup' => 'block-weight-' . $region,
    ];

    // Add the theme region.
    $form['blocks']['blocks']['region-' . $region] = [
      '#attributes' => [
        'class' => [
          'region-title',
        ],
      ],
      'title' => [
        '#markup' => $title,
        '#wrapper_attributes' => [
          'colspan' => 6,
        ],
      ],
    ];
    $regionEmptyClass = empty($blocks[$region]) ? 'region-empty' : 'region-populated';
    $form['blocks']['blocks']['region-' . $region . '-message'] = [
      '#attributes' => [
        'class' => [
          'region-message',
          'region-' . $region . '-message',
          $regionEmptyClass,
        ],
      ],
      'message' => [
        '#markup' => '<em>' . $this
          ->t('No blocks in this region') . '</em>',
        '#wrapper_attributes' => [
          'colspan' => 6,
        ],
      ],
    ];

    // Add each block specified for the region if there are any.
    if (isset($blocks[$region])) {

      /** @var BlockPluginInterface $block */
      foreach ($blocks[$region] as $block_id => $block) {
        $configuration = $block
          ->getConfiguration();
        $operations = [
          'edit' => [
            'title' => $this
              ->t('Edit'),
            'url' => Url::fromRoute('context.reaction.blocks.block_edit', [
              'context' => $context
                ->id(),
              'reaction_id' => $this
                ->getPluginId(),
              'block_id' => $block_id,
            ], [
              'query' => [
                'theme' => $theme,
              ],
            ]),
            'attributes' => $this
              ->getAjaxAttributes(),
          ],
          'delete' => [
            'title' => $this
              ->t('Delete'),
            'url' => Url::fromRoute('context.reaction.blocks.block_delete', [
              'context' => $context
                ->id(),
              'block_id' => $block_id,
            ]),
            'attributes' => $this
              ->getAjaxAttributes(),
          ],
        ];
        $form['blocks']['blocks'][$block_id] = [
          '#attributes' => [
            'class' => [
              'draggable',
            ],
          ],
          'label' => [
            '#markup' => $block
              ->label(),
          ],
          'category' => [
            '#markup' => $block
              ->getPluginDefinition()['category'],
          ],
          'unique' => [
            '#markup' => $this
              ->blockShouldBePlacedUniquely($block) ? $this
              ->t('Yes') : $this
              ->t('No'),
          ],
          'region' => [
            '#type' => 'select',
            '#title' => $this
              ->t('Region for @block block', [
              '@block' => $block
                ->label(),
            ]),
            '#title_display' => 'invisible',
            '#default_value' => $region,
            '#options' => $regions,
            '#attributes' => [
              'class' => [
                'block-region-select',
                'block-region-' . $region,
              ],
            ],
          ],
          'weight' => [
            '#type' => 'weight',
            '#default_value' => isset($configuration['weight']) ? $configuration['weight'] : 0,
            '#title' => $this
              ->t('Weight for @block block', [
              '@block' => $block
                ->label(),
            ]),
            '#title_display' => 'invisible',
            '#attributes' => [
              'class' => [
                'block-weight',
                'block-weight-' . $region,
              ],
            ],
          ],
          'operations' => [
            '#type' => 'operations',
            '#links' => $operations,
          ],
        ];
      }
    }
  }
  return $form;
}