template_preprocess_item_list

template_preprocess_item_list(&$variables)

Prepares variables for item list templates.

Default template: item-list.html.twig.

Parameters

array $variables: An associative array containing:

  • items: An array of items to be displayed in the list. Each item can be either a string or a render array. If #type, #theme, or #markup properties are not specified for child render arrays, they will be inherited from the parent list, allowing callers to specify larger nested lists without having to explicitly specify and repeat the render properties for all nested child lists.
  • title: A title to be prepended to the list.
  • list_type: The type of list to return (e.g. "ul", "ol").
  • wrapper_attributes: HTML attributes to be applied to the list wrapper.

See also

https://www.drupal.org/node/1842756

File

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

Code

function template_preprocess_item_list(&$variables) {
  $variables['wrapper_attributes'] = new Attribute($variables['wrapper_attributes']);
  foreach ($variables['items'] as &$item) {
    $attributes = array();
    // If the item value is an array, then it is a render array.
    if (is_array($item)) {
      // List items support attributes via the '#wrapper_attributes' property.
      if (isset($item['#wrapper_attributes'])) {
        $attributes = $item['#wrapper_attributes'];
      }
      // 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 item-list.html.twig 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
            // \Drupal\Core\Render\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['#list_type'] = $variables['list_type'];
        }
      }
    }

    // Set the item's value and attributes for the template.
    $item = array(
      'value' => $item,
      'attributes' => new Attribute($attributes),
    );
  }
}
doc_Drupal
2016-10-29 09:46:44
Comments
Leave a Comment

Please login to continue.