Projet

Général

Profil

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

root / drupal7 / sites / all / modules / media / modules / media_wysiwyg / media_wysiwyg.module @ 98df731a

1
<?php
2

    
3
/**
4
 * @file
5
 * Primarily Drupal hooks.
6
 */
7

    
8
// Functions for tracking the file usage of [[inline tags]].
9
require_once dirname(__FILE__) . '/includes/media_wysiwyg.file_usage.inc';
10

    
11
// Functions for working with [[inline tags]] and wysiwyg editors.
12
require_once dirname(__FILE__) . '/includes/media_wysiwyg.filter.inc';
13

    
14
// Functions for UUID support to embedded media.
15
require_once dirname(__FILE__) . '/includes/media_wysiwyg.uuid.inc';
16

    
17
// Functions for features integration.
18
require_once dirname(__FILE__) . '/includes/media_wysiwyg.features.inc';
19

    
20
/**
21
 * Implements hook_hook_info().
22
 */
23
function media_wysiwyg_hook_info() {
24
  $hooks = array(
25
    'media_wysiwyg_allowed_attributes_alter',
26
    'media_wysiwyg_token_to_markup_alter',
27
    'media_wysiwyg_allowed_view_modes_alter',
28
    'media_wysiwyg_format_form_prepare_alter',
29
  );
30

    
31
  return array_fill_keys($hooks, array('group' => 'media_wysiwyg'));
32
}
33

    
34
/**
35
 * Implements hook_menu().
36
 */
37
function media_wysiwyg_menu() {
38
  $items = array();
39

    
40
  $items['media/%file/format-form'] = array(
41
    'title' => 'Style selector',
42
    'description' => 'Choose a format for a piece of media',
43
    'page callback' => 'drupal_get_form',
44
    'page arguments' => array('media_wysiwyg_format_form', 1),
45
    'access callback' => 'media_wysiwyg_access',
46
    'access arguments' => array('view', 1),
47
    'file' => 'includes/media_wysiwyg.pages.inc',
48
    'theme callback' => 'media_dialog_get_theme_name',
49
    'type' => MENU_CALLBACK,
50
  );
51

    
52
  return $items;
53
}
54

    
55
/**
56
 * Implements hook_permission().
57
 */
58
function media_wysiwyg_permission() {
59
  return array(
60
    'use media wysiwyg' => array(
61
      'title' => t('Use Media WYSIWYG in an editor'),
62
      // Marked restrict because the WYSIWYG forms generate image derivatives,
63
      // which could lead to a DoS security vulnerability.
64
      'restrict access' => TRUE,
65
    ),
66
  );
67
}
68

    
69
/**
70
 * Access callback for WYSIWYG Media.
71
 */
72
function media_wysiwyg_access($op, $file = NULL, $account = NULL) {
73
  return user_access('use media wysiwyg', $account) && file_entity_access($op, $file, $account);
74
}
75

    
76
/**
77
 * Implements hook_element_info_alter().
78
 */
79
function media_wysiwyg_element_info_alter(&$types) {
80
  $types['text_format']['#pre_render'][] = 'media_wysiwyg_pre_render_text_format';
81
}
82

    
83
/**
84
 * Builds a map of media tags in the element.
85
 *
86
 * Builds a map of the media tags in an element that are being rendered to their
87
 * rendered HTML. The map is stored in JS, so we can transform them when the
88
 * editor is being displayed.
89
 */
