3 namespace Drupal\Core\Render\Element;
5 use Drupal\Core\Url as BaseUrl;
6 use Drupal\Component\Utility\NestedArray;
9 * Provides a link to show or hide help text on administration pages.
13 * $form['system_compact_link'] = [
14 * '#type' => 'system_compact_link',
18 * @RenderElement("system_compact_link")
20 class SystemCompactLink extends Link {
25 public function getInfo() {
26 $class = get_class($this);
29 [$class, 'preRenderCompactLink'],
30 [$class, 'preRenderLink'],
32 '#theme_wrappers' => [
34 '#attributes' => ['class' => ['compact-link']],
41 * Pre-render callback: Renders a link into #markup.
43 * Doing so during pre_render gives modules a chance to alter the link parts.
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
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
57 * The passed-in element containing the system compact link default values.
59 public static function preRenderCompactLink($element) {
60 // By default, link options to pass to l() are normally set in #options.
61 $element += ['#options' => []];
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()
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(),
80 $options = NestedArray::mergeDeep($element['#url']->getOptions(), $element['#options']);
81 $element['#markup'] = \Drupal::l($element['#title'], $element['#url']->setOptions($options));