Projet

Général

Profil

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

root / drupal7 / sites / all / modules / media / modules / media_wysiwyg / media_wysiwyg.module @ 1e39edcb

1 ca0757b9 Assos Assos
<?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 1e39edcb Assos Assos
// Functions for features integration.
18
require_once dirname(__FILE__) . '/includes/media_wysiwyg.features.inc';
19
20 ca0757b9 Assos Assos
/**
21
 * Implements hook_hook_info().
22
 */
23
function media_wysiwyg_hook_info() {
24
  $hooks = array(
25
    'media_wysiwyg_token_to_markup_alter',
26
    'media_wysiwyg_allowed_view_modes_alter',
27
    'media_wysiwyg_format_form_prepare_alter',
28
  );
29
30
  return array_fill_keys($hooks, array('group' => 'media_wysiwyg'));
31
}
32
33
/**
34
 * Implements hook_menu().
35
 */
36
function media_wysiwyg_menu() {
37 bf3c8457 Florent Torregrosa
  $items = array();
38
39 ca0757b9 Assos Assos
  $items['media/%file/format-form'] = array(
40
    'title' => 'Style selector',
41
    'description' => 'Choose a format for a piece of media',
42
    'page callback' => 'drupal_get_form',
43
    'page arguments' => array('media_wysiwyg_format_form', 1),
44 bf3c8457 Florent Torregrosa
    'access callback' => 'media_wysiwyg_access',
45 ca0757b9 Assos Assos
    'access arguments' => array('view', 1),
46
    'file' => 'includes/media_wysiwyg.pages.inc',
47
    'theme callback' => 'media_dialog_get_theme_name',
48
    'type' => MENU_CALLBACK,
49
  );
50
51
  return $items;
52
}
53
54 bf3c8457 Florent Torregrosa
/**
55
 * Implements hook_permission().
56
 */
57
function media_wysiwyg_permission() {
58
  return array(
59
    'use media wysiwyg' => array(
60
      'title' => t('Use Media WYSIWYG in an editor'),
61
      // Marked restrict because the WYSIWYG forms generate image derivatives,
62
      // which could lead to a DoS security vulnerability.
63
      'restrict access' => TRUE,
64
    ),
65
  );
66
}
67
68
/**
69
 * Access callback for WYSIWYG Media.
70
 */
71
function media_wysiwyg_access($op, $file = NULL, $account = NULL) {
72
  return user_access('use media wysiwyg', $account) && file_entity_access($op, $file, $account);
73
}
74
75 ca0757b9 Assos Assos
/**
76
 * Implements hook_element_info_alter().
77
 */
78
function media_wysiwyg_element_info_alter(&$types) {
79
  $types['text_format']['#pre_render'][] = 'media_wysiwyg_pre_render_text_format';
80
}
81
82 bf3c8457 Florent Torregrosa
/**
83
 * Builds a map of media tags in the element.
84
 *
85
 * Builds a map of the media tags in an element that are being rendered to their
86
 * rendered HTML. The map is stored in JS, so we can transform them when the
87
 * editor is being displayed.
88
 */
