| 1 |
<?php
|
| 2 |
|
| 3 |
// Capture alter hooks of various types.
|
| 4 |
foreach (array(
|
| 5 |
'form' => array('a', 'form', 1),
|
| 6 |
// 'node' => array('object->content', 'node->content'), // not in core yet
|
| 7 |
'profile' => array('b->content', 'account->content', 2)
|
| 8 |
) as $type => $array) {
|
| 9 |
list($var, $parent, $n) = $array;
|
| 10 |
$args = array();
|
| 11 |
for ($i = 0; $i < $n; ++$i) { $args[] = '&$'. chr(ord('a') + $i); }
|
| 12 |
eval("function field_spotter_{$type}_alter(". implode(', ', $args) .") { _field_spotter_alter('$type', '$parent', \$$var); }");
|
| 13 |
}
|
| 14 |
|
| 15 |
/**
|
| 16 |
* Alter everything hook for Form API-like structures.
|
| 17 |
*/
|
| 18 |
function _field_spotter_alter($type, $parent, &$array) {
|
| 19 |
field_spotter_recurse($array, $parent);
|
| 20 |
|
| 21 |
drupal_add_js(drupal_get_path('module', 'field_spotter') .'/field_spotter.js');
|
| 22 |
drupal_add_css(drupal_get_path('module', 'field_spotter') .'/field_spotter.css');
|
| 23 |
}
|
| 24 |
|
| 25 |
/**
|
| 26 |
* Add wrappers to each element in the array recursively.
|
| 27 |
*/
|
| 28 |
function field_spotter_recurse(&$array, $parent = '') {
|
| 29 |
static $ignore;
|
| 30 |
if (!isset($ignore)) {
|
| 31 |
$ignore = drupal_map_assoc(array('hidden'));
|
| 32 |
}
|
| 33 |
foreach (element_children($array) as $key) {
|
| 34 |
$item = &$array[$key];
|
| 35 |
if (isset($item['#type']) && isset($ignore[$item['#type']])) continue;
|
| 36 |
$id = $parent ? $parent .'[\''. addslashes($key) .'\']' : $key;
|
| 37 |
$prefix = '<span class="field-spotter" formkey="'. check_plain($id) .'">';
|
| 38 |
$suffix = '</span>';
|
| 39 |
$item['#prefix'] = isset($item['#prefix']) ? $prefix . $item['#prefix'] : $prefix;
|
| 40 |
$item['#suffix'] = isset($item['#suffix']) ? $item['#suffix'] . $suffix : $suffix;
|
| 41 |
field_spotter_recurse($item, $id);
|
| 42 |
}
|
| 43 |
}
|