item-list.func.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * @file
  4. * Stub file for bootstrap_item_list().
  5. */
  6. /**
  7. * Returns HTML for a list or nested list of items.
  8. *
  9. * - Uses an early D8 version of the theme function, which fixes bugs (and was
  10. * refused for commit because it was "too late to change theme output)".
  11. * - Removes first/last, even/odd classes.
  12. * - Removes useless div.item-list wrapper, allows optional #wrapper_attributes.
  13. * - Removes hard-coded #title as <h3>, introduce support for #title as an array
  14. * containing, text, tag and optional attributes.
  15. *
  16. * @param array $variables
  17. * An associative array containing:
  18. * - items: An array of items to be displayed in the list. If an item is a
  19. * string, then it is used as is. If an item is an array, then the "data"
  20. * element of the array is used as the contents of the list item. If an item
  21. * is an array with a "children" element, those children are displayed in a
  22. * nested list. All other elements are treated as attributes of the list
  23. * item element.
  24. * - title: The title of the list.
  25. * - type: The type of list to return (e.g. "ul", "ol").
  26. * - attributes: The attributes applied to the list element.
  27. *
  28. * @return string
  29. * The constructed HTML.
  30. *
  31. * @see theme_item_list()
  32. *
  33. * @ingroup theme_functions
  34. */
  35. function bootstrap_item_list($variables) {
  36. $items = $variables['items'];
  37. $title = $variables['title'];
  38. $type = $variables['type'];
  39. $list_attributes = $variables['attributes'];
  40. // Drupal core only supports #title as a string. This implementation supports
  41. // heading level, and attributes as well.
  42. $heading = '';
  43. if (!empty($title)) {
  44. // If it's a string, normalize into an array.
  45. if (is_string($title)) {
  46. $title = array(
  47. 'text' => $title,
  48. );
  49. }
  50. // Set defaults.
  51. $title += array(
  52. 'level' => 'h3',
  53. 'attributes' => array(),
  54. );
  55. // Heading outputs only when it has text.
  56. if (!empty($title['text'])) {
  57. $heading .= '<' . $title['level'] . drupal_attributes($title['attributes']) . '>';
  58. $heading .= empty($title['html']) ? check_plain($title['text']) : $title['text'];
  59. $heading .= '</' . $title['level'] . '>';
  60. }
  61. }
  62. $output = '';
  63. if ($items) {
  64. $output .= '<' . $type . drupal_attributes($list_attributes) . '>';
  65. foreach ($items as $key => $item) {
  66. $attributes = array();
  67. if (is_array($item)) {
  68. $value = '';
  69. if (isset($item['data'])) {
  70. // Allow data to be renderable.
  71. if (is_array($item['data']) && (!empty($item['data']['#type']) || !empty($item['data']['#theme']))) {
  72. $value .= drupal_render($item['data']);
  73. }
  74. else {
  75. $value .= $item['data'];
  76. }
  77. }
  78. $attributes = array_diff_key($item, array('data' => 0, 'children' => 0));
  79. // Append nested child list, if any.
  80. if (isset($item['children'])) {
  81. // HTML attributes for the outer list are defined in the 'attributes'
  82. // theme variable, but not inherited by children. For nested lists,
  83. // all non-numeric keys in 'children' are used as list attributes.
  84. $child_list_attributes = array();
  85. foreach ($item['children'] as $child_key => $child_item) {
  86. if (is_string($child_key)) {
  87. $child_list_attributes[$child_key] = $child_item;
  88. unset($item['children'][$child_key]);
  89. }
  90. }
  91. $value .= theme('item_list', array(
  92. 'items' => $item['children'],
  93. 'type' => $type,
  94. 'attributes' => $child_list_attributes,
  95. ));
  96. }
  97. }
  98. else {
  99. $value = $item;
  100. }
  101. $output .= '<li' . drupal_attributes($attributes) . '>' . $value . "</li>\n";
  102. }
  103. $output .= "</$type>";
  104. }
  105. // Output the list and title only if there are any list items.
  106. if (!empty($output)) {
  107. $output = $heading . $output;
  108. }
  109. return $output;
  110. }