summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2014-09-09 16:12:26 (GMT)
committerAlex Pott2014-09-09 16:12:26 (GMT)
commitcff05b2e534568d567e416774c9cca6b3014cc71 (patch)
tree292373f5550ada3bc7949e51dec2e0fa4c3ad3f3
parent320ebd0363a92ec2444988498865309fbd7e44af (diff)
Issue #1898478 by joelpittet, Cottser, lokapujya, m1r1k, jstoller, er.pushpinderrana, duellj, organicwire, jessebeach, idflood, Jalandhar, Risse, derheap, galooph, mike.roberts, tlattimore, nadavoid, LinL, steveoliver, chakrapani, likin, killerpoke, EVIIILJ, vlad.dancer, podarok, m86 | c4rl: Menu.inc - Convert theme_ functions to Twig.
-rw-r--r--core/includes/menu.inc88
-rw-r--r--core/includes/theme.inc3
-rw-r--r--core/lib/Drupal/Core/Menu/LocalTaskManagerInterface.php2
-rw-r--r--core/modules/system/css/system.theme.css2
-rw-r--r--core/modules/system/templates/menu-local-action.html.twig15
-rw-r--r--core/modules/system/templates/menu-local-task.html.twig18
-rw-r--r--core/modules/system/templates/menu-local-tasks.html.twig25
-rw-r--r--core/themes/seven/seven.theme94
-rw-r--r--core/themes/seven/templates/menu-local-tasks.html.twig30
9 files changed, 141 insertions, 136 deletions
diff --git a/core/includes/menu.inc b/core/includes/menu.inc
index 68c3c54..12d2a23 100644
--- a/core/includes/menu.inc
+++ b/core/includes/menu.inc
@@ -371,18 +371,18 @@ function theme_menu_link(array $variables) {
}
/**
- * Returns HTML for a single local task link.
+ * Prepares variables for single local task link templates.
*
- * @param $variables
+ * Default template: menu-local-task.html.twig.
+ *
+ * @param array $variables
* An associative array containing:
* - element: A render element containing:
* - #link: A menu link array with 'title', 'href', and 'localized_options'
* keys.
* - #active: A boolean indicating whether the local task is active.
- *
- * @ingroup themeable
*/
-function theme_menu_local_task($variables) {
+function template_preprocess_menu_local_task(&$variables) {
$link = $variables['element']['#link'];
$link += array(
'localized_options' => array(),
@@ -392,6 +392,7 @@ function theme_menu_local_task($variables) {
if (!empty($variables['element']['#active'])) {
// Add text to indicate active tab for non-visual users.
$active = '<span class="visually-hidden">' . t('(active tab)') . '</span>';
+ $variables['attributes']['class'] = array('active');
// If the link does not contain HTML already, String::checkPlain() it now.
// After we set 'html'=TRUE the link will not be sanitized by l().
@@ -403,29 +404,36 @@ function theme_menu_local_task($variables) {
}
$link['localized_options']['set_active_class'] = TRUE;
+ $variables['link'] = array(
+ '#type' => 'link',
+ '#title' => $link_text,
+ '#options' => $link['localized_options'],
+ );
+
if (!empty($link['href'])) {
- // @todo - remove this once all pages are converted to routes.
- $a_tag = l($link_text, $link['href'], $link['localized_options']);
+ // @todo - Remove this once all pages are converted to routes.
+ $variables['link']['#href'] = $link['href'];
}
else {
- $a_tag = \Drupal::l($link_text, $link['route_name'], $link['route_parameters'], $link['localized_options']);
+ $variables['link'] += array(
+ '#route_name' => $link['route_name'],
+ '#route_parameters' => $link['route_parameters'],
+ );
}
-
- return '<li' . (!empty($variables['element']['#active']) ? ' class="active"' : '') . '>' . $a_tag . '</li>';
}
/**
- * Returns HTML for a single local action link.
+ * Prepares variables for single local action link templates.
*
- * @param $variables
+ * Default template: menu-local-action.html.twig.
+ *
+ * @param array $variables
* An associative array containing:
* - element: A render element containing:
* - #link: A menu link array with 'title', 'href', and 'localized_options'
* keys.
- *
- * @ingroup themeable
*/
-function theme_menu_local_action($variables) {
+function template_preprocess_menu_local_action(&$variables) {
$link = $variables['element']['#link'];
$link += array(
'href' => '',
@@ -436,19 +444,23 @@ function theme_menu_local_action($variables) {
$link['localized_options']['attributes']['class'][] = 'button-action';
$link['localized_options']['set_active_class'] = TRUE;
- $output = '<li>';
- // @todo Remove this check and the call to l() when all pages are converted to
- // routes.
+ $variables['link'] = array(
+ '#type' => 'link',
+ '#title' => $link['title'],
+ '#options' => $link['localized_options'],
+ );
+
// @todo Figure out how to support local actions without a href properly.
if ($link['href'] === '' && !empty($link['route_name'])) {
- $output .= Drupal::l($link['title'], $link['route_name'], $link['route_parameters'], $link['localized_options']);
+ $variables['link'] += array(
+ '#route_name' => $link['route_name'],
+ '#route_parameters' => $link['route_parameters'],
+ );
}
else {
- $output .= l($link['title'], $link['href'], $link['localized_options']);
+ // @todo - Remove this once all pages are converted to routes.
+ $variables['link']['#href'] = $link['href'];
}
- $output .= "</li>";
-
- return $output;
}
/**
@@ -641,36 +653,6 @@ function menu_local_tabs() {
}
/**
- * Returns HTML for primary and secondary local tasks.
- *
- * @param $variables
- * An associative array containing:
- * - primary: (optional) An array of local tasks (tabs).
- * - secondary: (optional) An array of local tasks (tabs).
- *
- * @ingroup themeable
- * @see menu_local_tasks()
- */
-function theme_menu_local_tasks(&$variables) {
- $output = '';
-
- if (!empty($variables['primary'])) {
- $variables['primary']['#prefix'] = '<h2 class="visually-hidden">' . t('Primary tabs') . '</h2>';
- $variables['primary']['#prefix'] .= '<ul class="tabs primary">';
- $variables['primary']['#suffix'] = '</ul>';
- $output .= drupal_render($variables['primary']);
- }
- if (!empty($variables['secondary'])) {
- $variables['secondary']['#prefix'] = '<h2 class="visually-hidden">' . t('Secondary tabs') . '</h2>';
- $variables['secondary']['#prefix'] .= '<ul class="tabs secondary">';
- $variables['secondary']['#suffix'] = '</ul>';
- $output .= drupal_render($variables['secondary']);
- }
-
- return $output;
-}
-
-/**
* Clears all cached menu data.
*
* This should be called any time broad changes
diff --git a/core/includes/theme.inc b/core/includes/theme.inc
index 537c58f..a35ab83 100644
--- a/core/includes/theme.inc
+++ b/core/includes/theme.inc
@@ -2377,12 +2377,15 @@ function drupal_common_theme() {
),
'menu_local_task' => array(
'render element' => 'element',
+ 'template' => 'menu-local-task',
),
'menu_local_action' => array(
'render element' => 'element',
+ 'template' => 'menu-local-action',
),
'menu_local_tasks' => array(
'variables' => array('primary' => array(), 'secondary' => array()),
+ 'template' => 'menu-local-tasks',
),
// From form.inc.
'input' => array(
diff --git a/core/lib/Drupal/Core/Menu/LocalTaskManagerInterface.php b/core/lib/Drupal/Core/Menu/LocalTaskManagerInterface.php
index c832b90..e75307d 100644
--- a/core/lib/Drupal/Core/Menu/LocalTaskManagerInterface.php
+++ b/core/lib/Drupal/Core/Menu/LocalTaskManagerInterface.php
@@ -49,7 +49,7 @@ interface LocalTaskManagerInterface extends PluginManagerInterface {
* The route for which to make renderable local tasks.
*
* @return array
- * A render array as expected by theme_menu_local_tasks.
+ * A render array as expected by menu-local-tasks.html.twig.
*/
public function getTasksBuild($current_route_name);
diff --git a/core/modules/system/css/system.theme.css b/core/modules/system/css/system.theme.css
index d415710..eb61ae9 100644
--- a/core/modules/system/css/system.theme.css
+++ b/core/modules/system/css/system.theme.css
@@ -450,7 +450,7 @@ ul.links a.active {
}
/**
- * Markup generated by theme_menu_local_tasks().
+ * Markup generated by menu-local-tasks.html.twig.
*/
div.tabs {
margin: 1em 0;
diff --git a/core/modules/system/templates/menu-local-action.html.twig b/core/modules/system/templates/menu-local-action.html.twig
new file mode 100644
index 0000000..0eb03a9
--- /dev/null
+++ b/core/modules/system/templates/menu-local-action.html.twig
@@ -0,0 +1,15 @@
+{#
+/**
+ * @file
+ * Default theme implementation for a single local action link.
+ *
+ * Available variables:
+ * - attributes: HTML attributes for the wrapper element.
+ * - link: A rendered link element.
+ *
+ * @see template_preprocess_menu_local_action()
+ *
+ * @ingroup themeable
+ */
+#}
+<li{{ attributes }}>{{ link }}</li>
diff --git a/core/modules/system/templates/menu-local-task.html.twig b/core/modules/system/templates/menu-local-task.html.twig
new file mode 100644
index 0000000..5939203
--- /dev/null
+++ b/core/modules/system/templates/menu-local-task.html.twig
@@ -0,0 +1,18 @@
+{#
+/**
+ * @file
+ * Default theme implementation for a local task link.
+ *
+ * Available variables:
+ * - attributes: HTML attributes for the wrapper element.
+ * - link: A rendered link element.
+ *
+ * Note: This template renders the content for each task item in
+ * menu-local-tasks.html.twig.
+ *
+ * @see template_preprocess_menu_local_task()
+ *
+ * @ingroup themeable
+ */
+#}
+<li{{ attributes }}>{{ link }}</li>
diff --git a/core/modules/system/templates/menu-local-tasks.html.twig b/core/modules/system/templates/menu-local-tasks.html.twig
new file mode 100644
index 0000000..dce922b
--- /dev/null
+++ b/core/modules/system/templates/menu-local-tasks.html.twig
@@ -0,0 +1,25 @@
+{#
+/**
+ * @file
+ * Default theme implementation to display primary and secondary local tasks.
+ *
+ * Available variables:
+ * - primary: HTML list items representing primary tasks.
+ * - secondary: HTML list items representing primary tasks.
+ *
+ * Each item in these variables (primary and secondary) can be individually
+ * themed in menu-local-task.html.twig.
+ *
+ * @see template_preprocess_menu_local_tasks()
+ *
+ * @ingroup themeable
+ */
+#}
+{% if primary %}
+ <h2 class="visually-hidden">{{ 'Primary tabs'|t }}</h2>
+ <ul class="tabs primary">{{ primary }}</ul>
+{% endif %}
+{% if secondary %}
+ <h2 class="visually-hidden">{{ 'Secondary tabs'|t }}</h2>
+ <ul class="tabs secondary">{{ secondary }}</ul>
+{% endif %}
diff --git a/core/themes/seven/seven.theme b/core/themes/seven/seven.theme
index 5ac883f..f2281cb 100644
--- a/core/themes/seven/seven.theme
+++ b/core/themes/seven/seven.theme
@@ -37,77 +37,34 @@ function seven_preprocess_page(&$variables) {
}
/**
- * Overrides theme_menu_local_tasks().
+ * Implements hook_pre_render_HOOK() for menu-local-tasks templates.
*
- * Returns HTML for primary and secondary local tasks.
+ * Use preprocess hook to set #attached to child elemnts
+ * because they will be processed by Twig and drupal_render will
+ * be invoked.
*/
-function seven_menu_local_tasks(&$variables) {
- $output = '';
-
+function seven_preprocess_menu_local_tasks(&$variables) {
if (!empty($variables['primary'])) {
$variables['primary']['#attached'] = array(
'library' => array(
'seven/drupal.nav-tabs',
),
);
- $variables['primary']['#prefix'] = '<h2 id="primary-tabs-title" class="visually-hidden">' . t('Primary tabs') . '</h2>';
- $variables['primary']['#prefix'] .= '<nav role="navigation" class="is-horizontal is-collapsible" aria-labelledby="primary-tabs-title" data-drupal-nav-tabs>';
- $variables['primary']['#prefix'] .= '<button class="reset-appearance tabs__tab tabs__trigger" aria-label="Primary tabs display toggle" data-drupal-nav-tabs-trigger>&bull;&bull;&bull;</button>';
- $variables['primary']['#prefix'] .= '<ul class="tabs primary clearfix" data-drupal-nav-tabs-target>';
- $variables['primary']['#suffix'] = '</ul>';
- $variables['primary']['#suffix'] .= '</nav>';
- $output .= drupal_render($variables['primary']);
}
- if (!empty($variables['secondary'])) {
+ elseif (!empty($variables['secondary'])) {
$variables['secondary']['#attached'] = array(
'library' => array(
'seven/drupal.nav-tabs',
),
);
- $variables['secondary']['#prefix'] = '<h2 id="secondary-tabs-title" class="visually-hidden">' . t('Secondary tabs') . '</h2>';
- $variables['secondary']['#prefix'] .= '<nav role="navigation" class="is-horizontal" aria-labelledby="secondary-tabs-title" data-drupal-nav-tabs>';
- $variables['secondary']['#prefix'] .= '<ul class="tabs secondary clearfix">';
- $variables['secondary']['#suffix'] = '</ul>';
- $variables['secondary']['#suffix'] .= '</nav>';
- $output .= drupal_render($variables['secondary']);
}
-
- return $output;
}
/**
- * Overrides theme_menu_local_task().
- *
- * Returns HTML for a local task.
+ * Implements hook_preprocess_HOOK() for menu-local-task templates.
*/
-function seven_menu_local_task($variables) {
- $link = $variables['element']['#link'];
- $link += array(
- 'localized_options' => array(),
- );
- $link_text = $link['title'];
-
- if (!empty($variables['element']['#active'])) {
- // Add text to indicate active tab for non-visual users.
- $active = '<span class="visually-hidden">' . t('(active tab)') . '</span>';
-
- // If the link does not contain HTML already, String::checkPlain() it now.
- // After we set 'html'=TRUE the link will not be sanitized by l().
- if (empty($link['localized_options']['html'])) {
- $link['title'] = String::checkPlain($link['title']);
- }
- $link['localized_options']['html'] = TRUE;
- $link_text = t('!local-task-title!active', array('!local-task-title' => $link['title'], '!active' => $active));
- }
- if (!empty($link['href'])) {
- // @todo - remove this once all pages are converted to routes.
- $a_tag = l($link_text, $link['href'], $link['localized_options']);
- }
- else {
- $a_tag = \Drupal::l($link_text, $link['route_name'], $link['route_parameters'], $link['localized_options']);
- }
-
- return '<li' . (!empty($variables['element']['#active']) ? ' class="tabs__tab active"' : ' class="tabs__tab"') . '>' . $a_tag . '</li>';
+function seven_preprocess_menu_local_task(&$variables) {
+ $variables['attributes']['class'][] = 'tabs__tab';
}
/**
@@ -164,22 +121,11 @@ function seven_preprocess_tablesort_indicator(&$variables) {
}
/**
- * Overrides theme_menu_local_action().
+ * Implements hook_preprocess_HOOK() for menu-local-action templates.
*/
-function seven_menu_local_action($variables) {
- $link = $variables['element']['#link'];
- $link += array(
- 'href' => '',
- 'localized_options' => array(),
- 'route_parameters' => array(),
- );
- $link['localized_options']['attributes']['class'][] = 'button';
- $link['localized_options']['attributes']['class'][] = 'button--primary';
- $link['localized_options']['attributes']['class'][] = 'button--small';
-
- // @todo Replace with a generalized solution for icons.
- // See http://drupal.org/node/1849712
- $link['localized_options']['attributes']['class'][] = 'button-action';
+function seven_preprocess_menu_local_action(array &$variables) {
+ $variables['link']['#options']['attributes']['class'][] = 'button--primary';
+ $variables['link']['#options']['attributes']['class'][] = 'button--small';
// We require Modernizr's touch test for button styling.
$libraries = array(
@@ -190,20 +136,6 @@ function seven_menu_local_action($variables) {
),
);
drupal_render($libraries);
-
- $output = '<li>';
- // @todo Remove this check and the call to l() when all pages are converted to
- // routes.
- // @todo Figure out how to support local actions without a href properly.
- if ($link['href'] === '' && !empty($link['route_name'])) {
- $output .= Drupal::l($link['title'], $link['route_name'], $link['route_parameters'], $link['localized_options']);
- }
- else {
- $output .= l($link['title'], $link['href'], $link['localized_options']);
- }
- $output .= "</li>";
-
- return $output;
}
/**
diff --git a/core/themes/seven/templates/menu-local-tasks.html.twig b/core/themes/seven/templates/menu-local-tasks.html.twig
new file mode 100644
index 0000000..c71ae04
--- /dev/null
+++ b/core/themes/seven/templates/menu-local-tasks.html.twig
@@ -0,0 +1,30 @@
+{#
+/**
+ * @file
+ * Seven theme implementation to display primary and secondary local tasks.
+ *
+ * Available variables:
+ * - primary: HTML list items representing primary tasks.
+ * - secondary: HTML list items representing primary tasks.
+ *
+ * Each item in these variables (primary and secondary) can be individually
+ * themed in menu-local-task.html.twig.
+ *
+ * @see template_preprocess_menu_local_tasks()
+ *
+ * @ingroup themeable
+ */
+#}
+{% if primary %}
+ <h2 id="primary-tabs-title" class="visually-hidden">{{ 'Primary tabs'|t }}</h2>
+ <nav role="navigation" class="is-horizontal is-collapsible" aria-labelledby="primary-tabs-title" data-drupal-nav-tabs>
+ <button class="reset-appearance tabs__tab tabs__trigger" aria-label="{{ 'Primary tabs display toggle'|t }}" data-drupal-nav-tabs-trigger>&bull;&bull;&bull;</button>
+ <ul class="tabs primary clearfix" data-drupal-nav-tabs-target>{{ primary }}</ul>
+ </nav>
+{% endif %}
+{% if secondary %}
+ <h2 id="secondary-tabs-title" class="visually-hidden">{{ 'Secondary tabs'|t }}</h2>
+ <nav role="navigation" class="is-horizontal" aria-labelledby="secondary-tabs-title" data-drupal-nav-tabs>
+ <ul class="tabs secondary clearfix">{{ secondary }}</ul>
+ </nav>
+{% endif %}