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