89
function media_wysiwyg_pre_render_text_format($element) {
90
  // filter_process_format() copies properties to the expanded 'value' child
91
  // element.
92
  if (!isset($element['format'])) {
93
    return $element;
94
  }
95
96
  $field = &$element['value'];
97
  $settings = array(
98
    'field' => $field['#id'],
99
  );
100
101
  if (!isset($field['#value'])) {
102
    return $element;
103
  }
104
105
  $tagmap = _media_wysiwyg_generate_tagMap($field['#value']);
106
107
  if (isset($tagmap)) {
108
    $element['#attached']['js'][] = array(
109
      'data' => array(
110
        'tagmap' => $tagmap,
111
      ),
112
      'type' => 'setting',
113
    );
114
  }
115
116
  // Load the media browser library.
117
  $element['#attached']['library'][] = array('media', 'media_browser');
118
  $element['#attached']['library'][] = array('media', 'media_browser_settings');
119
120
  // Add wysiwyg-specific settings.
121 fc3d89c3 Assos Assos
  $settings = array(
122
    'wysiwyg_allowed_attributes' => variable_get('media_wysiwyg_wysiwyg_allowed_attributes', _media_wysiwyg_wysiwyg_allowed_attributes_default()),
123
    'img_alt_field' => 'field_file_image_alt_text',
124
    'img_title_field' => 'field_file_image_title_text',
125
  );
126
127
  // The file_entity module lets you specify a string, possibly with tokens, for
128
  // the alt and title attributes of images. We need the actual field names instead.
129
  // If the variable only contains a token of the format [file:field_file_image_alt_text]
130
  // then it's possible to extract it.
131
  $alt_token = variable_get('file_entity_alt', '[file:field_file_image_alt_text]');
132
  $title_token = variable_get('file_entity_title', '[file:field_file_image_title_text]');
133
  $matches = array();
134
  if (preg_match('/^\[file:(field_[[:alnum:]_-]+)\]$/', trim($alt_token), $matches)) {
135
    $settings['img_alt_field'] = $matches[1];
136
  }
137
  if (preg_match('/^\[file:(field_[[:alnum:]_-]+)\]$/', trim($title_token), $matches)) {
138
    $settings['img_title_field'] = $matches[1];
139
  }
140
141 bf3c8457 Florent Torregrosa
  $element['#attached']['js'][] = array(
142
    'data' => array(
143
      'media' => $settings,
144
    ),
145
    'type' => 'setting',
146
  );
147
148
  // Add filter handling.
149
  $element['#attached']['js'][] = drupal_get_path('module', 'media_wysiwyg') . '/js/media_wysiwyg.filter.js';
150
151
  return $element;
152
}
153
154 da542b7b Assos Assos
/**
155
 * Implements hook_form_FORM_ID_alter().
156
 */
157
function media_wysiwyg_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
158
  // Add a checkbox that marks this field as one that can be
159
  // overridden in the WYSIWYG.
160
  if ($form['#instance']['entity_type'] == 'file') {
161
    $field_type = $form['#field']['type'];
162
    $allowed_field_types = variable_get('media_wysiwyg_wysiwyg_override_field_types', array('text', 'text_long'));
163
    if (in_array($field_type, $allowed_field_types)) {
164
      $form['instance']['settings']['wysiwyg_override'] = array(
165
        '#type' => 'checkbox',
166
        '#title' => t('Override in WYSIWYG'),
167
        '#description' => t('If checked, then this field may be overridden in the WYSIWYG editor.'),
168
        '#default_value' => isset($form['#instance']['settings']['wysiwyg_override']) ? $form['#instance']['settings']['wysiwyg_override'] : FALSE,
169
      );
170
    }
171
  }
172
}
173
174 ca0757b9 Assos Assos
/**
175
 * Implements hook_form_FORM_ID_alter().
176
 */
177
function media_wysiwyg_form_wysiwyg_profile_form_alter(&$form, &$form_state) {
178
  // Add warnings if the media filter is disabled for the WYSIWYG's text format.
179
  $form['buttons']['drupal']['media']['#element_validate'][] = 'media_wysiwyg_wysiwyg_button_element_validate';
180
  $form['buttons']['drupal']['media']['#after_build'][] = 'media_wysiwyg_wysiwyg_button_element_validate';
181
  form_load_include($form_state, 'inc', 'media_wysiwyg', 'wysiwyg_plugins/media');
182
}
183
184
/**
185
 * Element validate callback for the media WYSIWYG button.
186
 */
187
function media_wysiwyg_wysiwyg_button_element_validate($element, &$form_state) {
188
  if (!empty($element['#value'])) {
189
    $format = filter_format_load($form_state['build_info']['args'][0]->format);
190
    $filters = filter_list_format($format->format);
191
    if (empty($filters['media_filter']->status)) {
192
      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(
193
        '@format-link' => url('admin/config/content/formats/' . $format->format, array('query' => array('destination' => $_GET['q']))),
194
        '@format' => $format->name,
195
      )));
196
    }
