From 3a5a6bb7b8179ce002bcf414092012b555e8e087 Mon Sep 17 00:00:00 2001 From: Franck Deroche Date: Wed, 7 Dec 2011 07:49:51 +0100 Subject: [PATCH] Tweak content_get_nested_elements a lot - Change it to be non-recursive - Make it accept an array of field names - Use this in _content_field_invoke_default to prefetch the wrappers for all the fields in one pass. --- content.module | 60 ++++++++++++++++++++++++++++++++++++++++++------------- 1 files changed, 46 insertions(+), 14 deletions(-) diff --git a/content.module b/content.module index ea510c9..21e1f00 100644 --- a/content.module +++ b/content.module @@ -703,7 +703,7 @@ function content_associate_fields($module) { * ), * ); */ -function content_field($op, &$node, $field, &$items, $teaser, $page) { +function content_field($op, &$node, $field, &$items, $teaser, $page, $all_wrappers = NULL) { switch ($op) { case 'validate': // If the field is configured for multiple values and these are handled @@ -848,7 +848,12 @@ function content_field($op, &$node, $field, &$items, $teaser, $page) { // The location of the field's rendered output depends on whether the // field is in a fieldgroup or not. - $wrappers = content_get_nested_elements($node->content, $field['field_name']); + if (is_array($all_wrappers) && isset($all_wrappers[$field['field_name']])) { + $wrappers = $all_wrappers[$field['field_name']]; + } + else { + $wrappers = content_get_nested_elements($node->content, $field['field_name']); + } foreach ($wrappers as $wrapper) { $element = $wrapper['field']; // '#single' is not set if the field is hidden or inaccessible. @@ -878,7 +883,12 @@ function content_field($op, &$node, $field, &$items, $teaser, $page) { // The location of the field's rendered output depends on whether the // field is in a fieldgroup or not. - $wrappers = content_get_nested_elements($node->content, $field['field_name']); + if (is_array($all_wrappers) && isset($all_wrappers[$field['field_name']])) { + $wrappers = $all_wrappers[$field['field_name']]; + } + else { + $wrappers = content_get_nested_elements($node->content, $field['field_name']); + } foreach ($wrappers as $wrapper) { // '#children' is not set if the field is empty. $addition[$field['field_name'] .'_rendered'] .= isset($wrapper['#children']) ? $wrapper['#children'] : ''; @@ -1330,9 +1340,15 @@ function _content_field_invoke_default($op, &$node, $teaser = NULL, $page = NULL return content_storage($op, $node); } else { + if (in_array($op, array('alter', 'preprocess_node'))) { + $wrappers = content_get_nested_elements($node->content, array_keys($type['fields'])); + } + else { + $wrappers = NULL; + } foreach ($type['fields'] as $field) { $items = isset($node->$field['field_name']) ? $node->$field['field_name'] : array(); - $result = content_field($op, $node, $field, $items, $teaser, $page); + $result = content_field($op, $node, $field, $items, $teaser, $page, $wrappers); if (is_array($result)) { $return = array_merge($return, $result); } @@ -2800,19 +2816,35 @@ function content_inactive_instances($type_name = NULL) { * An array of all matching form elements, returned by reference. */ function &content_get_nested_elements(&$form, $field_name) { + if (!is_array($field_name)) { + $single = TRUE; + $names = array($field_name => $field_name); + } + else { + $single = FALSE; + $names = drupal_map_assoc($field_name); + } + $queue = array(&$form); $elements = array(); - foreach (element_children($form) as $key) { - if ($key === $field_name) { - $elements[] = &$form[$key]; - } - else if (is_array($form[$key])) { - $nested_form = &$form[$key]; - if ($sub_elements = &content_get_nested_elements($nested_form, $field_name)) { - $elements = array_merge($elements, $sub_elements); + while ($current = &content_shift($queue)) { + foreach (element_children($current) as $key) { + if (isset($names[$key])) { + $elements[$key][] = &$current[$key]; + } + else if (is_array($current[$key])) { + $queue[] = &$current[$key]; } } } - return $elements; + return $single ? $elements[$field_name] : $elements; +} + +function &content_shift(&$array) { + reset($array); + $k = key($array); + $v = &$array[$k]; + unset($array[$k]); + return $v; } /** @@ -2838,4 +2870,4 @@ function content_set_nested_elements(&$form, $field_name, $value) { } } return $success; -} \ No newline at end of file +} -- 1.7.5.4