function imagecache_actions_get_image_context in ImageCache Actions 7
Same name and namespace in other branches
- 8 utility.inc \imagecache_actions_get_image_context()
Returns an array with context information about the image.
This information is called by effects that need contextual information or that allow custom PHP:
- Custom action.
- Text from image alt or title.
- Text with tokens.
- Text from PHP code.
Parameters
object $image: The image object.
array $data: An associative array with the effect options.
Return value
array An associative array with context information about the image. It contains the following keys:
- effect_data: array with the effect data.
- managed_file: object|null, a managed file object for the current image. This may be (an extended) media/file_entity file object.
- referring_entities: array, list of (loaded) entities that have an image field referring to the managed file.
- entity: object|null, the 1st (and often only) entity that has an image field referring to the managed file.
- image_field: array|null, the (1st and often only) image field item of entity that refers to the managed file (single field item, not the whole field value).
2 calls to imagecache_actions_get_image_context()
- imagecache_customactions_effect in customactions/
imagecache_customactions.module - Image effect callback for the custom action effect.
- image_effects_text_get_text in image_effects_text/
image_effects_text.inc - Get the text to use for this image.
File
- ./
utility.inc, line 174 - utility.inc: uitility form, conversion and rendering functions for image processing.
Code
function imagecache_actions_get_image_context($image, $data) {
// Store context about the image.
$image_context = array(
'effect_data' => $data,
'managed_file' => NULL,
'referring_entities' => array(),
'entity' => NULL,
'image_field' => NULL,
);
// Find the managed file object (at most 1 object as 'uri' is a unique index).
$managed_file = file_load_multiple(array(), array(
'uri' => $image->source,
));
$managed_file = reset($managed_file);
if ($managed_file !== FALSE) {
$image_context['managed_file'] = $managed_file;
// And find the entities referring to this managed file, image fields first,
// then file fields (very specific use cases).
$references = file_get_file_references($managed_file, NULL, FIELD_LOAD_CURRENT, 'image') + file_get_file_references($managed_file, NULL, FIELD_LOAD_CURRENT, 'file');
if ($references) {
// Load referring entities., keep track of 1st reference separately.
$entity_type_first = '';
$field_name_first = '';
foreach ($references as $field_name => $field_references) {
foreach ($field_references as $entity_type => $entity_stubs) {
$entities = entity_load($entity_type, array_keys($entity_stubs));
if (!empty($entities)) {
$image_context['referring_entities'][$field_name][$entity_type] = $entities;
// Make it easy to access the '1st' entity and its referring image
// field, part 1: entity.
if (empty($entity_type_first)) {
$entity_type_first = $entity_type;
$field_name_first = $field_name;
$image_context['entity'] = reset($entities);
}
}
}
}
// Make it easy to access the '1st' entity and its referring image field,
// part 2: image field.
/** @var array|false $image_field */
$image_field = field_get_items($entity_type_first, $image_context['entity'], $field_name_first);
if ($image_field) {
// Get referring item.
foreach ($image_field as $image_field_value) {
if ($image_field_value['fid'] === $managed_file->fid) {
$image_context['image_field'] = $image_field_value;
break;
}
}
}
}
}
return $image_context;
}