You are here

function media_wysiwyg_pre_render_text_format in D7 Media 7.4

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

Builds a map of media tags in the element.

Builds a map of the media tags in an element that are being rendered to their rendered HTML. The map is stored in JS, so we can transform them when the editor is being displayed.

1 string reference to 'media_wysiwyg_pre_render_text_format'
media_wysiwyg_element_info_alter in modules/media_wysiwyg/media_wysiwyg.module
Implements hook_element_info_alter().

File

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

Code

function media_wysiwyg_pre_render_text_format($element) {

  // This callback may be called many times on a single page request. To avoid
  // having settings and libraries that only need to be added once be added many
  // times, we use this variable to add it to the first element only.
  $global_settings_added =& drupal_static(__FUNCTION__, FALSE);

  // filter_process_format() copies properties to the expanded 'value' child
  // element.
  if (!isset($element['format'])) {
    return $element;
  }
  $field =& $element['value'];
  if (!isset($field['#value']) && $field['#value'] != '') {
    return $element;
  }

  // Settings we want available client side.
  $settings = $global_settings_added ? array() : media_wysiwyg_global_js_settings();

  // Add all media tokens within this text field.
  $settings['tagMap'] = array();
  foreach (array(
    'value',
    'summary',
  ) as $column) {
    if (isset($element[$column]['#value'])) {
      $settings['tagMap'] += _media_wysiwyg_generate_tagmap($element[$column]['#value']);
    }
  }

  // During _media_wysiwyg_generate_tagmap(), a new static fid => type map
  // is created that is required client side to get the file type of a given
  // media element. Add this too.
  $settings['fidTypeMap'] = media_wysiwyg_fid_type_map_get(TRUE);

  // Attach all settings under the Drupal.settings.media namespace, even though
  // we come from "media_wysiwyg", it makes sense to keep everything related to
  // the media package within this namespace.
  $element['#attached']['js'][] = array(
    'data' => array(
      'media' => $settings,
    ),
    'type' => 'setting',
  );
  if (!$global_settings_added) {

    // Add required js tools for working with media instances in text fields.
    $us = drupal_get_path('module', 'media_wysiwyg');
    $element['#attached']['js'][] = $us . '/js/media_wysiwyg.utils.js';
    $element['#attached']['js'][] = $us . '/js/media_wysiwyg.filter.js';
    $element['#attached']['js'][] = $us . '/js/media_wysiwyg.instance.js';

    // Load the media browser library.
    $element['#attached']['library'][] = array(
      'media',
      'media_browser',
    );
    $element['#attached']['library'][] = array(
      'media',
      'media_browser_settings',
    );
  }
  $global_settings_added = TRUE;
  return $element;
}