Project

General

Profile

Revision 082b75eb

Added by Assos Assos over 6 years ago

Weekly update of contrib modules

View differences:

drupal7/sites/all/modules/file_entity/file_entity.tokens.inc
44 44
    'type' => 'url',
45 45
  );
46 46

  
47
  if (module_exists('token')) {
48
    $info['types']['file_field'] = array(
49
      'name' => t('Media'),
50
      'description' => t('Tokens related to a file_entity field.'),
51
      'hidden' => TRUE,
52
    );
53

  
54
    $default_text = ' ' . t('Defaults to first value.');
55

  
56
    $info['tokens']['file_field'] = array(
57
      'field' => array(
58
        'name' => t('Field token value'),
59
        'description' => t('Default: The value returned by the token field formatter.') . $default_text,
60
      ),
61
      'url' => array(
62
        'name' => t('URL'),
63
        'description' => t('URL of the file_entity resource.') . $default_text,
64
        'type' => 'array',
65
      ),
66
      'filename' => array(
67
        'name' => t('Filename'),
68
        'description' => t('Filename the file_entity resource.') . $default_text,
69
        'type' => 'array',
70
      ),
71
      'filemime' => array(
72
        'name' => t('MIME type'),
73
        'description' => t('MIME type of the file_entity resource.') . $default_text,
74
        'type' => 'array',
75
      ),
76
      'type' => array(
77
        'name' => t('File type'),
78
        'description' => t('File type of the file_entity resource.') . $default_text,
79
        'type' => 'array',
80
      ),
81
      'image' => array(
82
        'name' => t('Image'),
83
        'description' => t('URL of a representative image for the file_entity resource, e.g. a video thumbnail.') . $default_text,
84
        'type' => 'array',
85
      ),
86
      'height' => array(
87
        'name' => t('Height'),
88
        'description' => t('Height of the file_entity resource, for videos or images.') . $default_text,
89
        'type' => 'array',
90
      ),
91
      'width' => array(
92
        'name' => t('Width'),
93
        'description' => t('Width of the file_entity resource, for videos or images.') . $default_text,
94
        'type' => 'array',
95
      ),
96
      'https-url' => array(
97
        'name' => t('Secure URL'),
98
        'description' => t('URL of the file_entity resource using HTTPS.') . $default_text,
99
        'type' => 'array',
100
      ),
101
      'https-image' => array(
102
        'name' => t('Secure image'),
103
        'description' => t('URL of a representative image for the file_entity resource using HTTPS, usually for videos.') . $default_text,
104
        'type' => 'array',
105
      ),
106
    );
107

  
108
    $all_fields = field_info_field_map();
109
    foreach ($all_fields as $field_name => $field) {
110
      if ($field['type'] == 'file') {
111
        $field_info = _token_field_info($field_name);
112
        foreach (array_keys($field['bundles']) as $entity_type) {
113
          $info['tokens'][$entity_type][$field_name] = array(
114
            'name' => $field_info['label'],
115
            'description' => $field_info['description'],
116
            'type' => 'file_field',
117
            'module' => 'file_entity',
118
          );
119
        }
120
      }
121
    }
122
  }
123

  
47 124
  return $info;
48 125
}
49 126

  
......
55 132
}
56 133

  
57 134
/**
58
 * Implements hook_tokens().
135
 * Provide replacement values for placeholder tokens.
59 136
 */
