Verified Commit 3e3315d6 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

task: #3584347 Deprecate and replace system_admin_compact_mode()

By: znerol
By: nicxvan
By: andypost
By: catch
By: godotislate
By: quietone
By: kentr
By: longwave
By: alexpott
By: larowlan
By: nod_
By: longwave-bot
parent e0b53b2a
Loading
Loading
Loading
Loading
Loading
+7 −23
Changes for core/lib/Drupal/Core/Render/Element/SystemCompactLink.php: 7 added lines, 23 removed lines.
Original line number Diff line number Diff line
@@ -2,10 +2,8 @@

namespace Drupal\Core\Render\Element;

use Drupal\Core\Link as BaseLink;
use Drupal\Core\Render\Attribute\RenderElement;
use Drupal\Core\Url as BaseUrl;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Url;

/**
 * Provides a link to show or hide help text on administration pages.
@@ -57,29 +55,15 @@ public function getInfo() {
   *   The passed-in element containing the system compact link default values.
   */
  public static function preRenderCompactLink($element) {
    // By default, link options to pass to l() are normally set in #options.
    $element += ['#options' => []];

    if (system_admin_compact_mode()) {
      $element['#title'] = t('Show descriptions');
      $element['#url'] = BaseUrl::fromRoute('system.admin_compact_page', ['mode' => 'off']);
      $element['#options'] = [
        'attributes' => ['title' => t('Expand layout to include descriptions.')],
        'query' => \Drupal::destination()->getAsArray(),
      ];
    }
    else {
    $element['#title'] = t('Hide descriptions');
      $element['#url'] = BaseUrl::fromRoute('system.admin_compact_page', ['mode' => 'on']);
    $element['#url'] = Url::fromUri('internal:#?');
    $element['#options'] = [
        'attributes' => ['title' => t('Compress layout by hiding descriptions.')],
        'query' => \Drupal::destination()->getAsArray(),
      'attributes' => [
        'title' => t('Compress layout by hiding descriptions.'),
        'data-admin-compact-toggle' => TRUE,
      ],
    ];
    }

    $options = NestedArray::mergeDeep($element['#url']->getOptions(), $element['#options']);
    $element['#markup'] = BaseLink::fromTextAndUrl($element['#title'], $element['#url']->setOptions($options))->toString();

    $element['#attached']['library'][] = 'system/drupal.system.admin_compact';
    return $element;
  }

+0 −1
Changes for core/modules/system/config/install/system.site.yml: 0 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -7,7 +7,6 @@ page:
  403: ''
  404: ''
  front: /user/login
admin_compact_mode: false
weight_select_max: 100
default_langcode: en
mail_notification: null
+1 −0
Changes for core/modules/system/config/schema/system.schema.yml: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ system.site:
    admin_compact_mode:
      type: boolean
      label: 'Compact mode'
      deprecated: "The 'admin_compact_mode' config is deprecated in drupal:11.5.0 and is removed from drupal:12.0.0. There is no replacement. See https://www.drupal.org/node/3588109."
    weight_select_max:
      type: integer
      label: 'Weight element maximum value'
+8 −0
Changes for core/modules/system/css/system.admin_compact.css: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
/**
 * @file
 * Styles for admin compact mode.
 */

:root[data-admin-compact-mode] [data-admin-compact-collapsible] {
  display: none;
}
+78 −0
Changes for core/modules/system/js/system.admin_compact.js: 78 added lines, 0 removed lines.
Original line number Diff line number Diff line
/**
 * @file
 * Defines the behavior of admin compact mode.
 *
 * Collapses certain elements on admin pages to make them more compact.
 */

(function (Drupal, once) {
  // Set UI-impacting admin-compact-mode data attribute before Drupal behaviors
  // initialize to minimize flickering on load.
  if (JSON.parse(localStorage.getItem('Drupal.admin.compact_mode')) === true) {
    document.documentElement.setAttribute('data-admin-compact-mode', '');
  }

  /**
   * Toggles admin compact mode.
   *
   * @return {boolean}
   *   The next state: true if contact mode is enabled, false otherwise.
   */
  function toggleCompactMode() {
    const nextState = !document.documentElement.hasAttribute(
      'data-admin-compact-mode',
    );
    document.documentElement.toggleAttribute(
      'data-admin-compact-mode',
      nextState,
    );
    localStorage.setItem(
      'Drupal.admin.compact_mode',
      nextState ? 'true' : 'false',
    );

    return nextState;
  }

  /**
   * Update the toggle link element.
   *
   * @param {HTMLElement} element
   *   The toggle link element.
   * @param {boolean} compactMode
   *   The current state: true if contact mode is enabled, false otherwise.
   */
  function updateToggleElement(element, compactMode) {
    element.innerText = compactMode
      ? Drupal.t('Show descriptions')
      : Drupal.t('Hide descriptions');
    element.title = compactMode
      ? Drupal.t('Expand layout to include descriptions.')
      : Drupal.t('Compress layout by hiding descriptions.');
  }

  /**
   * Adds a click event listener to any [data-admin-compact-toggle] elements.
   *
   * @type {Drupal~behavior}
   */
  Drupal.behaviors.adminCompactMode = {
    attach(context) {
      once(
        'admin-compact-mode',
        '[data-admin-compact-toggle]',
        context,
      ).forEach((toggle) => {
        const initialMode = document.documentElement.hasAttribute(
          'data-admin-compact-mode',
        );
        updateToggleElement(toggle, initialMode);
        toggle.addEventListener('click', (event) => {
          const compactMode = toggleCompactMode();
          updateToggleElement(toggle, compactMode);
          event.preventDefault();
        });
      });
    },
  };
})(Drupal, once);
Loading