Projet

Général

Profil

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

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

1
<?php
2
/**
3
 * @file
4
 * Stub file for bootstrap_item_list().
5
 */
6

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

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

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

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

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

    
108
      $output .= '<li' . drupal_attributes($attributes) . '>' . $value . "</li>\n";
109
    }
110
    $output .= "</$type>";
111
  }
112

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

    
118
  return $output;
119
}