SystemCompactLink.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace Drupal\Core\Render\Element;
  3. use Drupal\Core\Url as BaseUrl;
  4. use Drupal\Component\Utility\NestedArray;
  5. /**
  6. * Provides a link to show or hide help text on administration pages.
  7. *
  8. * Usage example:
  9. * @code
  10. * $form['system_compact_link'] = [
  11. * '#type' => 'system_compact_link',
  12. * ];
  13. * @endcode
  14. *
  15. * @RenderElement("system_compact_link")
  16. */
  17. class SystemCompactLink extends Link {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function getInfo() {
  22. $class = get_class($this);
  23. return [
  24. '#pre_render' => [
  25. [$class, 'preRenderCompactLink'],
  26. [$class, 'preRenderLink'],
  27. ],
  28. '#theme_wrappers' => [
  29. 'container' => [
  30. '#attributes' => ['class' => ['compact-link']],
  31. ],
  32. ],
  33. ];
  34. }
  35. /**
  36. * Pre-render callback: Renders a link into #markup.
  37. *
  38. * Doing so during pre_render gives modules a chance to alter the link parts.
  39. *
  40. * @param array $element
  41. * A structured array whose keys form the arguments to Drupal::l():
  42. * - #title: The link text to pass as argument to Drupal::l().
  43. * - One of the following:
  44. * - #route_name and (optionally) a #route_parameters array; The route
  45. * name and route parameters which will be passed into the link
  46. * generator.
  47. * - #href: The system path or URL to pass as argument to Drupal::l().
  48. * - #options: (optional) An array of options to pass to Drupal::l() or the
  49. * link generator.
  50. *
  51. * @return array
  52. * The passed-in element containing the system compact link default values.
  53. */
  54. public static function preRenderCompactLink($element) {
  55. // By default, link options to pass to l() are normally set in #options.
  56. $element += ['#options' => []];
  57. if (system_admin_compact_mode()) {
  58. $element['#title'] = t('Show descriptions');
  59. $element['#url'] = BaseUrl::fromRoute('system.admin_compact_page', ['mode' => 'off']);
  60. $element['#options'] = [
  61. 'attributes' => ['title' => t('Expand layout to include descriptions.')],
  62. 'query' => \Drupal::destination()->getAsArray(),
  63. ];
  64. }
  65. else {
  66. $element['#title'] = t('Hide descriptions');
  67. $element['#url'] = BaseUrl::fromRoute('system.admin_compact_page', ['mode' => 'on']);
  68. $element['#options'] = [
  69. 'attributes' => ['title' => t('Compress layout by hiding descriptions.')],
  70. 'query' => \Drupal::destination()->getAsArray(),
  71. ];
  72. }
  73. $options = NestedArray::mergeDeep($element['#url']->getOptions(), $element['#options']);
  74. $element['#markup'] = \Drupal::l($element['#title'], $element['#url']->setOptions($options));
  75. return $element;
  76. }
  77. }