You are here

public function PageManagerController::selectBlock in Page Manager 8

Same name and namespace in other branches
  1. 8.4 page_manager_ui/src/Controller/PageManagerController.php \Drupal\page_manager_ui\Controller\PageManagerController::selectBlock()

Presents a list of blocks to add to the variant.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The current request.

string $block_display: The identifier of the block display variant.

string $tempstore_id: The identifier of the temporary store.

Return value

array The block selection page.

1 string reference to 'PageManagerController::selectBlock'
page_manager_ui.routing.yml in page_manager_ui/page_manager_ui.routing.yml
page_manager_ui/page_manager_ui.routing.yml

File

page_manager_ui/src/Controller/PageManagerController.php, line 307
Contains \Drupal\page_manager_ui\Controller\PageManagerController.

Class

PageManagerController
Provides route controllers for Page Manager.

Namespace

Drupal\page_manager_ui\Controller

Code

public function selectBlock(Request $request, $block_display, $tempstore_id) {
  $cached_values = $this->tempstore
    ->get($tempstore_id)
    ->get($block_display);

  /** @var \Drupal\page_manager\Plugin\DisplayVariant\PageBlockDisplayVariant $variant_plugin */
  $variant_plugin = $cached_values['plugin'];

  // Rehydrate the contexts on this end.
  $contexts = [];

  /**
   * @var string $context_name
   * @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context_definition
   */
  foreach ($cached_values['contexts'] as $context_name => $context_definition) {
    $contexts[$context_name] = new Context($context_definition);
  }
  $variant_plugin
    ->setContexts($contexts);

  // Add a section containing the available blocks to be added to the variant.
  $build = [
    '#type' => 'container',
    '#attached' => [
      'library' => [
        'core/drupal.ajax',
      ],
    ],
  ];
  $available_plugins = $this->blockManager
    ->getDefinitionsForContexts($variant_plugin
    ->getContexts());

  // Order by category, and then by admin label.
  $available_plugins = $this->blockManager
    ->getSortedDefinitions($available_plugins);
  foreach ($available_plugins as $plugin_id => $plugin_definition) {

    // Make a section for each region.
    $category = $plugin_definition['category'];
    $category_key = 'category-' . $category;
    if (!isset($build[$category_key])) {
      $build[$category_key] = [
        '#type' => 'fieldgroup',
        '#title' => $category,
        'content' => [
          '#theme' => 'links',
        ],
      ];
    }

    // Add a link for each available block within each region.
    $build[$category_key]['content']['#links'][$plugin_id] = [
      'title' => $plugin_definition['admin_label'],
      'url' => Url::fromRoute('page_manager.block_display_add_block', [
        'block_display' => $block_display,
        'block_id' => $plugin_id,
        'region' => $request->query
          ->get('region'),
        'destination' => $request->query
          ->get('destination'),
      ]),
      'attributes' => $this
        ->getAjaxAttributes(),
    ];
  }
  return $build;
}