Project

General

Profile

Revision 9a28ac3f

Added by Assos Assos almost 4 years ago

Weekly update of contrib modules

View differences:

drupal7/sites/all/modules/file_entity/file_entity.module
1250 1250
  }
1251 1251
}
1252 1252

  
1253
/**
1254
 * Replace file entity title text.
1255
 *
1256
 * @param $file
1257
 *   The file entity.
1258
 * @param $replace_options
1259
 *   (Optional) Options to pass to token_replace().
1260
 * @param $title
1261
 *   (Optional) The title text to use.
1262
 * @param string $langcode
1263
 *   (Optional) Language code
1264
 *
1265
 * @return string
1266
 *   Returns the replaced title text.
1267
 */
1268
function file_entity_replace_title($file, $replace_options = [], $title = NULL, $langcode = NULL) {
1269
  $replace_options += [
1270
    'clear' => TRUE,
1271
    'sanitize' => FALSE,
1272
  ];
1273

  
1274
  $title_default = '[file:field_file_image_title_text]';
1275
  if (!isset($title)) {
1276
    $title = variable_get('file_entity_title', $title_default);
1277
  }
1278
  // If the defaults are not changed then inlining replacement is much faster
1279
  // than dealing with the token system.
1280
  if ($title === $title_default) {
1281
    $title_items = field_get_items('file', $file, 'field_file_image_title_text', $langcode);
1282
    return $title_items ? $title_items[0]['value'] : '';
1283
  }
1284
  elseif (!empty($title)) {
1285
    $token_replaced = token_replace($title, ['file' => $file], $replace_options);
1286
    return decode_entities($token_replaced); // Filter out possible XSS.
1287
  }
1288

  
1289
  return '';
1290
}
1291

  
1292
/**
1293
 * Replace file entity alt.
1294
 *
1295
 * @param $file
1296
 *   The file entity.
1297
 * @param array $replace_options
1298
 *   (Optional) Options to pass to token_replace().
1299
 * @param $alt
1300
 *   (Optional) The alt text to use.
1301
 * @param string $langcode
1302
 *   (Optional) Language code
1303
 *
1304
 * @return string
1305
 *   Returns the replaced alt text.
1306
 */
1307
function file_entity_replace_alt($file, $replace_options = [], $alt = NULL, $langcode = NULL) {
1308
  $replace_options += [
1309
    'clear' => TRUE,
1310
    'sanitize' => FALSE,
1311
  ];
1312

  
1313
  $alt_default = '[file:field_file_image_alt_text]';
1314

  
1315
  if (!isset($alt)) {
1316
    $alt = variable_get('file_entity_alt', $alt_default);
1317
  }
1318

  
1319
  // If the defaults are not changed then inlining replacement is much faster
1320
  // than dealing with the token system.
1321
  if ($alt === $alt_default) {
1322
    $alt_items = field_get_items('file', $file, 'field_file_image_alt_text', $langcode);
1323
    return $alt_items ? $alt_items[0]['value'] : '';
1324
  }
1325
  elseif (!empty($alt)) {
1326
    $token_replaced = token_replace($alt, ['file' => $file], $replace_options);
1327
    return decode_entities($token_replaced); // Filter out possible XSS.
1328
  }
1329

  
1330
  return '';
1331
}
1332

  
1253 1333
/**
1254 1334
 * Implements hook_file_formatter_FORMATTER_view().
1255 1335
 *
......
1290 1370
        '#path' => $file->uri,
1291 1371
        '#width' => isset($file->override['attributes']['width']) ? $file->override['attributes']['width'] : $file->metadata['width'],
1292 1372
        '#height' => isset($file->override['attributes']['height']) ? $file->override['attributes']['height'] : $file->metadata['height'],
1293
        '#alt' => token_replace($display['settings']['alt'], array('file' => $file), $replace_options),
1294
        '#title' => token_replace($display['settings']['title'], array('file' => $file), $replace_options),
1373
        '#alt' => file_entity_replace_alt($file, $replace_options, $display['settings']['alt'], $langcode),
1374
        '#title' => file_entity_replace_title($file, $replace_options, $display['settings']['title'], $langcode),
1295 1375
      );
1296 1376
    }
1297 1377
    else {
......
1300 1380
        '#path' => $file->uri,
1301 1381
        '#width' => isset($file->override['attributes']['width']) ? $file->override['attributes']['width'] : $file->metadata['width'],
1302 1382
        '#height' => isset($file->override['attributes']['height']) ? $file->override['attributes']['height'] : $file->metadata['height'],
1303
        '#alt' => token_replace($display['settings']['alt'], array('file' => $file), $replace_options),
1304
        '#title' => token_replace($display['settings']['title'], array('file' => $file), $replace_options),
1383
        '#alt' => file_entity_replace_alt($file, $replace_options, $display['settings']['alt'], $langcode),
1384
        '#title' => file_entity_replace_title($file, $replace_options, $display['settings']['title'], $langcode),
1305 1385
      );
1306 1386
    }
1307 1387
    return $element;

Also available in: Unified diff