Projet

Général

Profil

Paste
Télécharger (10,5 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / media / modules / media_wysiwyg / media_wysiwyg.module @ ca0757b9

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
/**
18
 * Implements hook_hook_info().
19
 */
20
function media_wysiwyg_hook_info() {
21
  $hooks = array(
22
    'media_wysiwyg_token_to_markup_alter',
23
    'media_wysiwyg_allowed_view_modes_alter',
24
    'media_wysiwyg_format_form_prepare_alter',
25
  );
26

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

    
30
/**
31
 * Implements hook_menu().
32
 */
33
function media_wysiwyg_menu() {
34
  $items['media/%file/format-form'] = array(
35
    'title' => 'Style selector',
36
    'description' => 'Choose a format for a piece of media',
37
    'page callback' => 'drupal_get_form',
38
    'page arguments' => array('media_wysiwyg_format_form', 1),
39
    'access callback' => 'file_entity_access',
40
    'access arguments' => array('view', 1),
41
    'file' => 'includes/media_wysiwyg.pages.inc',
42
    'theme callback' => 'media_dialog_get_theme_name',
43
    'type' => MENU_CALLBACK,
44
  );
45

    
46
  return $items;
47
}
48

    
49
/**
50
 * Implements hook_element_info_alter().
51
 */
52
function media_wysiwyg_element_info_alter(&$types) {
53
  $types['text_format']['#pre_render'][] = 'media_wysiwyg_pre_render_text_format';
54
}
55

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

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

    
81
  return $element;
82
}
83

    
84
/**
85
 * Implements hook_form_FORM_ID_alter().
86
 */
87
function media_wysiwyg_form_media_admin_config_browser_alter(&$form, &$form_state) {
88
  $form['wysiwyg'] = array(
89
    '#type' => 'fieldset',
90
    '#title' => t('WYSIWYG configuration'),
91
    '#collapsible' => TRUE,
92
    '#collapsed' => FALSE,
93
  );
94
  $form['wysiwyg']['media_wysiwyg_wysiwyg_browser_plugins'] = array(
95
    '#type' => 'checkboxes',
96
    '#title' => t('Enabled browser plugins'),
97
    '#options' => array(),
98
    '#required' => FALSE,
99
    '#default_value' => variable_get('media_wysiwyg_wysiwyg_browser_plugins', array()),
100
    '#description' => t('If no plugins are selected, they will all be available.'),
101
  );
102

    
103
  $plugins = media_get_browser_plugin_info();
104

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

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

    
116
  if (module_exists('token')) {
117
    $form['wysiwyg']['media_wysiwyg_wysiwyg_upload_directory']['#description'] .= t('This field supports tokens.');
118
    $form['wysiwyg']['tokens'] = array(
119
      '#theme' => 'token_tree',
120
      '#dialog' => TRUE,
121
    );
122
  }
123

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

    
131
  $form['#submit'][] = 'media_wysiwyg_admin_config_browser_pre_submit';
132
}
133

    
134
/**
135
 * Manipulate values before form is submitted.
136
 */
137
function media_wysiwyg_admin_config_browser_pre_submit(&$form, &$form_state) {
138
  $wysiwyg_browser_plugins = array_unique(array_values($form_state['values']['media_wysiwyg_wysiwyg_browser_plugins']));
139
  if (empty($wysiwyg_browser_plugins[0])) {
140
    variable_del('media_wysiwyg_wysiwyg_browser_plugins');
141
    unset($form_state['values']['media_wysiwyg_wysiwyg_browser_plugins']);
142
  }
143
}
144

    
145
/**
146
 * Implements hook_filter_info().
147
 */
148
function media_wysiwyg_filter_info() {
149
  $filters['media_filter'] = array(
150
    'title' => t('Convert Media tags to markup'),
151
    '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.'),
152
    'process callback' => 'media_wysiwyg_filter',
153
    'weight' => 2,
154
    // @TODO not implemented
155
    'tips callback' => 'media_filter_tips',
156
  );
157

    
158
  return $filters;
159
}
160

    
161
/**
162
 * Prepares the page to be able to launch the media browser.
163
 *
164
 * Defines default variables.
165
 */
