This repository was archived by the owner on May 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcm_tools.element.inc
More file actions
63 lines (53 loc) · 1.44 KB
/
cm_tools.element.inc
File metadata and controls
63 lines (53 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
/**
* @file
* Render elements provided by CM Tools module.
*/
/**
* Implements hook_element_info().
*/
function cm_tools_element_info() {
$types = array();
/*
* Thin wrapper around #theme => 'item_list' which allows
* render arrays to be passed as '#items'.
*/
$types['cm_item_list'] = array(
'#theme' => 'item_list',
'#items' => array(),
'#list_type' => 'ul',
'#attributes' => array(),
'#pre_render' => array('cm_tools_item_list_pre_render'),
);
return $types;
}
/**
* Pre-render callback for the item_list element.
*
* We detect whether render arrays being passed under '#items'
* and if so, render them into strings.
*
* @param mixed $elements
* The elements to pre-render.
*
* @return mixed
* The pre-rendered elements.
*/
function cm_tools_item_list_pre_render($elements) {
// Need to replace the #type since theme('item_list')
// expects to find 'ul' or 'ol' there, not 'cm_item_list'!
$elements['#type'] = $elements['#list_type'];
// Sadly a bit of magic is necessary here since '#items'
// can legitimately be an array.
foreach ($elements['#items'] as &$item) {
// An item can be it's contents directly, or the
// contents can be found in the 'data' property.
if (is_array($item) && isset($item['data'])) {
$item = &$item['data'];
}
if (is_array($item) && !isset($item['children'])) {
$item = drupal_render($item);
}
}
return $elements;
}