90
function media_wysiwyg_pre_render_text_format($element) {
91
  // filter_process_format() copies properties to the expanded 'value' child
92
  // element.
93
  if (!isset($element['format'])) {
94
    return $element;
95
  }
96

    
97
  $field = &$element['value'];
98
  $settings = array(
99
    'field' => $field['#id'],
100
  );
101

    
102
  if (!isset($field['#value'])) {
103
    return $element;
104
  }
105

    
106
  $tagmap = _media_wysiwyg_generate_tagMap($field['#value']);
107

    
108
  if (isset($tagmap)) {
109
    $element['#attached']['js'][] = array(
110
      'data' => array(
111
        'tagmap' => $tagmap,
112
      ),
113
      'type' => 'setting',
114
    );
115
  }
116

    
117
  // Load the media browser library.
118
  $element['#attached']['library'][] = array('media', 'media_browser');
119
  $element['#attached']['library'][] = array('media', 'media_browser_settings');
120

    
121
  // Add wysiwyg-specific settings.
122
  $settings = array(
123
    'wysiwyg_allowed_attributes' => media_wysiwyg_allowed_attributes(),
124
    'img_alt_field' => 'field_file_image_alt_text',
125
    'img_title_field' => 'field_file_image_title_text',
126
  );
127

    
128
  // The file_entity module lets you specify a string, possibly with tokens, for
129
  // the alt and title attributes of images. We need the actual field names instead.
130
  // If the variable only contains a token of the format [file:field_file_image_alt_text]
131
  // then it's possible to extract it.
132
  $alt_token = variable_get('file_entity_alt', '[file:field_file_image_alt_text]');
133
  $title_token = variable_get('file_entity_title', '[file:field_file_image_title_text]');
134
  $matches = array();
135
  if (preg_match('/^\[file:(field_[[:alnum:]_-]+)\]$/', trim($alt_token), $matches)) {
136
    $settings['img_alt_field'] = $matches[1];
137
  }
138
  if (preg_match('/^\[file:(field_[[:alnum:]_-]+)\]$/', trim($title_token), $matches)) {
139
    $settings['img_title_field'] = $matches[1];
140
  }
141

    
142
  $element['#attached']['js'][] = array(
143
    'data' => array(
144
      'media' => $settings,
145
    ),
146
    'type' => 'setting',
147
  );
148

    
149
  // Add filter handling.
150
  $element['#attached']['js'][] = drupal_get_path('module', 'media_wysiwyg') . '/js/media_wysiwyg.filter.js';
151

    
152
  return $element;
153
}
154

    
155
/**
156
 * Implements hook_form_FORM_ID_alter().
157
 */
158
function media_wysiwyg_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
159
  // Add a checkbox that marks this field as one that can be
160
  // overridden in the WYSIWYG.
161
  if ($form['#instance']['entity_type'] == 'file') {
162
    $field_type = $form['#field']['type'];
163
    $allowed_field_types = variable_get('media_wysiwyg_wysiwyg_override_field_types', array('text', 'text_long'));
164
    if (in_array($field_type, $allowed_field_types)) {
165
      $form['instance']['settings']['wysiwyg_override'] = array(
166
        '#type' => 'checkbox',
167
        '#title' => t('Override in WYSIWYG'),
168
        '#description' => t('If checked, then this field may be overridden in the WYSIWYG editor.'),
169
        '#default_value' => isset($form['#instance']['settings']['wysiwyg_override']) ? $form['#instance']['settings']['wysiwyg_override'] : FALSE,
170
      );
171
    }
172
  }
173
}
174

    
175
/**
176
 * Implements hook_form_FORM_ID_alter().
177
 */
178
function media_wysiwyg_form_wysiwyg_profile_form_alter(&$form, &$form_state) {
179
  // Add warnings if the media filter is disabled for the WYSIWYG's text format.
180
  $form['buttons']['drupal']['media']['#element_validate'][] = 'media_wysiwyg_wysiwyg_button_element_validate';
181
  $form['buttons']['drupal']['media']['#after_build'][] = 'media_wysiwyg_wysiwyg_button_element_validate';
182
  form_load_include($form_state, 'inc', 'media_wysiwyg', 'wysiwyg_plugins/media');
183
}
184

    
185
/**
186
 * Element validate callback for the media WYSIWYG button.
187
 */
