summaryrefslogtreecommitdiff
path: root/sites/all/modules/media/modules/media_wysiwyg/media_wysiwyg.module
blob: 162e60bb25183e347a81511ba4656d2796092c8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php

/**
 * @file
 * Primarily Drupal hooks.
 */

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

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

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

/**
 * Implements hook_hook_info().
 */
function media_wysiwyg_hook_info() {
  $hooks = array(
    'media_wysiwyg_token_to_markup_alter',
    'media_wysiwyg_allowed_view_modes_alter',
    'media_wysiwyg_format_form_prepare_alter',
  );

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

/**
 * Implements hook_menu().
 */
function media_wysiwyg_menu() {
  $items = array();

  $items['media/%file/format-form'] = array(
    'title' => 'Style selector',
    'description' => 'Choose a format for a piece of media',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('media_wysiwyg_format_form', 1),
    'access callback' => 'media_wysiwyg_access',
    'access arguments' => array('view', 1),
    'file' => 'includes/media_wysiwyg.pages.inc',
    'theme callback' => 'media_dialog_get_theme_name',
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Implements hook_permission().
 */
function media_wysiwyg_permission() {
  return array(
    'use media wysiwyg' => array(
      'title' => t('Use Media WYSIWYG in an editor'),
      // Marked restrict because the WYSIWYG forms generate image derivatives,
      // which could lead to a DoS security vulnerability.
      'restrict access' => TRUE,
    ),
  );
}

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

/**
 * Implements hook_element_info_alter().
 */
function media_wysiwyg_element_info_alter(&$types) {
  $types['text_format']['#pre_render'][] = '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.
 */
function media_wysiwyg_pre_render_text_format($element) {
  // filter_process_format() copies properties to the expanded 'value' child
  // element.
  if (!isset($element['format'])) {
    return $element;
  }

  $field = &$element['value'];
  $settings = array(
    'field' => $field['#id'],
  );

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

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

  if (isset($tagmap)) {
    $element['#attached']['js'][] = array(
      'data' => array(
        'tagmap' => $tagmap,
      ),
      'type' => 'setting',
    );
  }

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

  // Add wysiwyg-specific settings.
  $settings = array('wysiwyg_allowed_attributes' => variable_get('media_wysiwyg_wysiwyg_allowed_attributes', _media_wysiwyg_wysiwyg_allowed_attributes_default()));
  $element['#attached']['js'][] = array(
    'data' => array(
      'media' => $settings,
    ),
    'type' => 'setting',
  );

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

  // Add CKEditor-specific JS.
  if (module_exists('ckeditor')) {
    $element['#attached']['js'][] = array(
      'data' => drupal_get_path('module', 'media_wysiwyg') . '/wysiwyg_plugins/media_ckeditor/library.js',
      'type' => 'file',
      'scope' => 'footer',
      'weight' => -20,
    );
  }

  return $element;
}

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

/**
 * Element validate callback for the media WYSIWYG button.
 */
function media_wysiwyg_wysiwyg_button_element_validate($element, &$form_state) {
  if (!empty($element['#value'])) {
    $format = filter_format_load($form_state['build_info']['args'][0]->format);
    $filters = filter_list_format($format->format);
    if (empty($filters['media_filter']->status)) {
      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(
        '@format-link' => url('admin/config/content/formats/' . $format->format, array('query' => array('destination' => $_GET['q']))),
        '@format' => $format->name,
      )));
    }
  }

  return $element;
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function media_wysiwyg_form_media_admin_config_browser_alter(&$form, &$form_state) {
  $form['wysiwyg'] = array(
    '#type' => 'fieldset',
    '#title' => t('WYSIWYG configuration'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['wysiwyg']['media_wysiwyg_wysiwyg_browser_plugins'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Enabled browser plugins'),
    '#options' => array(),
    '#required' => FALSE,
    '#default_value' => variable_get('media_wysiwyg_wysiwyg_browser_plugins', array()),
    '#description' => t('If no plugins are selected, they will all be available.'),
  );

  $plugins = media_get_browser_plugin_info();

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

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

  if (module_exists('token')) {
    $form['wysiwyg']['media_wysiwyg_wysiwyg_upload_directory']['#description'] .= t('This field supports tokens.');
    $form['wysiwyg']['tokens'] = array(
      '#theme' => 'token_tree',
      '#dialog' => TRUE,
    );
  }

  $form['wysiwyg']['media_wysiwyg_default_render'] = array(
    '#type' => 'radios',
    '#title' => t('How should file entities be rendered within a text field?'),
    '#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."),
    '#options' => array(
      'file_entity' => 'Full file entity rendering',
      'field_attach' => 'Legacy rendering (using field attach)',
    ),
    '#default_value' => variable_get('media_wysiwyg_default_render', 'file_entity'),
  );

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

  $form['#submit'][] = 'media_wysiwyg_admin_config_browser_pre_submit';
}

/**
 * Manipulate values before form is submitted.
 */
function media_wysiwyg_admin_config_browser_pre_submit(&$form, &$form_state) {
  $wysiwyg_browser_plugins = array_unique(array_values($form_state['values']['media_wysiwyg_wysiwyg_browser_plugins']));
  if (empty($wysiwyg_browser_plugins[0])) {
    variable_del('media_wysiwyg_wysiwyg_browser_plugins');
    unset($form_state['values']['media_wysiwyg_wysiwyg_browser_plugins']);
  }
}

/**
 * Implements hook_filter_info().
 */
function media_wysiwyg_filter_info() {
  $filters['media_filter'] = array(
    'title' => t('Convert Media tags to markup'),
    '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.'),
    'process callback' => 'media_wysiwyg_filter',
    'weight' => 2,
    // @TODO not implemented
    'tips callback' => 'media_filter_tips',
  );

  return $filters;
}

/**
 * Implements hook_wysiwyg_include_directory().
 */
function media_wysiwyg_wysiwyg_include_directory($type) {
  switch ($type) {
    case 'plugins':
      return 'wysiwyg_plugins';

      break;
  }
}

/**
 * Returns the default set of allowed attributes for use with WYSIWYG.
 *
 * @return array
 *   An array of whitelisted attributes.
 */
function _media_wysiwyg_wysiwyg_allowed_attributes_default() {
  return array(
    'alt',
    'title',
    'height',
    'width',
    'hspace',
    'vspace',
    'border',
    'align',
    'style',
    'class',
    'id',
    'usemap',
    'data-picture-group',
    'data-picture-align',
    'data-picture-mapping',
  );
}

/**
 * Returns a drupal_render() array for just the file portion of a file entity.
 *
 * Optional custom settings can override how the file is displayed.
 */
function media_wysiwyg_get_file_without_label($file, $view_mode, $settings = array()) {
  $file->override = $settings;

  $element = file_view_file($file, $view_mode);

  // 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'];
        }
      }
      // theme_image_formatter() and any potential replacements, such as
      // theme_colorbox_image_formatter(), also require attribute handling.
      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;
}

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

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

  return $fields_with_text_filtering;
}