197
  }
198
199
  return $element;
200
}
201
202
/**
203
 * Implements hook_form_FORM_ID_alter().
204
 */
205
function media_wysiwyg_form_media_admin_config_browser_alter(&$form, &$form_state) {
206
  $form['wysiwyg'] = array(
207
    '#type' => 'fieldset',
208
    '#title' => t('WYSIWYG configuration'),
209
    '#collapsible' => TRUE,
210
    '#collapsed' => FALSE,
211
  );
212
  $form['wysiwyg']['media_wysiwyg_wysiwyg_browser_plugins'] = array(
213
    '#type' => 'checkboxes',
214
    '#title' => t('Enabled browser plugins'),
215
    '#options' => array(),
216
    '#required' => FALSE,
217
    '#default_value' => variable_get('media_wysiwyg_wysiwyg_browser_plugins', array()),
218
    '#description' => t('If no plugins are selected, they will all be available.'),
219
  );
220
221
  $plugins = media_get_browser_plugin_info();
222
223
  foreach ($plugins as $key => $plugin) {
224
    $form['wysiwyg']['media_wysiwyg_wysiwyg_browser_plugins']['#options'][$key] = !empty($plugin['title']) ? $plugin['title'] : $key;
225
  }
226
227
  $form['wysiwyg']['media_wysiwyg_wysiwyg_upload_directory'] = array(
228
    '#type' => 'textfield',
229
    '#title' => t("File directory for uploaded media"),
230
    '#default_value' => variable_get('media_wysiwyg_wysiwyg_upload_directory', ''),
231
    '#description' => t('Optional subdirectory within the upload destination where files will be stored. Do not include preceding or trailing slashes.'),
232
  );
233
234
  if (module_exists('token')) {
235
    $form['wysiwyg']['media_wysiwyg_wysiwyg_upload_directory']['#description'] .= t('This field supports tokens.');
236
    $form['wysiwyg']['tokens'] = array(
237
      '#theme' => 'token_tree',
238
      '#dialog' => TRUE,
239
    );
240
  }
241
242 bf3c8457 Florent Torregrosa
  $form['wysiwyg']['media_wysiwyg_default_render'] = array(
243
    '#type' => 'radios',
244
    '#title' => t('How should file entities be rendered within a text field?'),
245
    '#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."),
246
    '#options' => array(
247
      'file_entity' => 'Full file entity rendering',
248
      'field_attach' => 'Legacy rendering (using field attach)',
249
    ),
250
    '#default_value' => variable_get('media_wysiwyg_default_render', 'file_entity'),
251
  );
252
253 ca0757b9 Assos Assos
  $form['wysiwyg']['media_wysiwyg_wysiwyg_allowed_types'] = array(
254
    '#type' => 'checkboxes',
255
    '#title' => t('Allowed types in WYSIWYG'),
256
    '#options' => file_entity_type_get_names(),
257
    '#default_value' => variable_get('media_wysiwyg_wysiwyg_allowed_types', array('audio', 'image', 'video', 'document')),
258
  );
259
260 da542b7b Assos Assos
  $options = array();
261
  foreach(field_info_field_types() as $key => $type) {
262
    $options[$key] = $type['label'];
263
  }
264
  asort($options);
265
  $form['wysiwyg']['media_wysiwyg_wysiwyg_override_field_types'] = array(
266
    '#type' => 'checkboxes',
267
    '#title' => t('Override field types in WYSIWYG'),
268
    '#options' => $options,
269
    '#default_value' => variable_get('media_wysiwyg_wysiwyg_override_field_types', array('text', 'text_long')),
270
    '#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.')
271
  );
272
273 ca0757b9 Assos Assos
  $form['#submit'][] = 'media_wysiwyg_admin_config_browser_pre_submit';
