item-list.func.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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(array $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. 'html' => TRUE,
  49. );
  50. }
  51. // Set defaults.
  52. $title += array(
  53. 'level' => 'h3',
  54. 'attributes' => array(),
  55. );
  56. // Heading outputs only when it has text.
  57. if (!empty($title['text'])) {
  58. $heading .= '<' . $title['level'] . drupal_attributes($title['attributes']) . '>';
  59. $heading .= empty($title['html']) ? check_plain($title['text']) : $title['text'];
  60. $heading .= '</' . $title['level'] . '>';
  61. }
  62. }
  63. $output = '';
  64. if ($items) {
  65. $output .= '<' . $type . drupal_attributes($list_attributes) . '>';
  66. foreach ($items as $key => $item) {
  67. $attributes = array();
  68. if (is_array($item)) {
  69. $value = '';
  70. if (isset($item['data'])) {
  71. // Allow data to be renderable.
  72. if (is_array($item['data']) && (!empty($item['data']['#type']) || !empty($item['data']['#theme']))) {
  73. $value .= drupal_render($item['data']);
  74. }
  75. else {
  76. $value .= $item['data'];
  77. }
  78. }
  79. $attributes = array_diff_key($item, array('data' => 0, 'children' => 0));
  80. // Append nested child list, if any.
  81. if (isset($item['children'])) {
  82. // HTML attributes for the outer list are defined in the 'attributes'
  83. // theme variable, but not inherited by children. For nested lists,
  84. // all non-numeric keys in 'children' are used as list attributes.
  85. $child_list_attributes = array();
  86. foreach ($item['children'] as $child_key => $child_item) {
  87. if (is_string($child_key)) {
  88. $child_list_attributes[$child_key] = $child_item;
  89. unset($item['children'][$child_key]);
  90. }
  91. }
  92. $build = array(
  93. '#theme' => 'item_list',
  94. '#items' => $item['children'],
  95. '#type' => $type,
  96. '#attributes' => $child_list_attributes,
  97. );
  98. $value .= drupal_render($build);
  99. }
  100. }
  101. else {
  102. $value = $item;
  103. }
  104. $output .= '<li' . drupal_attributes($attributes) . '>' . $value . "</li>\n";
  105. }
  106. $output .= "</$type>";
  107. }
  108. // Output the list and title only if there are any list items.
  109. if (!empty($output)) {
  110. $output = $heading . $output;
  111. }
  112. return $output;
  113. }