You are here

function textimage_field_formatter_view in Textimage 7.3

Same name and namespace in other branches
  1. 7.2 textimage.module \textimage_field_formatter_view()

Implements hook_field_formatter_view().

File

./textimage.module, line 557
Textimage - Provides text to image manipulations.

Code

function textimage_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {

  // If formatting a node, store entity for passing to theme.
  // The node entity will be used for the detokening of text.
  $node = $entity_type == 'node' ? $entity : NULL;

  // Check if the formatter involves a link.
  $href = NULL;
  if ($image_link_setting = $display['settings']['image_link']) {
    switch ($image_link_setting) {
      case 'content':
        $uri = entity_uri($entity_type, $entity);
        $href = $uri['path'];
        break;
      case 'file':
        $href = '#textimage_derivative_url#';
        break;
    }
  }
  $element = array();
  if ($field['module'] == 'text') {

    // Get sanitized text strings from a text field.
    $text = TextimageImager::getTextFieldText($items, $field, $instance, $node);
    if ($field['cardinality'] != 1 && $display['settings']['image_text_values'] == 'itemize') {

      // Build separate image for each text value.
      foreach ($text as $text_value) {
        $element[] = array(
          '#theme' => 'textimage_formatter',
          '#style_name' => $display['settings']['image_style'],
          '#text' => $text_value,
          '#node' => $node,
          '#alt' => $display['settings']['image_alt'],
          '#title' => $display['settings']['image_title'],
          '#href' => $href,
        );
      }
    }
    else {

      // Build single image with all text values.
      $element[] = array(
        '#theme' => 'textimage_formatter',
        '#style_name' => $display['settings']['image_style'],
        '#text' => $text,
        '#node' => $node,
        '#alt' => $display['settings']['image_alt'],
        '#title' => $display['settings']['image_title'],
        '#href' => $href,
      );
    }
  }
  elseif ($field['module'] == 'image') {

    // Get source image from an image field.
    foreach ($items as $delta => $item) {
      $source_image_file = file_load($item['fid']);
      $element[$delta] = array(
        '#theme' => 'textimage_formatter',
        '#style_name' => $display['settings']['image_style'],
        '#text' => NULL,
        '#node' => $node,
        '#source_image_file' => $source_image_file,
        '#force_hashed_filename' => TRUE,
        '#alt' => $display['settings']['image_alt'],
        '#title' => $display['settings']['image_title'],
        '#href' => $href,
      );
    }
  }
  return $element;
}