I wanted to see what API functions are available for field collections. I would assume there's a hook_field_collection_load(), a hook_field_collection_view_alter() and I nor other developers shouldn't be expected to dive into entity.module code and classes to figure out what hooks there are available to implement.

Comments

tim.plunkett’s picture

Category: feature » task

/me nods

kmhanser’s picture

I concur.

trying to figure out how field_collections render their output so I can alter it slightly before display, and I can't find where to hook in.

are there any hooks for field_collections? or should i just start hacking the module...?

tim.plunkett’s picture

Status: Active » Fixed

Actually, field_collection has no hooks or alters of its own.

Both the field collection and the field collection items are fields, so they are subject to the hooks of the Field API.

For example, if I have a content type named 'tour' with a field collection named 'field_tour_date', and I want to change the name of the 'edit' link, here is code you could use. (This uses hook_field_attach_view_alter().)

/**
 * Implements hook_field_attach_view_alter().
 */
function MYMODULE_field_attach_view_alter(&$output, $context) {
  $content_type = 'tour';
  $field_name = 'field_tour_date';

  if ($context['entity_type'] == 'node' && $context['entity']->type == $content_type) {
    foreach (element_children($output[$field_name]) as $delta) {
      if (isset($output[$field_name][$delta]['links']['#links']['edit'])) {
        $output[$field_name][$delta]['links']['#links']['edit']['title'] = t('Click here to edit');
      }
    }
  }
}

Obviously you could just change the edit text in the UI, but you could have certain cases you wanted to change it.

dave reid’s picture

That seems odd. There should be hooks like hook_field_collection_item_load/update/save/view right? Sometimes I don't want a hook that will get invoked for every entity load, but just for a specific entity.

tim.plunkett’s picture

Status: Fixed » Active

And I would have gotten away with it if it weren't for that meddling Dave Reid!

I forgot that Entity API provides hooks for every module that integrates with it.

tim.plunkett’s picture

  • hook_field_collection_item_load()
  • hook_field_collection_item_insert()
  • hook_field_collection_item_presave()
  • hook_field_collection_item_update()
  • hook_field_collection_item_delete()
  • hook_field_collection_item_view()
  • hook_field_collection_item_view_alter()

Most of this can be copied straight from http://drupalcode.org/project/profile2.git/blob/refs/heads/7.x-1.x:/prof...

jmuzz’s picture

Issue summary: View changes
Status: Active » Fixed

The documentation for these functions was added to field_collection.api.php somewhere along the way.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.