Projet

Général

Profil

Paste
Télécharger (23,9 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / media / modules / media_wysiwyg / includes / media_wysiwyg.filter.inc @ e4215af7

1
<?php
2

    
3
/**
4
 * @file
5
 * Functions related to the WYSIWYG editor and the media input filter.
6
 */
7

    
8
define('MEDIA_WYSIWYG_TOKEN_REGEX', '/\[\[\{.*?"type":"media".*?\}\]\]/s');
9

    
10
/**
11
 * Filter callback for media markup filter.
12
 *
13
 * @TODO check for security probably pass text through filter_xss
14
 */
15
function media_wysiwyg_filter($text, $filter = NULL, $format = NULL, $langcode = NULL, $cache = NULL, $cache_id = NULL) {
16
  $replacements = array();
17
  $patterns = array();
18
  $rendered_text = $text;
19
  $count = 1;
20
  preg_match_all(MEDIA_WYSIWYG_TOKEN_REGEX, $text, $matches);
21
  if (!empty($matches[0])) {
22
    foreach ($matches[0] as $match) {
23
      $replacement = media_wysiwyg_token_to_markup(array($match), FALSE, $langcode);
24
      $rendered_text = str_replace($match, $replacement, $rendered_text, $count);
25
    }
26
  }
27
  return $rendered_text;
28
}
29

    
30
/**
31
 * Filter callback to configure media_filter_paragraph_fix filter.
32
 */
33
function _media_filter_paragraph_fix_settings($form, &$form_state, $filter, $format, $defaults) {
34
  $filter->settings += $defaults;
35
  $settings['replace'] = array(
36
    '#type' => 'checkbox',
37
    '#title' => t('Replace paragraph tags with DIV.media-p tags'),
38
    '#default_value' => $filter->settings['replace'],
39
    '#description' => t('Default behaviour is to strip out parent P tags of media elements rather than replacing these.'),
40
  );
41
  return $settings;
42
}
43

    
44
/**
45
 * Filter callback to remove paragraph tags surrounding embedded media.
46
 */
47
function media_wysiwyg_filter_paragraph_fix($text, $filter) {
48
  $html_dom = filter_dom_load($text);
49
  // Store Nodes to remove to avoid inferferring with the NodeList iteration.
50
  $dom_nodes_to_remove = array();
51
  foreach ($html_dom->getElementsByTagName('p') as $paragraph) {
52
    if (preg_match(MEDIA_WYSIWYG_TOKEN_REGEX, $paragraph->nodeValue)) {
53
      if (empty($filter->settings['replace'])) {
54
        $sibling = $paragraph->firstChild;
55
        do {
56
          $next = $sibling->nextSibling;
57
          $paragraph->parentNode->insertBefore($sibling, $paragraph);
58
        } while ($sibling = $next);
59
        $dom_nodes_to_remove[] = $paragraph;
60
      }
61
      else {
62
        // Clone the P node into a DIV node.
63
        $div = $html_dom->createElement('div');
64
        $sibling = $paragraph->firstChild;
65
        do {
66
          $next = $sibling->nextSibling;
67
          $div->appendChild($sibling);
68
        } while ($sibling = $next);
69

    
70
        $classes = array('media-p');
71
        if ($paragraph->hasAttributes()) {
72
          foreach ($paragraph->attributes as $attr) {
73
            $name = $attr->nodeName;
74
            $value = $attr->nodeValue;
75
            if (strtolower($name) == 'class') {
76
              $classes[] = $value;
77
            }
78
            else {
79
              // Supressing errors with ID attribute or duplicate properties.
80
              @$div->setAttribute($name, $value);
81
            }
82
          }
83
        }
84
        $div->setAttribute('class', implode(' ', $classes));
85

    
86
        $paragraph->parentNode->insertBefore($div, $paragraph);
87
        $dom_nodes_to_remove[] = $paragraph;
88
      }
89
    }
90
  }
91
  foreach ($dom_nodes_to_remove as $paragraph) {
92
    $paragraph->parentNode->removeChild($paragraph);
93
  }
94
  $text = filter_dom_serialize($html_dom);
95
  return $text;
96
}
97

    
98
/**
99
 * Parses the contents of a CSS declaration block.
100
 *
101
 * @param string $declarations
102
 *   One or more CSS declarations delimited by a semicolon. The same as a CSS
103
 *   declaration block (see http://www.w3.org/TR/CSS21/syndata.html#rule-sets),
104
 *   but without the opening and closing curly braces. Also the same as the
105
 *   value of an inline HTML style attribute.
106
 *
107
 * @return array
108
 *   A keyed array. The keys are CSS property names, and the values are CSS
109
 *   property values.
110
 */
111
function media_wysiwyg_parse_css_declarations($declarations) {
112
  $properties = array();
113
  foreach (array_map('trim', explode(";", $declarations)) as $declaration) {
114
    if ($declaration != '') {
115
      list($name, $value) = array_map('trim', explode(':', $declaration, 2));
116
      $properties[strtolower($name)] = $value;
117
    }
118
  }
119
  return $properties;
120
}
121

    
122
/**
123
 * Replace callback to convert a media file tag into HTML markup.
124
 *
125
 * @param string $match
126
 *   Takes a match of tag code
127
 * @param bool $wysiwyg
128
 *   Set to TRUE if called from within the WYSIWYG text area editor.
129
 *
130
 * @return string
131
 *   The HTML markup representation of the tag, or an empty string on failure.
132
 *
133
 * @see media_wysiwyg_get_file_without_label()
134
 * @see hook_media_wysiwyg_token_to_markup_alter()
135
 */
136
function media_wysiwyg_token_to_markup($match, $wysiwyg = FALSE, $langcode = NULL) {
137
  static $recursion_stop;
138
  $settings = array();
139
  $match = str_replace("[[", "", $match);
140
  $match = str_replace("]]", "", $match);
141
  $tag = $match[0];
142

    
143
  // Drupal modules with email support often include site name in the subject line
144
  // wrapped in brackets. With a token, this is rendered as "[[site:name]]". Such a
145
  // format will cause a conflict with media_wysiwyg, which is looking for the same.
146
  if (module_exists('token_filter')) {
147
    $token_filter = _token_filter_filter_tokens('[' . $tag . ']', '', '', $langcode, NULL, NULL);
148
    if ($token_filter != '[' . $tag . ']') {
149
      return '[[' . $tag . ']]';
150
    }
151
  }
152

    
153
  try {
154
    if (!is_string($tag)) {
155
      throw new Exception('Unable to find matching tag');
156
    }
157

    
158
    $tag_info = drupal_json_decode($tag);
159
    if (!isset($tag_info['fid'])) {
160
      throw new Exception('No file Id');
161
    }
162

    
163
    // Ensure the 'link_text' key is always defined.
164
    if (!isset($tag_info['link_text'])) {
165
      $tag_info['link_text'] = NULL;
166
    }
167

    
168
    // Ensure a valid view mode is being requested.
169
    if (!isset($tag_info['view_mode'])) {
170
      $tag_info['view_mode'] = variable_get('media_wysiwyg_wysiwyg_default_view_mode', 'full');
171
    }
172
    elseif ($tag_info['view_mode'] != 'default') {
173
      $file_entity_info = entity_get_info('file');
174
      if (!in_array($tag_info['view_mode'], array_keys($file_entity_info['view modes']))) {
175
        // Media 1.x defined some old view modes that have been superseded by
176
        // more semantically named ones in File Entity. The media_update_7203()
177
        // function updates field settings that reference the old view modes,
178
        // but it's impractical to update all text content, so adjust
179
        // accordingly here.
180
        static $view_mode_updates = array(
181
          'media_preview' => 'preview',
182
          'media_small' => 'teaser',
183
          'media_large' => 'full',
184
        );
185
        if (isset($view_mode_updates[$tag_info['view_mode']])) {
186
          $tag_info['view_mode'] = $view_mode_updates[$tag_info['view_mode']];
187
        }
188
        else {
189
          throw new Exception('Invalid view mode');
190
        }
191
      }
192
    }
193

    
194
    $file = file_load($tag_info['fid']);
195
    if (!$file) {
196
      throw new Exception('Could not load media object');
197
    }
198
    // Check if we've got a recursion. Happens because a file_load() may
199
    // triggers file_entity_is_page() which then again triggers a file load.
200
    if (isset($recursion_stop[$file->fid])) {
201
      return '';
202
    }
203
    $recursion_stop[$file->fid] = TRUE;
204

    
205
    $tag_info['file'] = $file;
206

    
207
    // The class attributes is a string, but drupal requires it to be
208
    // an array, so we fix it here.
209
    if (!empty($tag_info['attributes']['class'])) {
210
      $tag_info['attributes']['class'] = explode(" ", $tag_info['attributes']['class']);
211
    }
212

    
213
    // Grab the potentially overridden fields from the file.
214
    $fields = media_wysiwyg_filter_field_parser($tag_info);
215
    foreach ($fields as $key => $value) {
216
      $file->{$key} = $value;
217
    }
218

    
219
    if (array_key_exists('attributes', $tag_info) && is_array($tag_info['attributes'])) {
220
      $attributes = $tag_info['attributes'];
221
    }
222
    else {
223
      $attributes = array();
224
    }
225
    $attribute_whitelist = media_wysiwyg_allowed_attributes();
226
    $settings['attributes'] = array_intersect_key($attributes, array_flip($attribute_whitelist));
227
    $settings['fields'] = $fields;
228
    if (isset($tag_info['fields']['external_url'])) {
229
      $settings['fields']['external_url'] = $tag_info['fields']['external_url'];
230
    }
231
    if (!empty($tag_info['attributes']) && is_array($tag_info['attributes'])) {
232
      $settings['attributes'] = array_intersect_key($tag_info['attributes'], array_flip($attribute_whitelist));
233

    
234
      // Many media formatters will want to apply width and height independently
235
      // of the style attribute or the corresponding HTML attributes, so pull
236
      // these two out into top-level settings. Different WYSIWYG editors have
237
      // different behavior with respect to whether they store user-specified
238
      // dimensions in the HTML attributes or the style attribute - check both.
239
      // Per http://www.w3.org/TR/html5/the-map-element.html#attr-dim-width, the
240
      // HTML attributes are merely hints: CSS takes precedence.
241
      if (isset($settings['attributes']['style'])) {
242
        $css_properties = media_wysiwyg_parse_css_declarations($settings['attributes']['style']);
243
        foreach (array('width', 'height') as $dimension) {
244
          if (isset($css_properties[$dimension]) && substr($css_properties[$dimension], -2) == 'px') {
245
            $settings[$dimension] = substr($css_properties[$dimension], 0, -2);
246
          }
247
          elseif (isset($settings['attributes'][$dimension])) {
248
            $settings[$dimension] = $settings['attributes'][$dimension];
249
          }
250
        }
251
        // If the element is floated via WYSIWYG editor functionality, delegate
252
        // that information to the outer element.
253
        if (!empty($css_properties['float'])) {
254
          $settings['float'] = $css_properties['float'];
255
        }
256
      }
257
      foreach (array('title', 'alt') as $field_type) {
258
        if (isset($settings['attributes'][$field_type])) {
259
          $settings['attributes'][$field_type] = decode_entities($settings['attributes'][$field_type]);
260
        }
261
      }
262
    }
263
    // Update file metadata from the potentially overridden tag info.
264
    foreach (array('width', 'height') as $dimension) {
265
      if (isset($settings['attributes'][$dimension])) {
266
        $file->metadata[$dimension] = $settings['attributes'][$dimension];
267
      }
268
    }
269
  }
270
  catch (Exception $e) {
271
    watchdog('media', 'Unable to render media from %tag. Error: %error', array('%tag' => $tag, '%error' => $e->getMessage()));
272
    return '';
273
  }
274

    
275
  // Remove any alignment classes from $settings, because it will be added later
276
  // in this function to the media's wrapper, and we don't want to confuse CSS
277
  // by having it on both the wrapper and the element.
278
  if (isset($settings['attributes']['class'])) {
279
    $alignment_classes = array(
280
      'media-wysiwyg-align-left',
281
      'media-wysiwyg-align-right',
282
      'media-wysiwyg-align-center',
283
    );
284
    $settings['attributes']['class'] = array_diff($settings['attributes']['class'], $alignment_classes);
285
  }
286

    
287
  // If the tag has link text stored with it, override the filename with it for
288
  // the rest of this function, so that if the file is themed as a link, the
289
  // desired text will be used (see, for example, theme_file_link()).
290
  // @todo: Try to find a less hacky way to do this.
291
  if (isset($tag_info['link_text']) && variable_get('media_wysiwyg_use_link_text_for_filename', 1)) {
292
    // The link text will have characters such as "&" encoded for HTML, but the
293
    // filename itself needs the raw value when it is used to build the link,
294
    // in order to avoid double encoding.
295
    $file->filename = decode_entities($tag_info['link_text']);
296
  }
297

    
298
  if ($wysiwyg) {
299
    $settings['wysiwyg'] = $wysiwyg;
300

    
301
    // Render file in WYSIWYG using appropriate view mode.
302
    $view_mode = db_query('SELECT view_mode FROM {media_view_mode_wysiwyg} WHERE type = :type', array(
303
      ':type' => $file->type,
304
    ))
305
      ->fetchField();
306
    if (empty($view_mode)) {
307
      $view_mode = $tag_info['view_mode'];
308
    }
309

    
310
    // If sending markup to a WYSIWYG, we need to pass the file information so
311
    // that an inline macro can be generated when the WYSIWYG is detached.
312
    // The WYSIWYG plugin is expecting this information in the
313
    // Drupal.settings.mediaDataMap variable.
314
    $element = media_wysiwyg_get_file_without_label($file, $view_mode, $settings, $langcode);
315
    $data = array(
316
      'type' => 'media',
317
      'fid'  => $file->fid,
318
      'view_mode' => $tag_info['view_mode'],
319
      'link_text' => $tag_info['link_text'],
320
    );
321
    drupal_add_js(array('mediaDataMap' => array($file->fid => $data)), 'setting');
322
    $element['#attributes']['data-fid'] = $file->fid;
323
    $element['#attributes']['data-media-element'] = '1';
324
    if (is_array($element['#attributes']['class'])) {
325
      $element['#attributes']['class'][] = 'media-element';
326
    }
327
    else {
328
      $element['#attributes']['class'] .= 'media-element';
329
    }
330
  }
331
  else {
332
    // Display the field elements.
333
    $element = array();
334
    // Render the file entity, for sites using the file_entity rendering method.
335
    if (variable_get('media_wysiwyg_default_render', 'file_entity') == 'file_entity') {
336
      $element['content'] = file_view($file, $tag_info['view_mode'], $langcode);
337
    }
338
    $element['content']['file'] = media_wysiwyg_get_file_without_label($file, $tag_info['view_mode'], $settings, $langcode);
339
    // Overwrite or set the file #alt attribute if it has been set in this
340
    // instance.
341
    if (!empty($element['content']['file']['#attributes']['alt'])) {
342
      $element['content']['file']['#alt'] = $element['content']['file']['#attributes']['alt'];
343
    }
344
    // Overwrite or set the file #title attribute if it has been set in this
345
    // instance.
346
    if (!empty($element['content']['file']['#attributes']['title'])) {
347
      $element['content']['file']['#title'] = $element['content']['file']['#attributes']['title'];
348
    }
349
    // For sites using the legacy field_attach rendering method, attach fields.
350
    if (variable_get('media_wysiwyg_default_render', 'file_entity') == 'field_attach') {
351
      field_attach_prepare_view('file', array($file->fid => $file), $tag_info['view_mode'], $langcode);
352
      entity_prepare_view('file', array($file->fid => $file), $langcode);
353
      $element['content'] += field_attach_view('file', $file, $tag_info['view_mode'], $langcode);
354

    
355
      // Add any float information via an extra class
356
      if (!empty($settings['float'])) {
357
        $element['content']['file']['#attributes']['class'][] = drupal_html_class('media-float-' . $settings['float']);
358
      }
359
    }
360
    if (count(element_children($element['content'])) > 1) {
361
      // Add surrounding divs to group them together.
362
      // We don't want divs when there are no additional fields to allow files
363
      // to display inline with text, without breaking p tags.
364
      $element['content']['#type'] = 'container';
365
      $element['content']['#attributes']['class'] = array(
366
        'media',
367
        'media-element-container',
368
        'media-' . $element['content']['file']['#view_mode'],
369
      );
370
      // Add the float information to the outer element.
371
      if (!empty($settings['float'])) {
372
        $element['content']['#attributes']['class'][] = drupal_html_class('media-float-' . $settings['float']);
373
      }
374
      if (variable_get('media_wysiwyg_remove_media_class', FALSE)) {
375
        $classes = $element['content']['#attributes']['class'];
376
        $element['content']['#attributes']['class'] = array_diff($classes, array('media'));
377
      }
378
    }
379

    
380
    // Conditionally add a pre-render if the media filter output is be cached.
381
    $filters = filter_get_filters();
382
    if (!isset($filters['media_filter']['cache']) || $filters['media_filter']['cache']) {
383
      $element['#pre_render'][] = 'media_wysiwyg_pre_render_cached_filter';
384
    }
385
  }
386
  if (!empty($element['content']) && !empty($tag_info['fields']['alignment'])) {
387
    // Set a CSS class if an alignment has been specified and is correct.
388
    $alignment = $tag_info['fields']['alignment'];
389
    if (in_array($alignment, array('left', 'right', 'center'))) {
390
      $alignment_class = 'media-wysiwyg-align-' . $alignment;
391
      $element['content']['#attributes']['class'][] = $alignment_class;
392
    }
393
  }
394
  drupal_alter('media_wysiwyg_token_to_markup', $element, $tag_info, $settings, $langcode);
395
  $output = drupal_render($element);
396
  unset($recursion_stop[$file->fid]);
397
  return $output;
398
}
399

    
400
/**
401
 * Parse the 'fields' entry of $tag_info into structured, sanitized fields data.
402
 *
403
 * The keys of the fields array are the string equivalent of accessing the
404
 * field's value, e.g. 'field_file_image_alt_text[und][0][value]' and the value
405
 * is the actual field value. These parts are turned into sanitized fields data
406
 * arrays with the full hierarchy of language, deltas, data column and finally
407
 * value.
408
 *
409
 * Only configured or programmed fields present in the site's installation is
410
 * parsed and returned.
411
 *
412
 * @param array $tag_info
413
 *   Media token as PHP array equivalent.
414
 *
415
 * @return array
416
 *   An array of fields with sanitized field data structures.
417
 */
418
function media_wysiwyg_filter_field_parser(array $tag_info) {
419
  $fields = array();
420
  if (isset($tag_info['fields'])) {
421
    // Field value reference candidates (keys) that end in [format] are
422
    // associated with long-text fields that may have HTML Entities. Those
423
    // values will need to be URLDecoded as well as HTMLDecoded.
424
    $url_encoded_fields = array();
425
    foreach ($tag_info['fields'] as $candidate => $field_value) {
426
      if (preg_match('/\[format\]$/', $candidate) > 0) {
427
        $url_encoded_fields[] = preg_replace('/\[format\]$/', '[value]', $candidate);
428
      }
429
    }
430

    
431
    foreach ($tag_info['fields'] as $candidate => $field_value) {
432
      if (strpos($candidate, 'field_') === 0) {
433
        $parsed_field = explode('[', str_replace(']', '', $candidate));
434
        // We are garuanteed to have a value in $parsed_field[0].
435
        $info = field_info_field($parsed_field[0]);
436
        if (!$info) {
437
          // Not an existing/configured field.
438
          continue;
439
        }
440

    
441
        // Certain types of fields, because of differences in markup, end up
442
        // here with incomplete arrays. Make a best effort to support as many
443
        // types of fields as possible.
444
        // Single-value select lists show up here with only 2 array items.
445
        if (count($parsed_field) == 2) {
446
          if ($info && !empty($info['columns'])) {
447
            // Assume single-value.
448
            $parsed_field[] = 0;
449
            // Next tack on the column for this field.
450
            $parsed_field[] = key($info['columns']);
451
          }
452
        }
453
        // Multi-value select lists show up here with 3 array items.
454
        elseif (count($parsed_field) == 3 && (empty($parsed_field[2]) || is_numeric($parsed_field[2]))) {
455
          // They just need the value column.
456
          $parsed_field[3] = key($info['columns']);
457
        }
458

    
459
        // Each key of the field needs to be the child of the previous key.
460
        $ref = &$fields;
461
        foreach ($parsed_field as $key) {
462
          if (!isset($ref[$key])) {
463
            $ref[$key] = array();
464
          }
465
          $ref = &$ref[$key];
466
        }
467
        // The value should be set at the deepest level.
468
        if (in_array($candidate, $url_encoded_fields)) {
469
          // Fields that use rich-text markup will be urlencoded.
470
          $ref = urldecode(decode_entities($field_value));
471
        }
472
        else {
473
          // Only entities need to be decoded.
474
          $ref = decode_entities($field_value);
475
        }
476
      }
477
    }
478
    // Strip fields for empty values. This is necessary to do post parsing of
479
    // all field entries as they may refer to the same field several times.
480
    foreach ($fields as $field_name => $field_languages) {
481
      $info = field_info_field($field_name);
482
      if (isset($field_languages) && is_array($field_languages)) {
483
        foreach ($field_languages as $lang => $items) {
484
          $fields[$field_name][$lang] = _field_filter_items($info, $items);
485
        }
486
      }
487
    }
488
  }
489
  return $fields;
490
}
491

    
492
/**
493
 * Creates map of inline media tags.
494
 *
495
 * Generates an array of [inline tags] => <html> to be used in filter
496
 * replacement and to add the mapping to JS.
497
 *
498
 * @param string $text
499
 *   The String containing text and html markup of textarea
500
 *
501
 * @return array
502
 *   An associative array with tag code as key and html markup as the value.
503
 *
504
 * @see media_process_form()
505
 * @see media_token_to_markup()
506
 */
507
function _media_wysiwyg_generate_tagMap($text) {
508
  // Making $tagmap static as this function is called many times and
509
  // adds duplicate markup for each tag code in Drupal.settings JS,
510
  // so in media_process_form it adds something like tagCode:<markup>,
511
  // <markup> and when we replace in attach see two duplicate images
512
  // for one tagCode. Making static would make function remember value
513
  // between function calls. Since media_process_form is multiple times
514
  // with same form, this function is also called multiple times.
515
  static $tagmap = array();
516
  preg_match_all("/\[\[(?!nid:).*?\]\]/s", $text, $matches, PREG_SET_ORDER);
517
  foreach ($matches as $match) {
518
    // We see if tagContent is already in $tagMap, if not we add it
519
    // to $tagmap.  If we return an empty array, we break embeddings of the same
520
    // media multiple times.
521
    if (empty($tagmap[$match[0]])) {
522
      // @TODO: Total HACK, but better than nothing.
523
      // We should find a better way of cleaning this up.
524
      if ($markup_for_media = media_wysiwyg_token_to_markup($match, TRUE)) {
525
        $tagmap[$match[0]] = $markup_for_media;
526
      }
527
      else {
528
        $missing = file_create_url(drupal_get_path('module', 'media') . '/images/icons/default/image-x-generic.png');
529
        $tagmap[$match[0]] = '<div><img src="' . $missing . '" width="100px" height="100px"/></div>';
530
      }
531
    }
532
  }
533
  return $tagmap;
534
}
535

    
536
/**
537
 * Return a list of view modes allowed for a file embedded in the WYSIWYG.
538
 *
539
 * @param object $file
540
 *   A file entity.
541
 *
542
 * @return array
543
 *   An array of view modes that can be used on the file when embedded in the
544
 *   WYSIWYG.
545
 *
546
 * @deprecated
547
 */
548
function media_wysiwyg_get_wysiwyg_allowed_view_modes($file) {
549
  $enabled_view_modes = &drupal_static(__FUNCTION__, array());
550

    
551
  // @todo Add more caching for this.
552
  if (!isset($enabled_view_modes[$file->type])) {
553
    $enabled_view_modes[$file->type] = array();
554

    
555
    // Add the default view mode by default.
556
    $enabled_view_modes[$file->type]['default'] = array('label' => t('Default'), 'custom settings' => TRUE);
557

    
558
    $entity_info = entity_get_info('file');
559
    $view_mode_settings = field_view_mode_settings('file', $file->type);
560
    foreach ($entity_info['view modes'] as $view_mode => $view_mode_info) {
561
      // Do not show view modes that don't have their own settings and will
562
      // only fall back to the default view mode.
563
      if (empty($view_mode_settings[$view_mode]['custom_settings'])) {
564
        continue;
565
      }
566

    
567
      // Don't present the user with an option to choose a view mode in which
568
      // the file is hidden.
569
      $extra_fields = field_extra_fields_get_display('file', $file->type, $view_mode);
570
      if (empty($extra_fields['file']['visible'])) {
571
        continue;
572
      }
573

    
574
      // Add the view mode to the list of enabled view modes.
575
      $enabled_view_modes[$file->type][$view_mode] = $view_mode_info;
576
    }
577
  }
578

    
579
  $view_modes = $enabled_view_modes[$file->type];
580
  media_wysiwyg_wysiwyg_allowed_view_modes_restrict($view_modes, $file);
581
  drupal_alter('media_wysiwyg_allowed_view_modes', $view_modes, $file);
582
  // Invoke the deprecated/misspelled alter hook as well.
583
  drupal_alter('media_wysiwyg_wysiwyg_allowed_view_modes', $view_modes, $file);
584
  return $view_modes;
585
}
586
/**
587
 * Do not show restricted view modes.
588
 */
589
function media_wysiwyg_wysiwyg_allowed_view_modes_restrict(&$view_modes, &$file) {
590
  $restricted_view_modes = db_query('SELECT display FROM {media_restrict_wysiwyg} WHERE type = :type', array(':type' => $file->type))->fetchCol();
591
  foreach ($restricted_view_modes as $restricted_view_mode) {
592
    if (array_key_exists($restricted_view_mode, $view_modes)) {
593
      unset($view_modes[$restricted_view_mode]);
594
    }
595
  }
596
}
597
/**
598
 * #pre_render callback: Modify the element if the render cache is filtered.
599
 */
600
function media_wysiwyg_pre_render_cached_filter($element) {
601
  // Remove contextual links since they are not compatible with cached filtered
602
  // text.
603
  if (isset($element['content']['#contextual_links'])) {
604
    unset($element['content']['#contextual_links']);
605
  }
606

    
607
  return $element;
608
}