188
function media_wysiwyg_wysiwyg_button_element_validate($element, &$form_state) {
189
  if (!empty($element['#value'])) {
190
    $format = filter_format_load($form_state['build_info']['args'][0]->format);
191
    $filters = filter_list_format($format->format);
192
    if (empty($filters['media_filter']->status)) {
193
      form_error($element, t('The <em>Convert Media tags to markup</em> filter must be enabled for the <a href="@format-link">@format format</a> in order to use the Media browser WYSIWYG button.', array(
194
        '@format-link' => url('admin/config/content/formats/' . $format->format, array('query' => array('destination' => $_GET['q']))),
195
        '@format' => $format->name,
196
      )));
197
    }
198
  }
199

    
200
  return $element;
201
}
202

    
203
/**
204
 * Implements hook_form_FORM_ID_alter().
205
 */
206
function media_wysiwyg_form_media_admin_config_browser_alter(&$form, &$form_state) {
207
  $form['wysiwyg'] = array(
208
    '#type' => 'fieldset',
209
    '#title' => t('WYSIWYG configuration'),
210
    '#collapsible' => TRUE,
211
    '#collapsed' => FALSE,
212
  );
213
  $form['wysiwyg']['media_wysiwyg_wysiwyg_browser_plugins'] = array(
214
    '#type' => 'checkboxes',
215
    '#title' => t('Enabled browser plugins'),
216
    '#options' => array(),
217
    '#required' => FALSE,
218
    '#default_value' => variable_get('media_wysiwyg_wysiwyg_browser_plugins', array()),
219
    '#description' => t('If no plugins are selected, they will all be available.'),
220
  );
221

    
222
  $plugins = media_get_browser_plugin_info();
223

    
224
  foreach ($plugins as $key => $plugin) {
225
    $form['wysiwyg']['media_wysiwyg_wysiwyg_browser_plugins']['#options'][$key] = !empty($plugin['title']) ? $plugin['title'] : $key;
226
  }
227

    
228
  $form['wysiwyg']['media_wysiwyg_wysiwyg_upload_directory'] = array(
229
    '#type' => 'textfield',
230
    '#title' => t("File directory for uploaded media"),
231
    '#default_value' => variable_get('media_wysiwyg_wysiwyg_upload_directory', ''),
232
    '#description' => t('Optional subdirectory within the upload destination where files will be stored. Do not include preceding or trailing slashes.'),
233
  );
234

    
235
  if (module_exists('token')) {
236
    $form['wysiwyg']['media_wysiwyg_wysiwyg_upload_directory']['#description'] .= t('This field supports tokens.');
237
    $form['wysiwyg']['tokens'] = array(
238
      '#theme' => 'token_tree',
239
      '#dialog' => TRUE,
240
    );
241
  }
242

    
243
  $form['wysiwyg']['media_wysiwyg_default_render'] = array(
244
    '#type' => 'radios',
245
    '#title' => t('How should file entities be rendered within a text field?'),
246
    '#description' => t("Full file entity rendering is the best choice for most sites. It respects the file entity's display settings specified at admin/structure/file-types. If your site already uses the legacy method, note that changing this option will affect your site's markup. Testing it on a non-production site is recommended."),
247
    '#options' => array(
248
      'file_entity' => 'Full file entity rendering',
249
      'field_attach' => 'Legacy rendering (using field attach)',
250
    ),
251
    '#default_value' => variable_get('media_wysiwyg_default_render', 'file_entity'),
252
  );
253

    
254
  $form['wysiwyg']['media_wysiwyg_wysiwyg_allowed_types'] = array(
255
    '#type' => 'checkboxes',
256
    '#title' => t('Allowed types in WYSIWYG'),
257
    '#options' => file_entity_type_get_names(),
258
    '#default_value' => variable_get('media_wysiwyg_wysiwyg_allowed_types', array('audio', 'image', 'video', 'document')),
259
  );
260

    
261
  $options = array();
262
  foreach(field_info_field_types() as $key => $type) {
263
    $options[$key] = $type['label'];
264
  }
265
  asort($options);
266
  $form['wysiwyg']['media_wysiwyg_wysiwyg_override_field_types'] = array(
267
    '#type' => 'checkboxes',
268
    '#title' => t('Override field types in WYSIWYG'),
269
    '#options' => $options,
270
    '#default_value' => variable_get('media_wysiwyg_wysiwyg_override_field_types', array('text', 'text_long')),
271
    '#description' => t('If checked, then the field type may be overridden in the WYSIWYG editor. Not all field types (e.g. Term reference) currently support being overridden so the desired result might not be achieved.')
272
  );
273

    
274
  $form['wysiwyg']['media_wysiwyg_use_link_text_for_filename'] = array(
275
    '#type' => 'checkbox',
276
    '#title' => t("Use link text for filename"),
277
    '#default_value' => variable_get('media_wysiwyg_use_link_text_for_filename', 1),
278
    '#description' => t('When formatting inserted media, allow editable link text to be used in place of the filename. Turn this off if your file view modes handle link formatting.'),
279
  );
280

    
281
  $form['#submit'][] = 'media_wysiwyg_admin_config_browser_pre_submit';
282
}
283

    
284
/**
285
 * Manipulate values before form is submitted.
286
 */
