You are here

function file_entity_tokens in File Entity (fieldable files) 7.3

Same name and namespace in other branches
  1. 8.2 file_entity.tokens.inc \file_entity_tokens()
  2. 7 file_entity.tokens.inc \file_entity_tokens()
  3. 7.2 file_entity.tokens.inc \file_entity_tokens()

Provide replacement values for placeholder tokens.

File

./file_entity.tokens.inc, line 140
Token integration for the file_entity module.

Code

function file_entity_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();

  // Check that this token call contains the data we need
  if ($type == 'entity' && !empty($data['entity_type']) && !empty($data['entity']) && !empty($data['token_type']) && module_exists('token')) {
    foreach ($tokens as $name => $original) {

      // Split out the token into its parts
      $parts = explode(':', $name, 3);
      $field_name = $parts[0];
      $property = isset($parts[1]) ? $parts[1] : '';
      $array_handler = isset($parts[2]) ? $parts[2] : '';

      // Check that the field has content and that we should handle it
      if (!empty($data['entity']->{$field_name}) && _token_module($data['token_type'], $field_name) == 'file_entity') {

        // Get basic information
        $entity_type = $data['entity_type'];
        if ($entity_type == 'taxonomy_term') {
          $entity_type = 'term';
        }
        $langcode = isset($options['language']) ? $options['language']->language : NULL;
        $entity = clone $data['entity'];

        // If we are looking for the field output, let field module handle it
        if (empty($property) || $property == 'field') {
          unset($entity->_field_view_prepared);
          $field_output = field_view_field($entity_type, $entity, $field_name, 'token', $langcode);
          $field_output['#token_options'] = $options;
          $field_output['#pre_render'][] = 'token_pre_render_field_token';
          $replacements[$original] = drupal_render($field_output);
        }
        else {
          $items = field_get_items($entity_type, $entity, $field_name);
          $return = _file_entity_tokens_get_property($items, $property, $array_handler);

          // We may get a single value or an array.
          // Handle array with the array function from token module.
          if (is_array($return)) {
            $search_tokens = token_find_with_prefix($tokens, $field_name);
            if ($array_tokens = token_find_with_prefix($search_tokens, $property)) {
              $replacements += token_generate('array', $array_tokens, array(
                'array' => $return,
              ), $options);
            }
          }
          else {
            if (!is_null($return)) {
              $replacements[$original] = $return;
            }
          }
        }

        // Unset clone of entity
        unset($entity);
      }
    }
  }
  $url_options = array(
    'absolute' => TRUE,
  );
  if (isset($options['language'])) {
    $url_options['language'] = $options['language'];
    $language_code = $options['language']->language;
  }
  else {
    $language_code = NULL;
  }
  $sanitize = !empty($options['sanitize']);

  // File tokens.
  if ($type == 'file' && !empty($data['file'])) {
    $file = $data['file'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'type':
          if ($file_type = file_type_load($file->type)) {
            $replacements[$original] = $sanitize ? check_plain($file_type->label) : $file_type->label;
          }
          break;
        case 'download-url':
          $uri = file_entity_download_uri($file);
          $replacements[$original] = url($uri['path'], $uri['options'] + $url_options);
          break;
      }
    }

    // Chained token relationships.
    if (($file_type_tokens = token_find_with_prefix($tokens, 'type')) && ($file_type = file_type_load($file->type))) {
      $replacements += token_generate('file-type', $file_type_tokens, array(
        'file_type' => $file_type,
      ), $options);
    }
    if ($download_url_tokens = token_find_with_prefix($tokens, 'download-url')) {
      $replacements += token_generate('url', $download_url_tokens, file_entity_download_uri($file), $options);
    }
  }

  // File type tokens.
  if ($type == 'file-type' && !empty($data['file_type'])) {
    $file_type = $data['file_type'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'name':
          $replacements[$original] = $sanitize ? check_plain($file_type->label) : $file_type->label;
          break;
        case 'machine-name':

          // This is a machine name so does not ever need to be sanitized.
          $replacements[$original] = $file_type->type;
          break;
        case 'count':
          $query = db_select('file_managed');
          $query
            ->condition('type', $file_type->type);
          $query
            ->addTag('file_type_file_count');
          $count = $query
            ->countQuery()
            ->execute()
            ->fetchField();
          $replacements[$original] = (int) $count;
          break;
        case 'edit-url':
          $replacements[$original] = url('admin/structure/file-types/manage/' . $file_type->type . '/fields', $url_options);
          break;
      }
    }
  }
  return $replacements;
}