SystemCompactLink.php 2.7 KB

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