Commit 9e163fc5 authored by alexpott's avatar alexpott

Issue #1833932 by lauriii, lokapujya, cilefen, Cottser, joelpittet,...

Issue #1833932 by lauriii, lokapujya, cilefen, Cottser, joelpittet, jenlampton, iMiksu, lanchez, ericrdb, mikispeed, Eric_A, mikemiles86: Convert theme_system_compact_link() into a #type link
parent ed01f40c
<?php
/**
* @file
* Contains \Drupal\Core\Render\Element\SystemCompactLink.
*/
namespace Drupal\Core\Render\Element;
use Drupal\Core\Url as BaseUrl;
use Drupal\Component\Utility\NestedArray;
/**
* Provides a link render element to show or hide inline help descriptions.
*
* @RenderElement("system_compact_link")
*/
class SystemCompactLink extends Link {
/**
* {@inheritdoc}
*/
public function getInfo() {
$class = get_class($this);
return array(
'#pre_render' => array(
array($class, 'preRenderCompactLink'),
array($class, 'preRenderLink'),
),
'#theme_wrappers' => array(
'container' => array(
'#attributes' => array('class' => array('compact-link')),
),
),
);
}
/**
* Pre-render callback: Renders a link into #markup.
*
* Doing so during pre_render gives modules a chance to alter the link parts.
*
* @param array $element
* A structured array whose keys form the arguments to Drupal::l():
* - #title: The link text to pass as argument to Drupal::l().
* - One of the following:
* - #route_name and (optionally) a #route_parameters array; The route
* name and route parameters which will be passed into the link
* generator.
* - #href: The system path or URL to pass as argument to Drupal::l().
* - #options: (optional) An array of options to pass to Drupal::l() or the
* link generator.
*
* @return array
* 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 += array('#options' => array());
if (system_admin_compact_mode()) {
$element['#title'] = t('Show descriptions');
$element['#url'] = BaseUrl::fromRoute('system.admin_compact_page', array('mode' => 'off'));
$element['#options'] = array(
'attributes' => array('title' => t('Expand layout to include descriptions.')),
'query' => drupal_get_destination(),
);
}
else {
$element['#title'] = t('Hide descriptions');
$element['#url'] = BaseUrl::fromRoute('system.admin_compact_page', array('mode' => 'on'));
$element['#options'] = array(
'attributes' => array('title' => t('Compress layout by hiding descriptions.')),
'query' => drupal_get_destination(),
);
}
$options = NestedArray::mergeDeep($element['#url']->getOptions(), $element['#options']);
$element['#markup'] = \Drupal::l($element['#title'], $element['#url']->setOptions($options));
return $element;
}
}
......@@ -57,7 +57,7 @@
}
/**
* Markup generated by theme_system_compact_link().
* System compact link: to toggle the display of description text.
*/
.compact-link {
margin: 0 0 0.5em 0;
......
......@@ -184,4 +184,50 @@ function testMoreLink() {
}
}
/**
* Tests system #type 'system_compact_link'.
*/
function testSystemCompactLink() {
$elements = array(
array(
'name' => "#type 'system_compact_link' when admin compact mode is off",
'value' => array(
'#type' => 'system_compact_link',
),
'expected' => '//div[@class="compact-link"]/a[contains(@href, "admin/compact/on?") and text()="Hide descriptions"]',
),
array(
'name' => "#type 'system_compact_link' when adding extra attributes",
'value' => array(
'#type' => 'system_compact_link',
'#attributes' => array(
'class' => array('kittens-rule'),
),
),
'expected' => '//div[@class="compact-link"]/a[contains(@href, "admin/compact/on?") and @class="kittens-rule" and text()="Hide descriptions"]',
),
);
foreach ($elements as $element) {
$xml = new \SimpleXMLElement(drupal_render($element['value']));
$result = $xml->xpath($element['expected']);
$this->assertTrue($result, '"' . $element['name'] . '" is rendered correctly by drupal_render().');
}
// Set admin compact mode on for additional tests.
\Drupal::request()->cookies->set('Drupal_visitor_admin_compact_mode', TRUE);
$element = array(
'name' => "#type 'system_compact_link' when admin compact mode is on",
'value' => array(
'#type' => 'system_compact_link',
),
'expected' => '//div[@class="compact-link"]/a[contains(@href, "admin/compact?") and text()="Show descriptions"]',
);
$xml = new \SimpleXMLElement(drupal_render($element['value']));
$result = $xml->xpath($element['expected']);
$this->assertTrue($result, '"' . $element['name'] . '" is rendered correctly by drupal_render().');
}
}
......@@ -81,7 +81,7 @@ function template_preprocess_admin_block_content(&$variables) {
*/
function template_preprocess_admin_page(&$variables) {
$variables['system_compact_link'] = array(
'#theme' => 'system_compact_link',
'#type' => 'system_compact_link',
);
$variables['containers'] = array();
$stripe = 0;
......@@ -110,7 +110,7 @@ function template_preprocess_admin_page(&$variables) {
*/
function template_preprocess_system_admin_index(&$variables) {
$variables['system_compact_link'] = array(
'#theme' => 'system_compact_link',
'#type' => 'system_compact_link',
);
$variables['containers'] = array();
$stripe = 0;
......
......@@ -210,10 +210,6 @@ function system_theme() {
'variables' => array('menu_items' => NULL),
'file' => 'system.admin.inc',
),
'system_compact_link' => array(
'variables' => array(),
'function' => 'theme_system_compact_link',
),
));
}
......@@ -1213,24 +1209,6 @@ function system_time_zones($blank = NULL) {
return $zones;
}
/**
* Returns HTML for a link to show or hide inline help descriptions.
*
* @ingroup themeable
*/
function theme_system_compact_link() {
$output = '<div class="compact-link">';
if (system_admin_compact_mode()) {
$output .= \Drupal::l(t('Show descriptions'), new Url('system.admin_compact_page', array('mode' => 'off'), array('attributes' => array('title' => t('Expand layout to include descriptions.')), 'query' => drupal_get_destination())));
}
else {
$output .= \Drupal::l(t('Hide descriptions'), new Url('system.admin_compact_page', array('mode' => 'on'), array('attributes' => array('title' => t('Compress layout by hiding descriptions.')), 'query' => drupal_get_destination())));
}
$output .= '</div>';
return $output;
}
/**
* Attempts to get a file using Guzzle HTTP client and to store it locally.
*
......
......@@ -97,7 +97,8 @@ public function buildForm(array $form, FormStateInterface $form_state) {
$hide_descriptions = system_admin_compact_mode();
$form['system_compact_link'] = array(
'#theme' => 'system_compact_link',
'#id' => FALSE,
'#type' => 'system_compact_link',
);
$form['permissions'] = array(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment