You are here

function imagefield_tokens_field_formatter_prepare_view in ImageField Tokens 7

Implements hook_field_formatter_prepare_view().

File

./imagefield_tokens.field.inc, line 121
Implements additional formatter for ImageField Tokens.

Code

function imagefield_tokens_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
  foreach ($entities as $id => $entity) {
    if (!empty($items[$id][0]) && isset($items[$id][0]['is_default'])) {
      if (!empty($instances[$id]['settings']['alt_field']) && !empty($instances[$id]['settings']['alt_field_default'])) {
        $alt_field_default = $instances[$id]['settings']['alt_field_default'];
      }
      if (!empty($instances[$id]['settings']['title_field']) && !empty($instances[$id]['settings']['title_field_default'])) {
        $title_field_default = $instances[$id]['settings']['title_field_default'];
      }
    }

    // Truncate fields to max allowed length.
    // alt=512, title=1024 per image.install.
    if (!empty($alt_field_default)) {
      $alt_field_default = token_replace($alt_field_default, [
        $entity_type => $entity,
      ], [
        'sanitize' => TRUE,
        'clear' => TRUE,
      ]);
      $alt_field_default = truncate_utf8($alt_field_default, 1024, TRUE, TRUE);
      $alt_field_default = htmlspecialchars_decode($alt_field_default, ENT_QUOTES | ENT_HTML401);
    }
    if (!empty($title_field_default)) {
      $title_field_default = token_replace($title_field_default, [
        $entity_type => $entity,
      ], [
        'sanitize' => TRUE,
        'clear' => TRUE,
      ]);
      $title_field_default = truncate_utf8($title_field_default, 1024, TRUE, TRUE);
      $title_field_default = htmlspecialchars_decode($title_field_default, ENT_QUOTES | ENT_HTML401);
    }
    if (isset($items[$id][0]['alt']) && isset($alt_field_default)) {
      $items[$id][0]['alt'] = $alt_field_default;
    }
    if (isset($items[$id][0]['title']) && isset($title_field_default)) {
      $items[$id][0]['title'] = $title_field_default;
    }
  }
}