Advertisement
Sk8erPeter

D7 - Changing language switcher block links' title attribute

Jul 22nd, 2012
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.08 KB | None | 0 0
  1. <?php
  2.  
  3. // Drupal 7
  4. // template.php in my custom zenTest theme... (subtheme of Zen)
  5. // changing the "title" attribute of the language switcher block's links
  6. // ........................
  7.  
  8. /**
  9.  * Implements hook_theme()
  10.  *
  11.  * @return array
  12.  */
  13. function zenTest_theme() {
  14.     return array(
  15.       'links__locale_block' => array(
  16.         'variables' => array('links' => NULL, 'attributes' => array('class' => array('links')), 'heading' => array()),
  17.       ),
  18.     );
  19. }
  20.  
  21.  
  22. /**
  23.  * @see zenTest_links()
  24.  *
  25.  * @param array $variables
  26.  * @return string
  27.  */
  28. function zenTest_links__locale_block($variables) {
  29.     $node = menu_get_object();
  30.     if ($node->nid) {
  31.         if ($node->type == 'test_multilingual_type') {
  32.             $myTestFieldValue = $node->field_title_for_test['und'][0]['value'];
  33.             foreach ($variables['links'] as $langcode => $langLinksArray) {
  34.                 $options = array('langcode'=>$langcode);
  35.                 $variables['links'][$langcode]['attributes']['title'] = t('!myTestFieldValue (original: !originalTitle)', array(
  36.                   '!myTestFieldValue' => $myTestFieldValue,
  37.                   '!originalTitle' => $langLinksArray['attributes']['title'],
  38.                     ),
  39.                     $options
  40.                 );
  41.             }
  42.         }
  43.     }
  44.    
  45.     return zenTest_links($variables);
  46. }
  47.  
  48. /**
  49.  * Returns HTML for a set of links.
  50.  *
  51.  * @param $variables
  52.  *   An associative array containing:
  53.  *   - links: An associative array of links to be themed. The key for each link
  54.  *     is used as its CSS class. Each link should be itself an array, with the
  55.  *     following elements:
  56.  *     - title: The link text.
  57.  *     - href: The link URL. If omitted, the 'title' is shown as a plain text
  58.  *       item in the links list.
  59.  *     - html: (optional) Whether or not 'title' is HTML. If set, the title
  60.  *       will not be passed through check_plain().
  61.  *     - attributes: (optional) Attributes for the anchor, or for the <span> tag
  62.  *       used in its place if no 'href' is supplied. If element 'class' is
  63.  *       included, it must be an array of one or more class names.
  64.  *     If the 'href' element is supplied, the entire link array is passed to l()
  65.  *     as its $options parameter.
  66.  *   - attributes: A keyed array of attributes for the UL containing the
  67.  *     list of links.
  68.  *   - heading: (optional) A heading to precede the links. May be an associative
  69.  *     array or a string. If it's an array, it can have the following elements:
  70.  *     - text: The heading text.
  71.  *     - level: The heading level (e.g. 'h2', 'h3').
  72.  *     - class: (optional) An array of the CSS classes for the heading.
  73.  *     When using a string it will be used as the text of the heading and the
  74.  *     level will default to 'h2'. Headings should be used on navigation menus
  75.  *     and any list of links that consistently appears on multiple pages. To
  76.  *     make the heading invisible use the 'element-invisible' CSS class. Do not
  77.  *     use 'display:none', which removes it from screen-readers and assistive
  78.  *     technology. Headings allow screen-reader and keyboard only users to
  79.  *     navigate to or skip the links. See
  80.  *     http://juicystudio.com/article/screen-readers-display-none.php and
  81.  *     http://www.w3.org/TR/WCAG-TECHS/H42.html for more information.
  82.  */
  83. function zenTest_links($variables) {
  84.     $links = $variables['links'];
  85.     $attributes = $variables['attributes'];
  86.     $heading = $variables['heading'];
  87.     global $language_url;
  88.     $output = '';
  89.  
  90.     if (count($links) > 0) {
  91.         $output = '';
  92.  
  93.         // Treat the heading first if it is present to prepend it to the
  94.         // list of links.
  95.         if (!empty($heading)) {
  96.             if (is_string($heading)) {
  97.                 // Prepare the array that will be used when the passed heading
  98.                 // is a string.
  99.                 $heading = array(
  100.                   'text' => $heading,
  101.                   // Set the default level of the heading.
  102.                   'level' => 'h2',
  103.                 );
  104.             }
  105.             $output .= '<' . $heading['level'];
  106.             if (!empty($heading['class'])) {
  107.                 $output .= drupal_attributes(array('class' => $heading['class']));
  108.             }
  109.             $output .= '>' . check_plain($heading['text']) . '</' . $heading['level'] . '>';
  110.         }
  111.  
  112.         $output .= '<ul' . drupal_attributes($attributes) . '>';
  113.  
  114.         $num_links = count($links);
  115.         $i = 1;
  116.  
  117.         foreach ($links as $key => $link) {
  118.             $class = array($key);
  119.  
  120.             // Add first, last and active classes to the list of links to help out themers.
  121.             if ($i == 1) {
  122.                 $class[] = 'first';
  123.             }
  124.             if ($i == $num_links) {
  125.                 $class[] = 'last';
  126.             }
  127.             if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))
  128.                 && (empty($link['language']) || $link['language']->language == $language_url->language)) {
  129.                 $class[] = 'active';
  130.             }
  131.             $output .= '<li' . drupal_attributes(array('class' => $class)) . '>';
  132.  
  133.             if (isset($link['href'])) {
  134.                 // Pass in $link as $options, they share the same keys.
  135.                 $output .= l($link['title'], $link['href'], $link);
  136.             }
  137.             elseif (!empty($link['title'])) {
  138.                 // Some links are actually not links, but we wrap these in <span> for adding title and class attributes.
  139.                 if (empty($link['html'])) {
  140.                     $link['title'] = check_plain($link['title']);
  141.                 }
  142.                 $span_attributes = '';
  143.                 if (isset($link['attributes'])) {
  144.                     $span_attributes = drupal_attributes($link['attributes']);
  145.                 }
  146.                 $output .= '<span' . $span_attributes . '>' . $link['title'] . '</span>';
  147.             }
  148.  
  149.             $i++;
  150.             $output .= "</li>\n";
  151.         }
  152.  
  153.         $output .= '</ul>';
  154.     }
  155.  
  156.     return $output;
  157. }
  158.  
  159. // ........................
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement