| Versionen | |
|---|---|
| drupal7 | _field_invoke_multiple($op, $obj_type, &$objects, &$a = NULL, &$b = NULL, $default = FALSE) |
Invoke a field operation across fields on multiple objects.
$op Possible operations include:
$obj_type The type of $object; e.g. 'node' or 'user'.
$objects An array of objects, keyed by object id.
$a
$b Currently always NULL.
$default
An array of returned values keyed by object id.
modules/
<?php
function _field_invoke_multiple($op, $obj_type, &$objects, &$a = NULL, &$b = NULL, $default = FALSE) {
$fields = array();
$grouped_instances = array();
$grouped_objects = array();
$grouped_items = array();
$return = array();
// Preparation:
// - Get the list of fields contained in the various bundles.
// - For each field, group the corresponding instances, objects and field
// values.
// - Initialize the return value for each object.
foreach ($objects as $object) {
list($id, $vid, $bundle) = field_attach_extract_ids($obj_type, $object);
foreach (field_info_instances($bundle) as $instance) {
$field_name = $instance['field_name'];
if (!isset($grouped_fields[$field_name])) {
$fields[$field_name] = field_info_field($field_name);
}
$grouped_instances[$field_name][$id] = $instance;
$grouped_objects[$field_name][$id] = &$objects[$id];
$grouped_items[$field_name][$id] = isset($object->$field_name) ? $object->$field_name : array();
}
$return[$id] = array();
}
// Call each field's operation.
foreach ($fields as $field_name => $field) {
if (!empty($field)) {
$function = $default ? 'field_default_' . $op : $field['module'] . '_field_' . $op;
if (drupal_function_exists($function)) {
$results = $function($obj_type, $grouped_objects[$field_name], $field, $grouped_instances[$field_name], $grouped_items[$field_name], $a, $b);
// Merge results by object.
if (isset($results)) {
foreach ($results as $id => $result) {
if (is_array($result)) {
$return[$id] = array_merge($return[$id], $result);
}
else {
$return[$id][] = $result;
}
}
}
}
}
// Populate field values back in the objects, but avoid replacing missing
// fields with an empty array (those are not equivalent on update).
foreach ($grouped_objects[$field_name] as $id => $object) {
if ($grouped_items[$field_name][$id] !== array() || property_exists($object, $field_name)) {
$object->$field_name = $grouped_items[$field_name][$id];
}
}
}
return $return;
}
?>
Kommentare
Kommentar hinzufügen