166
function media_wysiwyg_include_browser_js() {
167
  static $included;
168
  if ($included) {
169
    return;
170
  }
171
  $included = TRUE;
172
  module_load_include('inc', 'media', 'includes/media.browser');
173
  $javascript = media_browser_js();
174
  foreach ($javascript as $key => $definitions) {
175
    foreach ($definitions as $definition) {
176
      $function = 'drupal_add_' . $key;
177
      // Since the arguments to pass are variable, use call_user_func_array().
178
      // This will not handle all potential drupal_add_*() functions directly
179
      // but covers the js and library needed here, which are unlikely to be
180
      // expanded since this function is only a workaround for a wysiwyg
181
      // limitation.
182
      call_user_func_array($function, $definition);
183
    }
184
  }
185
  // Add wysiwyg-specific settings.
186
  $settings = array('wysiwyg_allowed_attributes' => variable_get('media_wysiwyg_wysiwyg_allowed_attributes', _media_wysiwyg_wysiwyg_allowed_attributes_default()));
187
  drupal_add_js(array('media' => $settings), 'setting');
188
}
189

    
190
/**
191
 * Implements hook_wysiwyg_include_directory().
192
 */
193
function media_wysiwyg_wysiwyg_include_directory($type) {
194
  switch ($type) {
195
    case 'plugins':
196
      return 'wysiwyg_plugins';
197

    
198
      break;
199
  }
200
}
201

    
202
/**
203
 * Returns the default set of allowed attributes for use with WYSIWYG.
204
 *
205
 * @return array
206
 *   An array of whitelisted attributes.
207
 */
208
function _media_wysiwyg_wysiwyg_allowed_attributes_default() {
209
  return array(
210
    'alt',
211
    'title',
212
    'height',
213
    'width',
214
    'hspace',
215
    'vspace',
216
    'border',
217
    'align',
218
    'style',
219
    'class',
220
    'id',
221
    'usemap',
222
    'data-picture-group',
223
    'data-picture-align',
224
    'data-picture-mapping',
225
  );
226
}
227

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

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

    
238
  // The formatter invoked by file_view_file() can use $file->override to
239
  // customize the returned render array to match the requested settings. To
240
  // support simple formatters that don't do this, set the element attributes to
241
  // what was requested, but not if the formatter applied its own logic for
242
  // element attributes.
243
  if (isset($settings['attributes'])) {
244
    if (empty($element['#attributes'])) {
245
      $element['#attributes'] = $settings['attributes'];
246
    }
247

    
248
    // While this function may be called for any file type, images are a common
249
    // use-case, and image theme functions have their own structure for render
250
    // arrays.
251
    if (isset($element['#theme'])) {
252
      // theme_image() and theme_image_style() require the 'alt' attributes to
253
      // be passed separately from the 'attributes' array. (see
254
      // http://drupal.org/node/999338). Until that's fixed, implement this
255
      // special-case logic. Image formatters using other theme functions are
256
      // responsible for their own 'alt' attribute handling. See
257
      // theme_media_formatter_large_icon() for an example.
258
      if (in_array($element['#theme'], array('image', 'image_style'))) {
259
        if (empty($element['#alt']) && isset($settings['attributes']['alt'])) {
260
          $element['#alt'] = $settings['attributes']['alt'];
261
        }
262
      }
263
      // theme_image_formatter() and any potential replacements, such as
264
      // theme_colorbox_image_formatter(), also require attribute handling.
265
      elseif (strpos($element['#theme'], 'image_formatter') !== FALSE) {
266
        // theme_image_formatter() requires the attributes to be
267
        // set on the item rather than the element itself.
268
        if (empty($element['#item']['attributes'])) {
269
          $element['#item']['attributes'] = $settings['attributes'];
270
        }
271

    
272
        // theme_image_formatter() also requires alt, title, height, and
273
        // width attributes to be set on the item rather than within its
274
        // attributes array.
275
        foreach (array('alt', 'title', 'width', 'height') as $attr) {
276
          if (isset($settings['attributes'][$attr])) {
277
            $element['#item'][$attr] = $settings['attributes'][$attr];
278
          }
279
        }
280
      }
281
    }
282
  }
283

    
284
  return $element;
285
}
286

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

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

    
302
  return $fields_with_text_filtering;
303
}