You are here

block_place.module in Drupal 8

Controls the placement of blocks from all pages.

Deprecated

in drupal:8.8.0 and is removed from drupal:9.0.0. See the change record for a list of alternatives.

File

core/modules/block_place/block_place.module
View source
<?php

/**
 * @file
 * Controls the placement of blocks from all pages.
 *
 * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. See the
 * change record for a list of alternatives.
 *
 * @see https://www.drupal.org/node/3081957
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;

/**
 * Implements hook_help().
 */
function block_place_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.block_place':
      $output = '<h3>' . t('About') . '</h3>';
      $output .= '<p><strong>' . t('Place Blocks (Core, Experimental) is deprecated in Drupal 8.8.0 and will be removed in Drupal 9.0.0. See the change record for a list of alternatives. See <a href=":change-record">the change record</a>.', [
        ':change-record' => 'https://www.drupal.org/node/3081957',
      ]) . '</strong></p>';
      $output .= '<p>' . t('The Place Blocks module allows you to place blocks from every page. For more information, see the <a href=":blocks-documentation">online documentation for the Place Blocks module</a>.', [
        ':blocks-documentation' => 'https://www.drupal.org/documentation/modules/block_place/',
      ]) . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<p>' . t('Block placement is specific to each theme on your site. This module allows you to place blocks in the context of your content pages.') . '</p>';
      return $output;
  }
}

/**
 * Implements hook_toolbar().
 */
function block_place_toolbar() {

  // Link to the current page with a query parameter.
  $query = \Drupal::request()->query
    ->all();
  $wrapper_class = '';
  $status_class = '';
  $description = '';
  if (isset($query['block-place'])) {
    $status_class = 'active';
    $wrapper_class = 'is-active';
    $description = t('Exit Place block mode.');
    unset($query['block-place']);
    unset($query['destination']);
  }
  else {
    $status_class = 'inactive';
    $description = t('Show regions to Place blocks.');
    $query['block-place'] = '1';

    // Setting destination is both a work-around for the toolbar "Back to site"
    // link in escapeAdmin.js and used for the destination after picking a
    // block.
    $query['destination'] = Url::fromRoute('<current>')
      ->toString();
  }

  // Remove on Admin routes.
  $admin_route = \Drupal::service('router.admin_context')
    ->isAdminRoute();

  // Remove on Block Demo page.
  $admin_demo = \Drupal::routeMatch()
    ->getRouteName() === 'block.admin_demo';
  $access = \Drupal::currentUser()
    ->hasPermission('administer blocks') && !$admin_route && !$admin_demo;

  // The 'Place Block' tab is a simple link, with no corresponding tray.
  $items['block_place'] = [
    '#cache' => [
      'contexts' => [
        'user.permissions',
        'url.query_args',
      ],
    ],
    '#type' => 'toolbar_item',
    'tab' => [
      '#access' => $access,
      '#type' => 'link',
      '#title' => t('Place block'),
      '#url' => Url::fromRoute('<current>', [], [
        'query' => $query,
      ]),
      '#attributes' => [
        'title' => $description,
        'class' => [
          'toolbar-icon',
          'toolbar-icon-place-block-' . $status_class,
        ],
      ],
    ],
    '#wrapper_attributes' => [
      'class' => [
        'toolbar-tab',
        'block-place-toolbar-tab',
        $wrapper_class,
      ],
    ],
    '#weight' => 100,
    '#attached' => [
      'library' => [
        'block_place/drupal.block_place.icons',
      ],
    ],
  ];
  return $items;
}

Functions

Namesort descending Description
block_place_help Implements hook_help().
block_place_toolbar Implements hook_toolbar().