287
function media_wysiwyg_admin_config_browser_pre_submit(&$form, &$form_state) {
288
  $wysiwyg_browser_plugins = array_unique(array_values($form_state['values']['media_wysiwyg_wysiwyg_browser_plugins']));
289
  if (empty($wysiwyg_browser_plugins[0])) {
290
    variable_del('media_wysiwyg_wysiwyg_browser_plugins');
291
    unset($form_state['values']['media_wysiwyg_wysiwyg_browser_plugins']);
292
  }
293
}
294

    
295
/**
296
 * Implements hook_filter_info().
297
 */
298
function media_wysiwyg_filter_info() {
299
  $filters['media_filter'] = array(
300
    'title' => t('Convert Media tags to markup'),
301
    'description' => t('This filter will convert [[{type:media... ]] tags into markup. This must be enabled for the Media WYSIWYG integration to work with this input format.'),
302
    'process callback' => 'media_wysiwyg_filter',
303
    'weight' => 2,
304
    // @TODO not implemented
305
    'tips callback' => 'media_filter_tips',
306
  );
307

    
308
  $filters['media_filter_paragraph_fix'] = array(
309
    'title' => t('Ensure that embedded Media tags are not contained in paragraphs'),
310
    'description' => t('This filter will strip any paragraph tags surrounding embedded Media tags. This helps to avoid the chopped up markup that can result from unexpectedly closed paragraph tags. This filter should be positioned above (before) the "Convert Media tags to markup" filter.'),
311
    'process callback' => 'media_wysiwyg_filter_paragraph_fix',
312
    'weight' => 1,
313
  );
314

    
315
  return $filters;
316
}
317

    
318
/**
319
 * Implements hook_wysiwyg_include_directory().
320
 */
321
function media_wysiwyg_wysiwyg_include_directory($type) {
322
  switch ($type) {
323
    case 'plugins':
324
      return 'wysiwyg_plugins';
325

    
326
      break;
327
  }
328
}
329

    
330
/**
331
 * Returns the set of allowed attributes for use with WYSIWYG.
332
 *
333
 * @return array
334
 *   An array of whitelisted attributes.
335
 */
336
function media_wysiwyg_allowed_attributes() {
337
  // Support the legacy variable for this.
338
  $allowed_attributes = variable_get('media_wysiwyg_wysiwyg_allowed_attributes', array(
339
    'alt',
340
    'title',
341
    'height',
342
    'width',
343
    'hspace',
344
    'vspace',
345
    'border',
346
    'align',
347
    'style',
348
    'class',
349
    'id',
350
    'usemap',
351
    'data-picture-group',
352
    'data-picture-align',
353
    'data-picture-mapping',
354
    'data-delta',
355
  ));
356
  drupal_alter('media_wysiwyg_allowed_attributes', $allowed_attributes);
357
  return $allowed_attributes;  
358
}
359

    
360
/**
361
 * Returns a drupal_render() array for just the file portion of a file entity.
362
 *
363
 * Optional custom settings can override how the file is displayed.
364
 */
