You are here

function _multifield_entity_multifields_pseudo_entities_module_invoke in Multifield 7

Same name and namespace in other branches
  1. 7.2 multifield.module \_multifield_entity_multifields_pseudo_entities_module_invoke()

Run all of a given entity multifields' values through an entity hook.

The hook will be called with the following two parameters: 1. 'multifield' as the entity type 2. The pseudo-entity representing the multifield item value.

Parameters

string $entity_type: The entity type.

object $entity: The entity.

string $hook: The entity hook to invoke.

Throws

\EntityMalformedException

5 calls to _multifield_entity_multifields_pseudo_entities_module_invoke()
multifield_field_attach_delete in ./multifield.field.inc
Implements hook_field_attach_update().
multifield_field_attach_delete_revision in ./multifield.field.inc
Implements hook_field_attach_delete_revision().
multifield_field_attach_insert in ./multifield.field.inc
Implements hook_field_attach_insert().
multifield_field_attach_presave in ./multifield.field.inc
Implements hook_field_attach_presave().
multifield_field_attach_update in ./multifield.field.inc
Implements hook_field_attach_update().

File

./multifield.module, line 675

Code

function _multifield_entity_multifields_pseudo_entities_module_invoke($entity_type, $entity, $hook) {

  // It should not be possible to have multifields on a multifield entity
  // itself, so abort.
  if ($entity_type == 'multifield') {
    return;
  }
  list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  $multifields = multifield_get_fields();
  $instances = array_intersect_key(field_info_instances($entity_type, $bundle), $multifields);
  foreach (array_keys($instances) as $field_name) {
    $machine_name = $multifields[$field_name];
    if (!empty($entity->{$field_name})) {
      foreach ($entity->{$field_name} as $langcode => &$items) {

        // Gather the original field values from the parent entity if it has them.
        $original_items = !empty($entity->original->{$field_name}[$langcode]) ? $entity->original->{$field_name}[$langcode] : array();
        foreach ($items as $delta => &$item) {
          $pseudo_entity = _multifield_field_item_to_entity($machine_name, $item);

          // Add the original item value to the pseudo-entity.
          if (!empty($original_items[$delta])) {
            $pseudo_entity->original = _multifield_field_item_to_entity($machine_name, $original_items[$delta]);
          }

          // Invoke the hook.
          module_invoke_all($hook, 'multifield', $pseudo_entity);

          // Serialize the item value back.
          unset($pseudo_entity->original);
          $item = _multifield_field_entity_to_item($pseudo_entity);
        }
      }
    }
  }
}