You are here

public function Panels::selectBlock in Panels 8.4

Same name and namespace in other branches
  1. 8.3 src/Controller/Panels.php \Drupal\panels\Controller\Panels::selectBlock()

Presents a list of blocks to add to the variant.

Parameters

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

string $machine_name: 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 'Panels::selectBlock'
panels.routing.yml in ./panels.routing.yml
panels.routing.yml

File

src/Controller/Panels.php, line 107

Class

Panels
Provides route controllers for Panels routes.

Namespace

Drupal\panels\Controller

Code

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

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

  /** @var \Drupal\panels\Plugin\PanelsPattern\PanelsPatternInterface $pattern_plugin */
  $pattern_plugin = $variant_plugin
    ->getPattern();
  $contexts = $pattern_plugin
    ->getDefaultContexts($this->tempstore, $tempstore_id, $machine_name);
  $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' => $pattern_plugin
        ->getBlockAddUrl($tempstore_id, $machine_name, $plugin_id, $request->query
        ->get('region'), $request->query
        ->get('destination')),
      'attributes' => $this
        ->getAjaxAttributes(),
    ];
  }
  return $build;
}