274
}
275
276
/**
277
 * Manipulate values before form is submitted.
278
 */
279
function media_wysiwyg_admin_config_browser_pre_submit(&$form, &$form_state) {
280
  $wysiwyg_browser_plugins = array_unique(array_values($form_state['values']['media_wysiwyg_wysiwyg_browser_plugins']));
281
  if (empty($wysiwyg_browser_plugins[0])) {
282
    variable_del('media_wysiwyg_wysiwyg_browser_plugins');
283
    unset($form_state['values']['media_wysiwyg_wysiwyg_browser_plugins']);
284
  }
285
}
286
287
/**
288
 * Implements hook_filter_info().
289
 */
290
function media_wysiwyg_filter_info() {
291
  $filters['media_filter'] = array(
292
    'title' => t('Convert Media tags to markup'),
293
    '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.'),
294
    'process callback' => 'media_wysiwyg_filter',
295
    'weight' => 2,
296
    // @TODO not implemented
297
    'tips callback' => 'media_filter_tips',
298
  );
299
300 da542b7b Assos Assos
  $filters['media_filter_paragraph_fix'] = array(
301
    'title' => t('Ensure that embedded Media tags are not contained in paragraphs'),
302
    '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.'),
303
    'process callback' => 'media_wysiwyg_filter_paragraph_fix',
304
    'weight' => 1,
305
  );
306
307 ca0757b9 Assos Assos
  return $filters;
308
}
309
310
/**
311
 * Implements hook_wysiwyg_include_directory().
312
 */
313
function media_wysiwyg_wysiwyg_include_directory($type) {
314
  switch ($type) {
315
    case 'plugins':
316
      return 'wysiwyg_plugins';
317
318
      break;
319
  }
320
}
321
322
/**
323
 * Returns the default set of allowed attributes for use with WYSIWYG.
324
 *
325
 * @return array
326
 *   An array of whitelisted attributes.
327
 */
328
function _media_wysiwyg_wysiwyg_allowed_attributes_default() {
329
  return array(
330
    'alt',
331
    'title',
332
    'height',
333
    'width',
334
    'hspace',
335
    'vspace',
336
    'border',
337
    'align',
338
    'style',
339
    'class',
340
    'id',
341
    'usemap',
342
    'data-picture-group',
343
    'data-picture-align',
344
    'data-picture-mapping',
345
  );
346
}
347
348
/**
349
 * Returns a drupal_render() array for just the file portion of a file entity.
350
 *
351
 * Optional custom settings can override how the file is displayed.
352
 */