60 137
function file_entity_tokens($type, $tokens, array $data = array(), array $options = array()) {
61 138
  $replacements = array();
62 139

  
140
  // Check that this token call contains the data we need
141
  if ($type == 'entity' && !empty($data['entity_type']) && !empty($data['entity']) &&
142
    !empty($data['token_type']) && module_exists('token')) {
143

  
144
    foreach ($tokens as $name => $original) {
145

  
146
      // Split out the token into its parts
147
      $parts = explode(':', $name, 3);
148

  
149
      $field_name    = $parts[0];
150
      $property      = (isset($parts[1])) ? $parts[1] : '';
151
      $array_handler = (isset($parts[2])) ? $parts[2] : '';
152

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

  
156
        // Get basic information
157
        $entity_type = $data['entity_type'];
158
        $langcode = isset($options['language']) ? $options['language']->language : NULL;
159
        $entity = clone $data['entity'];
160

  
161
        // If we are looking for the field output, let field module handle it
162
        if (empty($property) || $property == 'field') {
163
          unset($entity->_field_view_prepared);
164
          $field_output = field_view_field($entity_type, $entity, $field_name, 'token', $langcode);
165
          $field_output['#token_options'] = $options;
166
          $field_output['#prerender'][] = 'token_pre_render_field_token';
167
          $replacements[$original] = drupal_render($field_output);
168
        }
169
        else {
170
          $items = field_get_items($entity_type, $entity, $field_name);
171
          $return = _file_entity_tokens_get_property($items, $property, $array_handler);
172

  
173
          // We may get a single value or an array.
174
          // Handle array with the array function from token module.
175
          if (is_array($return)) {
176
            $search_tokens = token_find_with_prefix($tokens, $field_name);
177
            if ($array_tokens = token_find_with_prefix($search_tokens, $property)) {
178
              $replacements += token_generate('array', $array_tokens, array('array' => $return), $options);
179
            }
180
          }
181
          else {
182
            $replacements[$original] = $return;
183
          }
184
        }
185

  
186
        // Unset clone of entity
187
        unset($entity);
188
      }
189
    }
190
  }
191

  
63 192
  $url_options = array('absolute' => TRUE);
64 193
  if (isset($options['language'])) {
65 194
    $url_options['language'] = $options['language'];
......
131 260

  
132 261
  return $replacements;
133 262
}
263

  
264
/**
265
 * This helper function gets file properties for token replacement.
266
 *
267
 * @param array $files
268
 * An array of files that are values for the field.
269
 *
270
 * @param string $property
271
 * The property to retrieve from the file info. See file_entity_token_info() for
272
 * a list of properties.
273
 *
274
 * @param string $array_handler
275
 * The optional array modifier, e.g. "count" or "join:,".
276
 *
277
 * @return mixed
278
 * Either a single value, the first of the array, or an array of values.
279
 */
280
function _file_entity_tokens_get_property($files, $property, $array_handler = 'first') {
281

  
282
  // If we only need the first variable
283
  $return_first = ($array_handler == 'first' || empty($array_handler) || $array_handler == 'value:0');
284

  
285
  // This static variable stores image info
286
  $info = &drupal_static(__FUNCTION__);
287

  
288
  foreach ($files as $file) {
289
    $file['url'] = file_create_url($file['uri']);
290
    $file['https-url'] = str_replace('http://', 'https://', $file['url']);
291

  
292
    // If values are: filename, filemime, type, url, https-url
293
    if (isset($file[$property])) {
294
      $value = $file[$property];
295
    }
296

  
297
    // If values are: image, height, width, https-image
298
    elseif (!empty($info[$file['fid']])) {
299
      $value = $info[$file['fid']][$property] ?: NULL;
300
    }
301
    // If values are files types
302
    else {
303

  
304
      // If file type is image
305
      if ($file['type'] == 'image') {
306
        $imageuri = $file['uri'];
307
      }
308

  
309
      // If file type is video
310
      elseif ($file['type'] == 'video') {
311
        list($provider, $filename) = explode('://v/', $file['uri']);
312
        $imageuri = "public://file_entity-$provider/$filename.jpg";
313
      }
314

  
315
      // Do nothing for other file types
316
      // @todo: Other file types may need handling
317
      else {
318
        $imageuri = FALSE;
319
      }
320

  
321
      if ($info[$file['fid']] = image_get_info($imageuri)) {
322
        $info[$file['fid']]['image'] = file_create_url($imageuri);
323
        $info[$file['fid']]['https-image'] = str_replace('http://', 'https://', $info[$file['fid']]['image']);
324
      }
325

  
326
      $value = $info[$file['fid']][$property] ?: NULL;
327
    }
328

  
329
    if ($return_first) {
330
      return $value;
331
    }
332
    $values[] = $value;
333
  }
334

  
335
  return $values;
336
}

Also available in: Unified diff