function template_preprocess_item_list

Preprocesses variables for theme_item_list().

Parameters

array $variables: An associative array containing theme variables for theme_item_list(). 'items' in variables will be processed to automatically inherit the variables of this list to any possibly contained nested lists that do not specify custom render properties. This allows callers to specify larger nested lists, without having to explicitly specify and repeat the render properties for all nested child lists.

Related topics

File

drupal/core/includes/theme.inc, line 2158
The theme system, which controls the output of Drupal.

Code

function template_preprocess_item_list(&$variables) {
  foreach ($variables['items'] as &$item) {

    // If the item value is an array, then it is a render array.
    if (is_array($item)) {

      // Determine whether there are any child elements in the item that are not
      // fully-specified render arrays. If there are any, then the child
      // elements present nested lists and we automatically inherit the render
      // array properties of the current list to them.
      foreach (element_children($item) as $key) {
        $child =& $item[$key];

        // If this child element does not specify how it can be rendered, then
        // we need to inherit the render properties of the current list.
        if (!isset($child['#type']) && !isset($child['#theme']) && !isset($child['#markup'])) {

          // Since theme_item_list() supports both strings and render arrays as
          // items, the items of the nested list may have been specified as the
          // child elements of the nested list, instead of #items. For
          // convenience, we automatically move them into #items.
          if (!isset($child['#items'])) {

            // This is the same condition as in element_children(), which cannot
            // be used here, since it triggers an error on string values.
            foreach ($child as $child_key => $child_value) {
              if ($child_key[0] !== '#') {
                $child['#items'][$child_key] = $child_value;
                unset($child[$child_key]);
              }
            }
          }

          // Lastly, inherit the original theme variables of the current list.
          $child['#theme'] = $variables['theme_hook_original'];
          $child['#type'] = $variables['type'];
        }
      }
    }
  }
}