353 fc3d89c3 Assos Assos
function media_wysiwyg_get_file_without_label($file, $view_mode, $settings = array(), $langcode = NULL) {
354 da542b7b Assos Assos
  // Get the override alt & title from the fields override array. Grab the
355
  // "special" field names from the token replacement in file_entity, then see
356
  // if an override is provided and needed.
357
  $pattern = '/\[(\w+):(\w+)\]/';
358
  $alt = variable_get('file_entity_alt', '[file:field_file_image_alt_text]');
359
  $title = variable_get('file_entity_title', '[file:field_file_image_title_text]');
360
  $overrides = array(
361
    'alt' => $alt,
362
    'title' => $title,
363
  );
364
  foreach ($overrides as $field_type => $field_name) {
365
    preg_match($pattern, $field_name, $matches);
366
    if (!empty($matches[2])) {
367
      $field_name = $matches[2];
368 1e39edcb Assos Assos
      $field_langcode = field_language('file', $file, $field_name);
369
      if (isset($settings['fields'][$field_name][$field_langcode]['value'])) {
370 da542b7b Assos Assos
        if (empty($settings['attributes'][$field_type])) {
371 1e39edcb Assos Assos
          $settings['attributes'][$field_type] = $settings['fields'][$field_name][$field_langcode]['value'];
372 da542b7b Assos Assos
        }
373
      }
374 1e39edcb Assos Assos
      if (isset($settings['fields'][$field_name][$field_langcode][0]['value'])) {
375 da542b7b Assos Assos
        if (empty($settings['attributes'][$field_type])) {
376 1e39edcb Assos Assos
          $settings['attributes'][$field_type] = $settings['fields'][$field_name][$field_langcode][0]['value'];
377 da542b7b Assos Assos
        }
378
      }
379
    }
380
  }
381
382 ca0757b9 Assos Assos
  $file->override = $settings;
383
384 fc3d89c3 Assos Assos
  $element = file_view_file($file, $view_mode, $langcode);
385
386
  // Field Translation Support.
387
  if (field_has_translation_handler('file')) {
388
    if ($field_items = field_get_items('file', $file, 'field_file_image_alt_text', $langcode)) {
389
      $value = field_view_value('file', $file, 'field_file_image_alt_text', $field_items[0], array(), $langcode);
390
      $element['#alt'] = isset($value['#markup']) ? $value['#markup'] : '';
391
    }
392
  }
393 ca0757b9 Assos Assos
394
  // The formatter invoked by file_view_file() can use $file->override to
395
  // customize the returned render array to match the requested settings. To
396
  // support simple formatters that don't do this, set the element attributes to
397
  // what was requested, but not if the formatter applied its own logic for
398
  // element attributes.
399
  if (isset($settings['attributes'])) {
400
    if (empty($element['#attributes'])) {
401
      $element['#attributes'] = $settings['attributes'];
402
    }
403
404
    // While this function may be called for any file type, images are a common
405
    // use-case, and image theme functions have their own structure for render
406
    // arrays.
407
    if (isset($element['#theme'])) {
408
      // theme_image() and theme_image_style() require the 'alt' attributes to
409
      // be passed separately from the 'attributes' array. (see
410
      // http://drupal.org/node/999338). Until that's fixed, implement this
411
      // special-case logic. Image formatters using other theme functions are
412
      // responsible for their own 'alt' attribute handling. See
413
      // theme_media_formatter_large_icon() for an example.
414
      if (in_array($element['#theme'], array('image', 'image_style'))) {
415
        if (empty($element['#alt']) && isset($settings['attributes']['alt'])) {
416
          $element['#alt'] = $settings['attributes']['alt'];
417
        }
418
      }
419
      // theme_image_formatter() and any potential replacements, such as
420
      // theme_colorbox_image_formatter(), also require attribute handling.
421
      elseif (strpos($element['#theme'], 'image_formatter') !== FALSE) {
422
        // theme_image_formatter() requires the attributes to be
423
        // set on the item rather than the element itself.
424
        if (empty($element['#item']['attributes'])) {
425
          $element['#item']['attributes'] = $settings['attributes'];
426
        }
427
428
        // theme_image_formatter() also requires alt, title, height, and
429
        // width attributes to be set on the item rather than within its
430
        // attributes array.
431
        foreach (array('alt', 'title', 'width', 'height') as $attr) {
432
          if (isset($settings['attributes'][$attr])) {
433
            $element['#item'][$attr] = $settings['attributes'][$attr];
434
          }
435
        }
436
      }
437
    }
438
  }
439
440
  return $element;
441
}
442
443
/**
444
 * Returns an array containing the names of all fields that perform text filtering.
445
 */
446
function media_wysiwyg_filter_fields_with_text_filtering($entity_type, $entity) {
447
  list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
448
  $fields = field_info_instances($entity_type, $bundle);
449
450
  // Get all of the fields on this entity that allow text filtering.
451
  $fields_with_text_filtering = array();
452
  foreach ($fields as $field_name => $field) {
453
    if (!empty($field['settings']['text_processing'])) {
454
      $fields_with_text_filtering[] = $field_name;
455
    }
456
  }
457
458
  return $fields_with_text_filtering;
459
}
460 0ccfec7f Assos Assos
461
/**
462
 * Return a list of view modes allowed for a file type.
463
 *
464
 * @param string $file_type
465
 *   A file type machine name.
466
 *
467
 * @return array
468
 *   An array of view modes that can be used on the file type.
469
 */