365
function media_wysiwyg_get_file_without_label($file, $view_mode, $settings = array(), $langcode = NULL) {
366
  // Get the override alt & title from the fields override array. Grab the
367
  // "special" field names from the token replacement in file_entity, then see
368
  // if an override is provided and needed.
369
  $pattern = '/\[(\w+):(\w+)\]/';
370
  $alt = variable_get('file_entity_alt', '[file:field_file_image_alt_text]');
371
  $title = variable_get('file_entity_title', '[file:field_file_image_title_text]');
372
  $overrides = array(
373
    'alt' => $alt,
374
    'title' => $title,
375
  );
376
  foreach ($overrides as $field_type => $field_name) {
377
    preg_match($pattern, $field_name, $matches);
378
    if (!empty($matches[2])) {
379
      $field_name = $matches[2];
380
      $field_langcode = field_language('file', $file, $field_name);
381
      if (isset($settings['fields'][$field_name][$field_langcode]['value'])) {
382
        if (empty($settings['attributes'][$field_type])) {
383
          $settings['attributes'][$field_type] = $settings['fields'][$field_name][$field_langcode]['value'];
384
        }
385
      }
386
      if (isset($settings['fields'][$field_name][$field_langcode][0]['value'])) {
387
        if (empty($settings['attributes'][$field_type])) {
388
          $settings['attributes'][$field_type] = $settings['fields'][$field_name][$field_langcode][0]['value'];
389
        }
390
      }
391
    }
392
  }
393

    
394
  $file->override = $settings;
395

    
396
  $element = file_view_file($file, $view_mode, $langcode);
397

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

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

    
416
    // While this function may be called for any file type, images are a common
417
    // use-case, and image theme functions have their own structure for render
418
    // arrays.
419
    if (isset($element['#theme'])) {
420
      // theme_image() and theme_image_style() require the 'alt' attributes to
421
      // be passed separately from the 'attributes' array. (see
422
      // http://drupal.org/node/999338). Until that's fixed, implement this
423
      // special-case logic. Image formatters using other theme functions are
424
      // responsible for their own 'alt' attribute handling. See
425
      // theme_media_formatter_large_icon() for an example.
426
      if (in_array($element['#theme'], array('image', 'image_style'))) {
427
        if (empty($element['#alt']) && isset($settings['attributes']['alt'])) {
428
          $element['#alt'] = $settings['attributes']['alt'];
429
        }
430
      }
431
      // theme_image_formatter() and any potential replacements, such as
432
      // theme_colorbox_image_formatter(), also require attribute handling.
433
      elseif (strpos($element['#theme'], 'image_formatter') !== FALSE) {
434
        // theme_image_formatter() requires the attributes to be
435
        // set on the item rather than the element itself.
436
        if (empty($element['#item']['attributes'])) {
437
          $element['#item']['attributes'] = $settings['attributes'];
438
        }
439

    
440
        // theme_image_formatter() also requires alt, title, height, and
441
        // width attributes to be set on the item rather than within its
442
        // attributes array.
443
        foreach (array('alt', 'title', 'width', 'height') as $attr) {
444
          if (isset($settings['attributes'][$attr])) {
445
            $element['#item'][$attr] = $settings['attributes'][$attr];
446
          }
447
        }
448
      }
449
    }
450
  }
451

    
452
  return $element;
453
}
454

    
455
/**
456
 * Returns an array containing the names of all fields that perform text filtering.
457
 */
458
function media_wysiwyg_filter_fields_with_text_filtering($entity_type, $entity) {
459
  list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
460
  $fields = field_info_instances($entity_type, $bundle);
461

    
462
  // Get all of the fields on this entity that allow text filtering.
463
  $fields_with_text_filtering = array();
464
  foreach ($fields as $field_name => $field) {
465
    if (!empty($field['settings']['text_processing'])) {
466
      $fields_with_text_filtering[] = $field_name;
467
    }
468
  }
469

    
470
  return $fields_with_text_filtering;
471
}
472

    
473
/**
474
 * Return a list of view modes allowed for a file type.
475
 *
476
 * @param string $file_type
477
 *   A file type machine name.
478
 *
479
 * @return array
480
 *   An array of view modes that can be used on the file type.
481
 */
