Révision d1c64ea8
Ajouté par Assos Assos il y a plus de 6 ans
drupal7/sites/all/modules/file_entity/file_entity.file.inc | ||
---|---|---|
176 | 176 |
// Add alt and title text to images. |
177 | 177 |
file_entity_set_title_alt_properties($files); |
178 | 178 |
|
179 |
// Load and unserialize metadata. |
|
179 |
// Add metadata to each file. |
|
180 |
foreach ($files as $file) { |
|
181 |
$file->metadata = array(); |
|
182 |
} |
|
180 | 183 |
$results = db_query("SELECT * FROM {file_metadata} WHERE fid IN (:fids)", array(':fids' => array_keys($files))); |
181 | 184 |
|
182 | 185 |
foreach ($results as $result) { |
... | ... | |
200 | 203 |
file_entity_set_title_alt_properties($files); |
201 | 204 |
} |
202 | 205 |
|
206 |
/** |
|
207 |
* Implements hook_entity_load(). |
|
208 |
*/ |
|
209 |
function file_entity_entity_load($entities, $entity_type) { |
|
210 |
file_entity_set_title_alt_properties_on_file_fields($entities, $entity_type); |
|
211 |
} |
|
212 |
|
|
213 |
/** |
|
214 |
* Implements hook_entitycache_load(). |
|
215 |
*/ |
|
216 |
function file_entity_entitycache_load($entities, $entity_type) { |
|
217 |
file_entity_set_title_alt_properties_on_file_fields($entities, $entity_type); |
|
218 |
} |
|
219 |
|
|
220 |
/** |
|
221 |
* Sets the title / alt properties on file fields attached to entities. |
|
222 |
* |
|
223 |
* Files attached to a file or image field can be stored in the field cache or |
|
224 |
* entity cache for whichever entity that the field is attached to. Because |
|
225 |
* $file->alt and $file->title are set in file_entity_file_load() based on the |
|
226 |
* current page language, they will go into the cache with that language as |
|
227 |
* well. To ensure that the correct language is used when the entity is later |
|
228 |
* loaded and displayed in a different language, the alt and title properties |
|
229 |
* can be set again using this function. |
|
230 |
* |
|
231 |
* @param array $entities |
|
232 |
* An array of entity objects of the same type. |
|
233 |
* @param string $entity_type |
|
234 |
* The type of entity. |
|
235 |
* |
|
236 |
* @see file_entity_entity_load() |
|
237 |
* @see file_entity_entitycache_load() |
|
238 |
*/ |
|
239 |
function file_entity_set_title_alt_properties_on_file_fields($entities, $entity_type) { |
|
240 |
foreach ($entities as $entity) { |
|
241 |
list(, , $bundle) = entity_extract_ids($entity_type, $entity); |
|
242 |
foreach (field_info_instances($entity_type, $bundle) as $instance) { |
|
243 |
if (!empty($entity->{$instance['field_name']})) { |
|
244 |
foreach ($entity->{$instance['field_name']} as &$items) { |
|
245 |
foreach ($items as &$item) { |
|
246 |
// We need to detect any field items that passed through |
|
247 |
// file_field_load(), whether they are files, images, or something |
|
248 |
// else. There is no direct way to do that, but checking for a few |
|
249 |
// expected file properties on the field item should be sufficient. |
|
250 |
if (is_array($item) && !empty($item['fid']) && isset($item['uri']) && isset($item['filename'])) { |
|
251 |
$file = (object) $item; |
|
252 |
file_entity_set_title_alt_properties(array($file)); |
|
253 |
$item = (array) $file; |
|
254 |
} |
|
255 |
} |
|
256 |
} |
|
257 |
} |
|
258 |
} |
|
259 |
} |
|
260 |
} |
|
261 |
|
|
203 | 262 |
/** |
204 | 263 |
* Set the title / alt properties of file objects. |
205 | 264 |
* |
206 |
* @param array $files List of file entities. |
|
265 |
* @param array $files |
|
266 |
* List of file entities. |
|
267 |
* @param stdClass $language |
|
268 |
* (optional) A language object to use for translating the title and alt |
|
269 |
* properties. Defaults to the language of the current request. |
|
207 | 270 |
*/ |
208 |
function file_entity_set_title_alt_properties($files) { |
|
271 |
function file_entity_set_title_alt_properties($files, $language = NULL) { |
|
272 |
if (!isset($language)) { |
|
273 |
$language = $GLOBALS['language']; |
|
274 |
} |
|
275 |
|
|
209 | 276 |
$alt = variable_get('file_entity_alt', '[file:field_file_image_alt_text]'); |
210 | 277 |
$title = variable_get('file_entity_title', '[file:field_file_image_title_text]'); |
211 | 278 |
|
212 | 279 |
$replace_options = array( |
213 | 280 |
'clear' => TRUE, |
214 | 281 |
'sanitize' => FALSE, |
215 |
'language' => $GLOBALS['language'],
|
|
282 |
'language' => $language,
|
|
216 | 283 |
); |
217 | 284 |
|
218 | 285 |
foreach ($files as $file) { |
219 |
$file->metadata = array(); |
|
220 |
|
|
221 | 286 |
// Load alt and title text from fields. |
222 | 287 |
if (!empty($alt)) { |
223 | 288 |
$output = token_replace($alt, array('file' => $file), $replace_options); |
Formats disponibles : Unified diff
Weekly update of contrib modules