field_view_field() API function has custom code to call the 'sanitize' and 'view' field operation on a single field.
It could use the recent _field_invoke($options = array('field_name' => $field_name)) instead.

The function could also use a test.

Nice task a 'Fields in Core initiative' contributor :-)

Comments

dakala’s picture

Assigned: Unassigned » dakala

I think I can help with this too, with some help please. The relevant function in field.module is field_view_field() and this is how I think this change can be done:

1. Check that $object->$field['field_name'] is set
2. call _field_invoke('sanitize)
3. call _field_invoke('view') to get $view
4. assign $view[$field['field_name']] to $output

But I couldn't find any calls to the function anywhere so I really won't be able to test that my code works as expected.

bjaspan’s picture

@dakala: Your approach sounds correct. I don't think you need step 1 b/c _field_invoke does that.

It looks like you are right that there is no test for this function, and none of the function's normal callers (e.g. Views) exist for D7 yet. Therefore, as yched says, the function needs a test. So, this is a great opportunity for you to learn how to write D7 tests!

Look in field.test for examples. FieldAttachOtherTestCase has some tests for field_attach_view(). This isn't quite the same, as that function returns renderable content instead of rendered content, but it will still show you how to write a test that creates a field, instance, and entity, renders the entity, and verifies that it contains the right thing.

dakala’s picture

@bjaspan: Thanks for the tips.

dakala’s picture

Here's what I've come up with. Now working on the test but can't figure out what to assert yet.


function field_view_field($obj_type, $object, $build_mode = 'full') {
  $output = '';
  if (isset($object->$field['field_name'])) {
    $null = NULL;
    $options = array('field_name' => $object->$field['field_name']);

    _field_invoke('sanitize', $obj_type, $object, $null, $null, $options);

    $output = _field_invoke('view', $obj_type, $object, $build_mode, $null, $options);
  }

  return $output;
}

yched’s picture

Status: Active » Closed (duplicate)

Hm, embarrassing, but I think actually this whole task is a no go.

The aim of field_view_field() is to let the caller specify custom display settings. See #612894: field_format() is just a pile of code that doesn't work for more details.
If we use _field_invoke('view') here, the display settings will be taken from the $instance definition loaded inside _field_invoke(). No good.

Really sorry to realize this after you spent time on the task, dakala :-(.
Marking as a duplicate of the issue mentioned above.

yched’s picture

Actually, this is more a 'won't fix' than a 'duplicate'.
Let's move discussion over to #612894: field_format() is just a pile of code that doesn't work.

yched’s picture

Status: Closed (duplicate) » Closed (won't fix)