block_place.module 2.8 KB

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