Posted by crea on May 27, 2010 at 7:10pm
5 followers
| Project: | Date |
| Version: | 6.x-2.x-dev |
| Component: | Date Field |
| Category: | bug report |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | needs review |
Issue Summary
I have a date field with both from and to values and number of values = 1, and I have to print the field separately from the node template. So in "display values" settings I have "exclude" checkbox turned on for the field.
However, I found that printing the field using content_format() function like I'm used to doesn't work. After some troubleshooting I've found that theme_date_display_combination() has a bug inside (or it's too smart to be a generic function). It has following block of code:
<?php
// If this is a full node or a pseudo node created by grouping
// multiple values, see exactly which values are supposed to be visible.
if (isset($node->$field_name)) {
$node = date_prepare_node($node, $field, $type_name, $context, $options);
// Did the current value get removed by formatter settings?
if (empty($node->{$field_name}[$item['#delta']])) {
return $output;
}
// Adjust the $element values to match the changes.
$element['#node'] = $node;
}
?>I've found that in my case $item array doesn't have "#delta" key, so the function terminates without printing anything.
Comments
#1
I found the same exact bug, and did a backtrace to find where exactly the
$item['#delta']comes from -- and therefore, why it isn't appearing in our$item.First, my use case:
I'm pulling up a node through
node_load()and rendering the field like this:<?php$node = node_load($nid);
return content_format('field_date', $node->field_date[0], 'default', $node);
?>
Second, the problem:
$item['#delta']is actually first set in the following code snippet fromcontent_field($op='view'):<?phpif ($single) {
foreach ($items as $delta => $item) {
$element['items'][$delta] += $format_info;
$element['items'][$delta]['#item']['#delta'] = $delta;
}
}
?>
This
content_field($op='view')is called in only two places (as far as I can tell).First place that
content_field($op='view')is called:content_view_field()Second place that
content_field($op='view')is called:_content_field_invoke_default($op='view'), which is called by...content_view(), which is CCK's implementation of hook_nodeapi's $op='view'Could we fix our problem by calling
content_view_field()orcontent_view()instead? No, because:content_view_field()requires the field definition, and we don't have the field definition when we're pulling it in throughnode_load().hook_nodeapi($op='view')are only triggered when the page is actually being loaded for viewing -- not throughnode_load().content_field($op='view')in some applications -- especially when it's going to be needed over multiple nodes.So, we need to accept that there are legitimate use cases where
$item['#delta']will not be set.Finally, the solution:
Checking if
isset($item['#delta'])ought to be sufficient to avoid the unwanted triggering of thereturn $output;in the next line. That's what the attached patch does.#2
Marking as needs review.
#3
Updating the title to be simpler and more to the point.
#4
patch in #1 works for me
#5
I can also confirm that this resolves the issue presented.
Applied the patch from #1 and added the following to theme_preprocess_node
<?php$vars['event_times'] = content_format('field_event_datetime', $vars['field_event_datetime'][0], 'default', $vars);
?>