470
function media_wysiwyg_get_file_type_view_mode_options($file_type) {
471
  $enabled_view_modes = &drupal_static(__FUNCTION__, array());
472
473
  // @todo Add more caching for this.
474
  if (!isset($enabled_view_modes[$file_type])) {
475
    $enabled_view_modes[$file_type] = array();
476
477
    // Add the default view mode by default.
478
    $enabled_view_modes[$file_type]['default'] = t('Default');
479
480
    $entity_info = entity_get_info('file');
481
    $view_mode_settings = field_view_mode_settings('file', $file_type);
482
    foreach ($entity_info['view modes'] as $view_mode => $view_mode_info) {
483
      // Do not show view modes that don't have their own settings and will
484
      // only fall back to the default view mode.
485
      if (empty($view_mode_settings[$view_mode]['custom_settings'])) {
486
        continue;
487
      }
488
489
      // Don't present the user with an option to choose a view mode in which
490
      // the file is hidden.
491
      $extra_fields = field_extra_fields_get_display('file', $file_type, $view_mode);
492
      if (empty($extra_fields['file']['visible'])) {
493
        continue;
494
      }
495
496
      // Add the view mode to the list of enabled view modes.
497
      $enabled_view_modes[$file_type][$view_mode] = $view_mode_info['label'];
498
    }
499
  }
500
501
  return $enabled_view_modes[$file_type];
502
}
503
504
/**
505
 * Return a list of view modes allowed for a file embedded in the WYSIWYG.
506
 *
507
 * @param object $file
508
 *   A file entity.
509
 *
510
 * @return array
511
 *   An array of view modes that can be used on the file when embedded in the
512
 *   WYSIWYG.
513
 */
514
function media_wysiwyg_get_file_view_mode_options($file) {
515
  $view_modes = media_wysiwyg_get_file_type_view_mode_options($file->type);
516
  drupal_alter('media_wysiwyg_allowed_view_modes', $view_modes, $file);
517
  // Invoke the deprecated/misspelled alter hook as well.
518
  drupal_alter('media_wysiwyg_wysiwyg_allowed_view_modes', $view_modes, $file);
519
  return $view_modes;
520
}
521 da542b7b Assos Assos
522
/**
523
 * Implements hook_file_displays_alter().
524
 */
525
function media_wysiwyg_file_displays_alter(&$displays, $file, $view_mode) {
526
  // Override the fields of the file when requested by the WYSIWYG.
527
  if (isset($file->override) && isset($file->override['fields'])) {
528
    $instance = field_info_instances('file', $file->type);
529
    foreach ($file->override['fields'] as $field_name => $value) {
530
      if (!isset($instance[$field_name]['settings']) || !isset($instance[$field_name]['settings']['wysiwyg_override']) || $instance[$field_name]['settings']['wysiwyg_override']) {
531
        $file->{$field_name} = $value;
532
      }
533
    }
534
  }
535
}
536 1e39edcb Assos Assos
537
/**
538
 * Implements hook_entity_info_alter().
539
 *
540
 * Add a file type named 'WYSIWYG'.
541
 */
542
function media_wysiwyg_entity_info_alter(&$entity_info) {
543
  $entity_info['file']['view modes'] += array(
544
    'wysiwyg' => array(
545
      'label' => t('WYSIWYG'),
546
      'custom settings' => TRUE,
547
    ),
548
  );
549
}
550
551
/**
552
 * Implements hook_form_FORM_ID_alter().
553
 *
554
 * Add select when editing file types to set wysiwyg view mode.
555
 */
