Skip to content

Instantly share code, notes, and snippets.

@martsie
Created January 25, 2017 10:50
Show Gist options
  • Save martsie/04e5a9983305fa0aba54ebb9b00b71e7 to your computer and use it in GitHub Desktop.
Save martsie/04e5a9983305fa0aba54ebb9b00b71e7 to your computer and use it in GitHub Desktop.
Overriding theme_item_list in Drupal 7
<?php
function my_awesome_theme_item_list($variables) {
$items = $variables['items'];
$title = $variables['title'];
$type = $variables['type'];
$attributes = $variables['attributes'];
// Only output the list container and title, if there are any list items.
// Check to see whether the block title exists before adding a header.
// Empty headers are not semantic and present accessibility challenges.
$output = '<div class="item-list">';
if (isset($title) && $title !== '') {
$output .= '<h3>' . $title . '</h3>';
}
if (!empty($items)) {
$output .= "<$type" . drupal_attributes($attributes) . '>';
$num_items = count($items);
foreach ($items as $i => $item) {
$attributes = array();
$children = array();
$data = '';
if (is_array($item)) {
foreach ($item as $key => $value) {
if ($key == 'data') {
$data = $value;
}
elseif ($key == 'children') {
$children = $value;
}
else {
$attributes[$key] = $value;
}
}
}
else {
$data = $item;
}
if (count($children) > 0) {
// Render nested list.
$data .= theme_item_list(array('items' => $children, 'title' => NULL, 'type' => $type, 'attributes' => $attributes));
}
if ($i == 0) {
$attributes['class'][] = 'first';
}
if ($i == $num_items - 1) {
$attributes['class'][] = 'last';
}
// Add a span inside each list item.
$output .= "<li" .="" drupal_attributes($attributes)="" '=""><span class="my-special-span">' . $data . "</span>\n";
}
$output .= "<!--$type-->";
}
$output .= '</li"></div>';
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment