block_place.module 3.3 KB

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