1

I have difficulties with getting a render Date (ISO) field from code. The field has machine-name field_date. By default, the Date field has three formatter: "long", "medium" and "small". The default formatter for node is set to "medium". (I've checked that, if the field is displayed in usual node's view, it's formatted as it should be) Here's the code:

$node = node_load($nid);
$field_date = field_get_items('node', $node, 'field_date');
$render_field_date = field_view_value('node', $node, 'field_date', $field_date[0]);

But the output I get is in "long" variant. Drupal.org says that by default it uses "default_formatter" which is defined in date_field_formatter_info() (having inspected the code, I figured out that that 'default_formatter' actually means 'long'). Well, I want it to be formatted in "medium" way. I write these lines:

$render_field_date = field_view_value('node', $node, 'field_date', $field_date[0], array('type' => 'medium') );

but it's still in its "long". What is wrong with the code?

1 Answer 1

2

Solved that! I should have gone a bit deeper in the Date source code.

The solution is this line:

$render_field_date = field_view_value('node', $node, 'field_date', $field_date[0], array('type' => 'date_default', 'settings' => array('format_type' => 'medium') ) );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.