Projet

Général

Profil

Paste
Télécharger (3,97 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / themes / bootstrap / templates / system / item-list.func.php @ eefc2ac0

1
<?php
2

    
3
/**
4
 * @file
5
 * Stub file for bootstrap_item_list().
6
 */
7

    
8
/**
9
 * Returns HTML for a list or nested list of items.
10
 *
11
 * - Uses an early D8 version of the theme function, which fixes bugs (and was
12
 *   refused for commit because it was "too late to change theme output)".
13
 * - Removes first/last, even/odd classes.
14
 * - Removes useless div.item-list wrapper, allows optional #wrapper_attributes.
15
 * - Removes hard-coded #title as <h3>, introduce support for #title as an array
16
 *   containing, text, tag and optional attributes.
17
 *
18
 * @param array $variables
19
 *   An associative array containing:
20
 *   - items: An array of items to be displayed in the list. If an item is a
21
 *     string, then it is used as is. If an item is an array, then the "data"
22
 *     element of the array is used as the contents of the list item. If an item
23
 *     is an array with a "children" element, those children are displayed in a
24
 *     nested list. All other elements are treated as attributes of the list
25
 *     item element.
26
 *   - title: The title of the list.
27
 *   - type: The type of list to return (e.g. "ul", "ol").
28
 *   - attributes: The attributes applied to the list element.
29
 *
30
 * @return string
31
 *   The constructed HTML.
32
 *
33
 * @see theme_item_list()
34
 *
35
 * @ingroup theme_functions
36
 */
37
function bootstrap_item_list(array $variables) {
38
  $items = $variables['items'];
39
  $title = $variables['title'];
40
  $type = $variables['type'];
41
  $list_attributes = $variables['attributes'];
42

    
43
  // Drupal core only supports #title as a string. This implementation supports
44
  // heading level, and attributes as well.
45
  $heading = '';
46
  if (!empty($title)) {
47
    // If it's a string, normalize into an array.
48
    if (is_string($title)) {
49
      $title = array(
50
        'text' => $title,
51
        'html' => TRUE,
52
      );
53
    }
54
    // Set defaults.
55
    $title += array(
56
      'level' => 'h3',
57
      'attributes' => array(),
58
    );
59
    // Heading outputs only when it has text.
60
    if (!empty($title['text'])) {
61
      $heading .= '<' . $title['level'] . drupal_attributes($title['attributes']) . '>';
62
      $heading .= empty($title['html']) ? check_plain($title['text']) : $title['text'];
63
      $heading .= '</' . $title['level'] . '>';
64
    }
65
  }
66

    
67
  $output = '';
68
  if ($items) {
69
    $output .= '<' . $type . drupal_attributes($list_attributes) . '>';
70
    foreach ($items as $key => $item) {
71
      $attributes = array();
72

    
73
      if (is_array($item)) {
74
        $value = '';
75
        if (isset($item['data'])) {
76
          // Allow data to be renderable.
77
          if (is_array($item['data']) && (!empty($item['data']['#type']) || !empty($item['data']['#theme']))) {
78
            $value .= drupal_render($item['data']);
79
          }
80
          else {
81
            $value .= $item['data'];
82
          }
83
        }
84
        $attributes = array_diff_key($item, array('data' => 0, 'children' => 0));
85

    
86
        // Append nested child list, if any.
87
        if (isset($item['children'])) {
88
          // HTML attributes for the outer list are defined in the 'attributes'
89
          // theme variable, but not inherited by children. For nested lists,
90
          // all non-numeric keys in 'children' are used as list attributes.
91
          $child_list_attributes = array();
92
          foreach ($item['children'] as $child_key => $child_item) {
93
            if (is_string($child_key)) {
94
              $child_list_attributes[$child_key] = $child_item;
95
              unset($item['children'][$child_key]);
96
            }
97
          }
98
          $build = array(
99
            '#theme' => 'item_list',
100
            '#items' => $item['children'],
101
            '#type' => $type,
102
            '#attributes' => $child_list_attributes,
103
          );
104
          $value .= drupal_render($build);
105
        }
106
      }
107
      else {
108
        $value = $item;
109
      }
110

    
111
      $output .= '<li' . drupal_attributes($attributes) . '>' . $value . "</li>\n";
112
    }
113
    $output .= "</$type>";
114
  }
115

    
116
  // Output the list and title only if there are any list items.
117
  if (!empty($output)) {
118
    $output = $heading . $output;
119
  }
120

    
121
  return $output;
122
}