You are here

function pdf_to_image_tokens in PDF to ImageField 7.3

Fills in a token with the filename of the source pdf.

For use with filefield_sources mostly. Not very well-structured here yet.

Implements hook_tokens().

File

./pdf_to_image.module, line 877
Generates thumbnail image(s) from an uploaded PDF.

Code

function pdf_to_image_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $sanitize = !empty($options['sanitize']);
  $replacements = array();
  if ($type == 'entity' && !empty($data['entity'])) {
    $entity = $data['entity'];
    $entity_type = $data['entity_type'];
    list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);

    // Find any fields on this entity
    // that may be source files for a PDF conversion.
    $pdf_fields = pdf_to_image_source_fields($entity_type, $bundle);
    foreach ($pdf_fields as $field_id => $field) {
      $source_values = field_get_items($entity_type, $entity, $field_id);
      if (!empty($source_values)) {

        // Assumed to be multiple.
        foreach ($source_values as $delta => $file_info) {

          // Fall-through. We've found at least one $file_info now.
          // Messy, but dunno what to do with multiple sources.
        }
      }
    }

    // This looks like a slow way to do things, no?
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'pdf-source-filename':
          $info = pathinfo($file_info['filename']);
          $replacements[$original] = $info['filename'];
          break;
      }
    }
  }
  return $replacements;
}