You are here

function media_wysiwyg_get_file_without_label in D7 Media 7.3

Same name and namespace in other branches
  1. 7.4 modules/media_wysiwyg/media_wysiwyg.module \media_wysiwyg_get_file_without_label()
  2. 7.2 modules/media_wysiwyg/media_wysiwyg.module \media_wysiwyg_get_file_without_label()

Returns a drupal_render() array for just the file portion of a file entity.

Optional custom settings can override how the file is displayed.

3 calls to media_wysiwyg_get_file_without_label()
media_wysiwyg_format_form in modules/media_wysiwyg/includes/media_wysiwyg.pages.inc
Form callback used when embedding media.
media_wysiwyg_format_form_view_mode in modules/media_wysiwyg/includes/media_wysiwyg.pages.inc
Add ajax preview when selecting view mode in wysiwyg editor.
media_wysiwyg_token_to_markup in modules/media_wysiwyg/includes/media_wysiwyg.filter.inc
Replace callback to convert a media file tag into HTML markup.

File

modules/media_wysiwyg/media_wysiwyg.module, line 445
Primarily Drupal hooks.

Code

function media_wysiwyg_get_file_without_label($file, $view_mode, $settings = array(), $langcode = NULL) {

  // Get the override alt & title from the fields override array. Grab the
  // "special" field names from the token replacement in file_entity, then see
  // if an override is provided and needed.
  $pattern = '/\\[(\\w+):(\\w+)\\]/';
  $alt = variable_get('file_entity_alt', '[file:field_file_image_alt_text]');
  $title = variable_get('file_entity_title', '[file:field_file_image_title_text]');
  $overrides = array(
    'alt' => $alt,
    'title' => $title,
  );
  foreach ($overrides as $field_type => $field_name) {
    preg_match($pattern, $field_name, $matches);
    if (!empty($matches[2])) {
      $field_name = $matches[2];
      $field_langcode = field_language('file', $file, $field_name);
      if (isset($settings['fields'][$field_name][$field_langcode]['value'])) {
        if (empty($settings['attributes'][$field_type])) {
          $settings['attributes'][$field_type] = $settings['fields'][$field_name][$field_langcode]['value'];
        }
      }
      if (isset($settings['fields'][$field_name][$field_langcode][0]['value'])) {
        if (empty($settings['attributes'][$field_type])) {
          $settings['attributes'][$field_type] = $settings['fields'][$field_name][$field_langcode][0]['value'];
        }
      }
    }
  }
  $file->override = $settings;
  $element = file_view_file($file, $view_mode, $langcode);

  // Field Translation Support.
  if (field_has_translation_handler('file')) {
    if ($field_items = field_get_items('file', $file, 'field_file_image_alt_text', $langcode)) {
      $value = field_view_value('file', $file, 'field_file_image_alt_text', $field_items[0], array(), $langcode);
      $element['#alt'] = isset($value['#markup']) ? $value['#markup'] : '';
    }
  }

  // The formatter invoked by file_view_file() can use $file->override to
  // customize the returned render array to match the requested settings. To
  // support simple formatters that don't do this, set the element attributes to
  // what was requested, but not if the formatter applied its own logic for
  // element attributes.
  if (isset($settings['attributes'])) {
    if (empty($element['#attributes'])) {
      $element['#attributes'] = $settings['attributes'];
    }

    // While this function may be called for any file type, images are a common
    // use-case, and image theme functions have their own structure for render
    // arrays.
    if (isset($element['#theme'])) {

      // theme_image() and theme_image_style() require the 'alt' attributes to
      // be passed separately from the 'attributes' array. (see
      // http://drupal.org/node/999338). Until that's fixed, implement this
      // special-case logic. Image formatters using other theme functions are
      // responsible for their own 'alt' attribute handling. See
      // theme_media_formatter_large_icon() for an example.
      if (in_array($element['#theme'], array(
        'image',
        'image_style',
      ))) {
        if (empty($element['#alt']) && isset($settings['attributes']['alt'])) {
          $element['#alt'] = $settings['attributes']['alt'];
        }
      }
      elseif (strpos($element['#theme'], 'image_formatter') !== FALSE) {

        // theme_image_formatter() requires the attributes to be
        // set on the item rather than the element itself.
        if (empty($element['#item']['attributes'])) {
          $element['#item']['attributes'] = $settings['attributes'];
        }

        // theme_image_formatter() also requires alt, title, height, and
        // width attributes to be set on the item rather than within its
        // attributes array.
        foreach (array(
          'alt',
          'title',
          'width',
          'height',
        ) as $attr) {
          if (isset($settings['attributes'][$attr])) {
            $element['#item'][$attr] = $settings['attributes'][$attr];
          }
        }
      }
    }
  }
  return $element;
}