Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Controls the placement of blocks from all pages.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@0
|
8 use Drupal\Core\Routing\RouteMatchInterface;
|
Chris@0
|
9 use Drupal\Core\Url;
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * Implements hook_help().
|
Chris@0
|
13 */
|
Chris@0
|
14 function block_place_help($route_name, RouteMatchInterface $route_match) {
|
Chris@0
|
15 switch ($route_name) {
|
Chris@0
|
16 case 'help.page.block_place':
|
Chris@0
|
17 $output = '<h3>' . t('About') . '</h3>';
|
Chris@0
|
18 $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>';
|
Chris@0
|
19 $output .= '<h3>' . t('Uses') . '</h3>';
|
Chris@0
|
20 $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>';
|
Chris@0
|
21 return $output;
|
Chris@0
|
22 }
|
Chris@0
|
23 }
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * Implements hook_toolbar().
|
Chris@0
|
27 */
|
Chris@0
|
28 function block_place_toolbar() {
|
Chris@0
|
29 // Link to the current page with a query parameter.
|
Chris@0
|
30 $query = \Drupal::request()->query->all();
|
Chris@0
|
31 $wrapper_class = '';
|
Chris@0
|
32 $status_class = '';
|
Chris@0
|
33 $description = '';
|
Chris@0
|
34 if (isset($query['block-place'])) {
|
Chris@0
|
35 $status_class = 'active';
|
Chris@0
|
36 $wrapper_class = 'is-active';
|
Chris@0
|
37 $description = t('Exit Place block mode.');
|
Chris@0
|
38 unset($query['block-place']);
|
Chris@0
|
39 unset($query['destination']);
|
Chris@0
|
40 }
|
Chris@0
|
41 else {
|
Chris@0
|
42 $status_class = 'inactive';
|
Chris@0
|
43 $description = t('Show regions to Place blocks.');
|
Chris@0
|
44 $query['block-place'] = '1';
|
Chris@0
|
45 // Setting destination is both a work-around for the toolbar "Back to site"
|
Chris@0
|
46 // link in escapeAdmin.js and used for the destination after picking a
|
Chris@0
|
47 // block.
|
Chris@0
|
48 $query['destination'] = Url::fromRoute('<current>')->toString();
|
Chris@0
|
49 }
|
Chris@0
|
50
|
Chris@0
|
51 // Remove on Admin routes.
|
Chris@0
|
52 $admin_route = \Drupal::service('router.admin_context')->isAdminRoute();
|
Chris@0
|
53 // Remove on Block Demo page.
|
Chris@0
|
54 $admin_demo = \Drupal::routeMatch()->getRouteName() === 'block.admin_demo';
|
Chris@0
|
55 $access = (\Drupal::currentUser()->hasPermission('administer blocks') && !$admin_route && !$admin_demo);
|
Chris@0
|
56
|
Chris@0
|
57 // The 'Place Block' tab is a simple link, with no corresponding tray.
|
Chris@0
|
58 $items['block_place'] = [
|
Chris@0
|
59 '#cache' => [
|
Chris@0
|
60 'contexts' => ['user.permissions', 'url.query_args'],
|
Chris@0
|
61 ],
|
Chris@0
|
62 '#type' => 'toolbar_item',
|
Chris@0
|
63 'tab' => [
|
Chris@0
|
64 '#access' => $access,
|
Chris@0
|
65 '#type' => 'link',
|
Chris@0
|
66 '#title' => t('Place block'),
|
Chris@0
|
67 '#url' => Url::fromRoute('<current>', [], ['query' => $query]),
|
Chris@0
|
68 '#attributes' => [
|
Chris@0
|
69 'title' => $description,
|
Chris@0
|
70 'class' => ['toolbar-icon', 'toolbar-icon-place-block-' . $status_class],
|
Chris@0
|
71 ],
|
Chris@0
|
72 ],
|
Chris@0
|
73 '#wrapper_attributes' => [
|
Chris@0
|
74 'class' => ['toolbar-tab', 'block-place-toolbar-tab', $wrapper_class],
|
Chris@0
|
75 ],
|
Chris@0
|
76 '#weight' => 100,
|
Chris@0
|
77 '#attached' => [
|
Chris@0
|
78 'library' => [
|
Chris@0
|
79 'block_place/drupal.block_place.icons',
|
Chris@0
|
80 ],
|
Chris@0
|
81 ],
|
Chris@0
|
82 ];
|
Chris@0
|
83 return $items;
|
Chris@0
|
84 }
|