482
function media_wysiwyg_get_file_type_view_mode_options($file_type) {
483
  $enabled_view_modes = &drupal_static(__FUNCTION__, array());
484

    
485
  // @todo Add more caching for this.
486
  if (!isset($enabled_view_modes[$file_type])) {
487
    $enabled_view_modes[$file_type] = array();
488

    
489
    // Add the default view mode by default.
490
    $enabled_view_modes[$file_type]['default'] = t('Default');
491

    
492
    $entity_info = entity_get_info('file');
493
    $view_mode_settings = field_view_mode_settings('file', $file_type);
494
    foreach ($entity_info['view modes'] as $view_mode => $view_mode_info) {
495
      // Do not show view modes that don't have their own settings and will
496
      // only fall back to the default view mode.
497
      if (empty($view_mode_settings[$view_mode]['custom_settings'])) {
498
        continue;
499
      }
500

    
501
      // Don't present the user with an option to choose a view mode in which
502
      // the file is hidden.
503
      $extra_fields = field_extra_fields_get_display('file', $file_type, $view_mode);
504
      if (empty($extra_fields['file']['visible'])) {
505
        continue;
506
      }
507

    
508
      // Add the view mode to the list of enabled view modes.
509
      $enabled_view_modes[$file_type][$view_mode] = $view_mode_info['label'];
510
    }
511
  }
512

    
513
  return $enabled_view_modes[$file_type];
514
}
515

    
516
/**
517
 * Return a list of view modes allowed for a file embedded in the WYSIWYG.
518
 *
519
 * @param object $file
520
 *   A file entity.
521
 *
522
 * @return array
523
 *   An array of view modes that can be used on the file when embedded in the
524
 *   WYSIWYG.
525
 */
526
function media_wysiwyg_get_file_view_mode_options($file) {
527
  $view_modes = media_wysiwyg_get_file_type_view_mode_options($file->type);
528
  drupal_alter('media_wysiwyg_allowed_view_modes', $view_modes, $file);
529
  // Invoke the deprecated/misspelled alter hook as well.
530
  drupal_alter('media_wysiwyg_wysiwyg_allowed_view_modes', $view_modes, $file);
531
  return $view_modes;
532
}
533

    
534
/**
535
 * Implements hook_file_displays_alter().
536
 */
537
function media_wysiwyg_file_displays_alter(&$displays, $file, $view_mode) {
538
  // Override the fields of the file when requested by the WYSIWYG.
539
  if (isset($file->override) && isset($file->override['fields'])) {
540
    $instance = field_info_instances('file', $file->type);
541
    foreach ($file->override['fields'] as $field_name => $value) {
542
      if (!isset($instance[$field_name]['settings']) || !isset($instance[$field_name]['settings']['wysiwyg_override']) || $instance[$field_name]['settings']['wysiwyg_override']) {
543
        $file->{$field_name} = $value;
544
      }
545
    }
546
  }
547
}
548

    
549
/**
550
 * Implements hook_entity_info_alter().
551
 *
552
 * Add a file type named 'WYSIWYG'.
553
 */
554
function media_wysiwyg_entity_info_alter(&$entity_info) {
555
  $entity_info['file']['view modes'] += array(
556
    'wysiwyg' => array(
557
      'label' => t('WYSIWYG'),
558
      'custom settings' => TRUE,
559
    ),
560
  );
561
}
562

    
563
/**
564
 * Implements hook_form_FORM_ID_alter().
565
 *
566
 * Add select when editing file types to set wysiwyg view mode.
567
 */
