ContextualLinks.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace Drupal\contextual\Element;
  3. use Drupal\Component\Utility\Html;
  4. use Drupal\Core\Render\Element\RenderElement;
  5. use Drupal\Core\Url;
  6. /**
  7. * Provides a contextual_links element.
  8. *
  9. * @RenderElement("contextual_links")
  10. */
  11. class ContextualLinks extends RenderElement {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function getInfo() {
  16. $class = get_class($this);
  17. return [
  18. '#pre_render' => [
  19. [$class, 'preRenderLinks'],
  20. ],
  21. '#theme' => 'links__contextual',
  22. '#links' => [],
  23. '#attributes' => ['class' => ['contextual-links']],
  24. '#attached' => [
  25. 'library' => [
  26. 'contextual/drupal.contextual-links',
  27. ],
  28. ],
  29. ];
  30. }
  31. /**
  32. * Pre-render callback: Builds a renderable array for contextual links.
  33. *
  34. * @param array $element
  35. * A renderable array containing a #contextual_links property, which is a
  36. * keyed array. Each key is the name of the group of contextual links to
  37. * render (based on the 'group' key in the *.links.contextual.yml files for
  38. * all enabled modules). The value contains an associative array containing
  39. * the following keys:
  40. * - route_parameters: The route parameters passed to the url generator.
  41. * - metadata: Any additional data needed in order to alter the link.
  42. * @code
  43. * array('#contextual_links' => array(
  44. * 'block' => array(
  45. * 'route_parameters' => array('block' => 'system.menu-tools'),
  46. * ),
  47. * 'menu' => array(
  48. * 'route_parameters' => array('menu' => 'tools'),
  49. * ),
  50. * ))
  51. * @endcode
  52. *
  53. * @return array
  54. * A renderable array representing contextual links.
  55. */
  56. public static function preRenderLinks(array $element) {
  57. // Retrieve contextual menu links.
  58. $items = [];
  59. $contextual_links_manager = static::contextualLinkManager();
  60. foreach ($element['#contextual_links'] as $group => $args) {
  61. $args += [
  62. 'route_parameters' => [],
  63. 'metadata' => [],
  64. ];
  65. $items += $contextual_links_manager->getContextualLinksArrayByGroup($group, $args['route_parameters'], $args['metadata']);
  66. }
  67. // Transform contextual links into parameters suitable for links.html.twig.
  68. $links = [];
  69. foreach ($items as $class => $item) {
  70. $class = Html::getClass($class);
  71. $links[$class] = [
  72. 'title' => $item['title'],
  73. 'url' => Url::fromRoute(isset($item['route_name']) ? $item['route_name'] : '', isset($item['route_parameters']) ? $item['route_parameters'] : [], $item['localized_options']),
  74. ];
  75. }
  76. $element['#links'] = $links;
  77. // Allow modules to alter the renderable contextual links element.
  78. static::moduleHandler()->alter('contextual_links_view', $element, $items);
  79. // If there are no links, tell drupal_render() to abort rendering.
  80. if (empty($element['#links'])) {
  81. $element['#printed'] = TRUE;
  82. }
  83. return $element;
  84. }
  85. /**
  86. * Wraps the contextual link manager.
  87. *
  88. * @return \Drupal\Core\Menu\ContextualLinkManager
  89. */
  90. protected static function contextualLinkManager() {
  91. return \Drupal::service('plugin.manager.menu.contextual_link');
  92. }
  93. /**
  94. * Wraps the module handler.
  95. *
  96. * @return \Drupal\Core\Extension\ModuleHandlerInterface
  97. */
  98. protected static function moduleHandler() {
  99. return \Drupal::moduleHandler();
  100. }
  101. }