You are here

function imagecache_actions_get_image_context in ImageCache Actions 8

Same name and namespace in other branches
  1. 7 utility.inc \imagecache_actions_get_image_context()

Return 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.
      foreach ($references as $field_name => $field_references) {
        foreach ($field_references as $entity_type => $entity_stubs) {
          $image_context['referring_entities'][$field_name][$entity_type] = entity_load($entity_type, array_keys($entity_stubs));
        }
      }

      // Make it easy to access the '1st' entity and its referring image field.
      reset($image_context['referring_entities']);
      list($field_name, $field_references) = each($image_context['referring_entities']);
      reset($field_references);
      list($entity_type, $entities) = each($field_references);
      reset($entities);
      list(, $image_context['entity']) = each($entities);

      /** @var array|false $image_field */
      $image_field = field_get_items($entity_type, $image_context['entity'], $field_name);
      if ($image_field) {

        // Get referring item.
        foreach ($image_field as $image_field_value) {
          if ($image_field_value['fid'] === $managed_file->fid) {

            // @todo: file_field as well or just ignore the type of field?
            $image_context['image_field'] = $image_field_value;
            break;
          }
        }
      }
    }
  }
  return $image_context;
}