556
function media_wysiwyg_form_file_entity_file_type_form_alter(&$form, &$form_state) {
557
  $options = array();
558
559
  // Add an option allowing users not to use a view mode.
560
  $options['none'] = t('None');
561
562
  // Add the default view mode by default.
563
  $options['default'] = t('Default');
564
565
  $entity_info = entity_get_info('file');
566
  foreach ($entity_info['view modes'] as $view_mode => $view_mode_info) {
567
    $options[$view_mode] = check_plain($view_mode_info['label']);
568
  }
569
570
  $file_type = $form['#file_type']->type;
571
  $view_mode = db_query('SELECT view_mode FROM {media_view_mode_wysiwyg} WHERE type = :type', array(':type' => $file_type))->fetchField();
572
  $view_mode = empty($view_mode) ? 'none' : $view_mode;
573
574
  $form['file_wysiwyg_view_mode'] = array(
575
    '#type' => 'select',
576
    '#title' => t('WYSIWYG view mode'),
577
    '#options' => $options,
578
    '#default_value' => $view_mode,
579
    '#description' => t('View mode to be used when displaying files inside of the WYSIWYG editor.'),
580
  );
581
582
  // Move submit after our select box. There might be a better way to do this.
583
  $form['submit']['#weight'] = 1;
584
585
  array_unshift($form['#submit'], 'media_wysiwyg_form_file_entity_file_type_form_alter_submit');
586
}
587
588
/**
589
 * Custom submit handler.
590
 *
591
 * Save wysiwyg view mode.
592
 *
593
 * @see media_wysiwyg_form_file_entity_file_type_form_alter().
594
 */
595
function media_wysiwyg_form_file_entity_file_type_form_alter_submit(&$form, &$form_state) {
596
  $file_type = $form['#file_type']->type;
597
  $view_mode = $form_state['values']['file_wysiwyg_view_mode'];
598
  db_delete('media_view_mode_wysiwyg')->condition('type', $file_type)->execute();
599
  if ($view_mode != 'none') {
600
    $record = array('type' => $file_type, 'view_mode' => $view_mode);
601
    drupal_write_record('media_view_mode_wysiwyg', $record);
602
  }
603
}
604
605
/**
606
 * Implements hook_form_FORM_ID_alter().
607
 *
608
 * Add checkbox to restrict file type view mode availability in wysiwyg.
609
 */
610
function media_wysiwyg_form_file_entity_file_display_form_alter(&$form, &$form_state) {
611
  $file_type = $form['#file_type'];
612
  $view_mode = $form['#view_mode'];
613
614
  if ($view_mode != 'none') {
615
    $restricted = db_query(
616
      'SELECT 1 FROM {media_restrict_wysiwyg} WHERE type = :type and display = :display',
617
      array(':type' => $file_type, ':display' => $view_mode)
618
    )->fetchField();
619
    $form['restrict_wysiwyg'] = array(
620
      '#type' => 'checkbox',
621
      '#title' => t('Restrict in WYSIWYG'),
622
      '#description' => t('If checked, then this mode will not be allowed from the WYSIWYG.'),
623
      '#default_value' => !empty($restricted),
624
    );
625
    array_unshift($form['#submit'], 'media_wysiwyg_form_file_entity_file_display_form_alter_submit');
626
  }
627
628
  return $form;
629
}
630
631
/**
632
 * Custom submit handler.
633
 *
634
 * Save restricted wysiwyg file types.
635
 *
636
 * @see media_wysiwyg_form_file_entity_file_display_form_alter().
637
 */
638
function media_wysiwyg_form_file_entity_file_display_form_alter_submit(&$form, &$form_state) {
639
  $file_type = $form['#file_type'];
640
  $view_mode = $form['#view_mode'];
641
  db_delete('media_restrict_wysiwyg')->condition('type', $file_type)->condition('display', $view_mode)->execute();
642
  if (!empty($form_state['values']['restrict_wysiwyg'])) {
643
    $record = array('type' => $file_type, 'display' => $view_mode);
644
    drupal_write_record('media_restrict_wysiwyg', $record);
645
  }
646
}