Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #1
dakalaI 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.
Comment #2
bjaspan CreditAttribution: bjaspan commented@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.
Comment #3
dakala@bjaspan: Thanks for the tips.
Comment #4
dakalaHere's what I've come up with. Now working on the test but can't figure out what to assert yet.
Comment #5
yched CreditAttribution: yched commentedHm, 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.
Comment #6
yched CreditAttribution: yched commentedActually, 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.
Comment #7
yched CreditAttribution: yched commented