568
function media_wysiwyg_form_file_entity_file_type_form_alter(&$form, &$form_state) {
569
  // #2609244 Keep media from trying to alter the File add form just edit.
570
  if (empty($form_state['build_info']['args'][0])) {
571
    return;
572
  }
573
  
574
  $options = array();
575

    
576
  // Add an option allowing users not to use a view mode.
577
  $options['none'] = t('None');
578

    
579
  // Add the default view mode by default.
580
  $options['default'] = t('Default');
581

    
582
  $entity_info = entity_get_info('file');
583
  foreach ($entity_info['view modes'] as $view_mode => $view_mode_info) {
584
    $options[$view_mode] = check_plain($view_mode_info['label']);
585
  }
586

    
587
  $file_type = $form['#file_type']->type;
588
  $view_mode = db_query('SELECT view_mode FROM {media_view_mode_wysiwyg} WHERE type = :type', array(':type' => $file_type))->fetchField();
589
  $view_mode = empty($view_mode) ? 'none' : $view_mode;
590

    
591
  $form['file_wysiwyg_view_mode'] = array(
592
    '#type' => 'select',
593
    '#title' => t('WYSIWYG view mode'),
594
    '#options' => $options,
595
    '#default_value' => $view_mode,
596
    '#description' => t('View mode to be used when displaying files inside of the WYSIWYG editor.'),
597
  );
598

    
599
  // Move submit after our select box. There might be a better way to do this.
600
  $form['submit']['#weight'] = 1;
601

    
602
  array_unshift($form['#submit'], 'media_wysiwyg_form_file_entity_file_type_form_alter_submit');
603
}
604

    
605
/**
606
 * Custom submit handler.
607
 *
608
 * Save wysiwyg view mode.
609
 *
610
 * @see media_wysiwyg_form_file_entity_file_type_form_alter().
611
 */
612
function media_wysiwyg_form_file_entity_file_type_form_alter_submit(&$form, &$form_state) {
613
  $file_type = $form['#file_type']->type;
614
  $view_mode = $form_state['values']['file_wysiwyg_view_mode'];
615
  db_delete('media_view_mode_wysiwyg')->condition('type', $file_type)->execute();
616
  if ($view_mode != 'none') {
617
    $record = array('type' => $file_type, 'view_mode' => $view_mode);
618
    drupal_write_record('media_view_mode_wysiwyg', $record);
619
  }
620
}
621

    
622
/**
623
 * Implements hook_form_FORM_ID_alter().
624
 *
625
 * Add checkbox to restrict file type view mode availability in wysiwyg.
626
 */
627
function media_wysiwyg_form_file_entity_file_display_form_alter(&$form, &$form_state) {
628
  $file_type = $form['#file_type'];
629
  $view_mode = $form['#view_mode'];
630

    
631
  if ($view_mode != 'none') {
632
    $restricted = db_query(
633
      'SELECT 1 FROM {media_restrict_wysiwyg} WHERE type = :type and display = :display',
634
      array(':type' => $file_type, ':display' => $view_mode)
635
    )->fetchField();
636
    $form['restrict_wysiwyg'] = array(
637
      '#type' => 'checkbox',
638
      '#title' => t('Restrict in WYSIWYG'),
639
      '#description' => t('If checked, then this mode will not be allowed from the WYSIWYG.'),
640
      '#default_value' => !empty($restricted),
641
    );
642
    array_unshift($form['#submit'], 'media_wysiwyg_form_file_entity_file_display_form_alter_submit');
643
  }
644

    
645
  return $form;
646
}
647

    
648
/**
649
 * Custom submit handler.
650
 *
651
 * Save restricted wysiwyg file types.
652
 *
653
 * @see media_wysiwyg_form_file_entity_file_display_form_alter().
654
 */
655
function media_wysiwyg_form_file_entity_file_display_form_alter_submit(&$form, &$form_state) {
656
  $file_type = $form['#file_type'];
657
  $view_mode = $form['#view_mode'];
658
  db_delete('media_restrict_wysiwyg')->condition('type', $file_type)->condition('display', $view_mode)->execute();
659
  if (!empty($form_state['values']['restrict_wysiwyg'])) {
660
    $record = array('type' => $file_type, 'display' => $view_mode);
661
    drupal_write_record('media_restrict_wysiwyg', $record);
662
  }
663
}