Projet

Général

Profil

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

root / drupal7 / sites / all / modules / media_ckeditor / media_ckeditor.module @ 5d940405

1
<?php
2

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

    
8
/**
9
 * Implements hook_element_info_alter().
10
 */
11
function media_ckeditor_element_info_alter(&$types) {
12
  $types['text_format']['#pre_render'][] = 'media_ckeditor_pre_render_text_format';
13
  $types['media']['#pre_render'][] = 'media_ckeditor_pre_render_media';
14
}
15

    
16
/**
17
 * Adds CKEditor-specific JavaScript.
18
 */
19
function media_ckeditor_pre_render_text_format($element) {
20
  // filter_process_format() copies properties to the expanded 'value' child
21
  // element.
22
  if (!isset($element['format'])) {
23
    return $element;
24
  }
25

    
26
  $field = &$element['value'];
27
  $settings = array(
28
    'field' => $field['#id'],
29
  );
30

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

    
35
  // Add CKEditor-specific JS.
36
  $element['#attached']['js'][] = array(
37
    'data' => drupal_get_path('module', 'media_ckeditor') . '/js/plugins/media/library.js',
38
    'type' => 'file',
39
    'scope' => 'footer',
40
    // It's important that this javascript fire after CKEditor.
41
    'weight' => 20,
42
  );
43

    
44
  // Communicate to javascript whether we will be displaying fully rendered
45
  // file entities in the WYSIWYG.
46
  $element['#attached']['js'][] = array(
47
    'data' => array(
48
      'media_ckeditor' => array(
49
        'fully_rendered_files' => _media_ckeditor_fully_rendered_files_in_wysiwyg(),
50
        'labels' => array(
51
          'settings' => t('Media settings'),
52
          'add' => t('Add media'),
53
        ),
54
      ),
55
    ),
56
    'type' => 'setting',
57
  );
58

    
59
  return $element;
60
}
61

    
62
/**
63
 * Implements hook_form_ID_alter().
64
 */
65
function media_ckeditor_form_media_wysiwyg_format_form_alter(&$form, $form_state) {
66
  // Add our overrides to the media format form javascript.
67
  $form['#attached']['js'][] = drupal_get_path('module', 'media_ckeditor') . '/js/media_ckeditor.format_form.js';
68
}
69

    
70
/**
71
 * Implements hook_media_browser_params_alter().
72
 */
73
function media_ckeditor_media_browser_params_alter(&$stored_params) {
74
  // We add this custom param when the media dialog is invoked in library.js.
75
  if (isset($stored_params['id']) && $stored_params['id'] == 'media_wysiwyg') {
76
    // Set the default browser params from settings form if not already set.
77
    if (empty($stored_params['enabledPlugins'])) {
78
      $stored_params['enabledPlugins'] = variable_get('media_wysiwyg_wysiwyg_browser_plugins', array());
79
    }
80
    if (empty($stored_params['file_directory'])) {
81
      $stored_params['file_directory'] = variable_get('media_wysiwyg_wysiwyg_upload_directory', '');
82
    }
83
    if (empty($stored_params['types'])) {
84
      $stored_params['types'] = variable_get('media_wysiwyg_wysiwyg_allowed_types', array(
85
        'audio',
86
        'image',
87
        'video',
88
        'document',
89
      ));
90
    }
91
    if (empty($stored_params['file_extensions'])) {
92
      $stored_params['file_extensions'] = variable_get('media_wysiwyg_wysiwyg_allowed_file_extensions',
93
        'jpg jpeg gif png txt doc docx xls xlsx pdf ppt pptx pps ppsx odt ods odp mp3 mov mp4 m4a m4v mpeg avi ogg oga ogv weba webp webm'
94
      );
95
    }
96
  }
97
}
98

    
99
/**
100
 * Implements hook_form_FORM_ID_alter().
101
 */
102
function media_ckeditor_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
103
  // Add a checkbox that marks this field as needing an "Insert" button.
104
  if (!empty($form['#instance']['widget']['module'])) {
105
    if ('media' === $form['#instance']['widget']['module']) {
106
      $form['instance']['settings']['wysiwyg_insert'] = array(
107
        '#type' => 'checkbox',
108
        '#title' => t('Show button for inserting files into WYSIWYG editor'),
109
        '#description' => t('If checked, then an "Insert" button will be available to put attached files into the active WYSIWYG editor.'),
110
        '#default_value' => isset($form['#instance']['settings']['wysiwyg_insert']) ? $form['#instance']['settings']['wysiwyg_insert'] : FALSE,
111
      );
112
    }
113
  }
114
}
115

    
116
/**
117
 * Pre-render callback for media elements.
118
 */
119
function media_ckeditor_pre_render_media($element) {
120
  // If the media widget has been configured to have an Insert button, we'll
121
  // communicate that information through javascript settings.
122
  if (isset($element['#entity_type']) && isset($element['#field_name']) && isset($element['#bundle'])) {
123
    $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
124
  }
125
  else {
126
    $instance = FALSE;
127
  }
128
  // Build JavaScript data to be attached to the element.
129
  $data['media_ckeditor']['image_properties'] = variable_get('media_ckeditor_image_properties', FALSE);
130
  if (!empty($instance['settings']['wysiwyg_insert'])) {
131
    $data['media_ckeditor']['wysiwyg_insert'][$element['#field_name']] = TRUE;
132
  }
133
  $element['#attached']['js'][] = array(
134
    'type' => 'setting',
135
    'data' => $data,
136
  );
137
  return $element;
138
}
139

    
140

    
141
/**
142
 * Helper function to tell whether we should render full entities in WYSIWYGs.
143
 */
144
function _media_ckeditor_fully_rendered_files_in_wysiwyg() {
145
  if (variable_get('media_ckeditor_fully_rendered_files', FALSE) &&
146
      user_access('view overridden file entities in wysiwyg')) {
147
    return TRUE;
148
  }
149
  return FALSE;
150
}
151

    
152
/**
153
 * Implements hook_menu().
154
 */
155
function media_ckeditor_menu() {
156
  $items = array();
157

    
158
  // Add our AJAX-friendly route for rendering the file entities for use in
159
  // the WYSIWYG editor.
160
  $items['media/rendered-in-wysiwyg'] = array(
161
    'page callback' => 'media_ckeditor_rendered_file_in_wysiwyg',
162
    'access callback' => '_media_ckeditor_fully_rendered_files_in_wysiwyg',
163
    'file' => 'includes/media_ckeditor.pages.inc',
164
    'theme callback' => 'ajax_base_page_theme',
165
    'type' => MENU_CALLBACK,
166
  );
167

    
168
  return $items;
169
}
170

    
171
/**
172
 * A permission to allow users to view overridden files in the WYSIWYG.
173
 */
174
function media_ckeditor_permission() {
175
  return array(
176
    'view overridden file entities in wysiwyg' => array(
177
      'title' => t('View overridden file entities in wysiwyg'),
178
      'description' => t('When users are embedding files in WYSIWYGs, the file entities will be displayed with whatever overrides the user may have specified in the Media popup.'),
179
    ),
180
  );
181
}
182

    
183
/**
184
 * Implements hook_form_FORM_ID_alter().
185
 */
186
function media_ckeditor_form_media_admin_config_browser_alter(&$form, &$form_state) {
187

    
188
  // Add our optional settings to the Media Browser admin form.
189
  $form['media_ckeditor'] = array(
190
    '#type' => 'fieldset',
191
    '#title' => t('Ckeditor WYSIWYG configuration'),
192
    '#collapsible' => TRUE,
193
    '#collapsed' => FALSE,
194
    '#weight' => 50,
195
  );
196
  $form['media_ckeditor']['media_ckeditor_fully_rendered_files'] = array(
197
    '#type' => 'checkbox',
198
    '#title' => t('Display fully rendered files in WYSIWYG'),
199
    '#description' => t('Check this box to render files in the WYSIWYG dynamically, to reflect any field overrides the user may have specified. Note that this requires the user has the corresponding permission: "View overridden file entities in wysiwyg"'),
200
    '#default_value' => variable_get('media_ckeditor_fully_rendered_files', FALSE),
201
  );
202
  $form['media_ckeditor']['media_ckeditor_image_properties'] = array(
203
    '#type' => 'checkbox',
204
    '#title' => t('Allow Image Properties configuration'),
205
    '#description' => t('Check this box to allow images inserted with the Media module to be resized and edited using the Image Properties menu option. Otherwise, resizing options should be provided as different image view modes.'),
206
    '#default_value' => variable_get('media_ckeditor_image_properties', FALSE),
207
  );
208
}
209

    
210
/**
211
 * Implements hook_media_wysiwyg_token_to_markup_alter().
212
 */
213
function media_ckeditor_media_wysiwyg_token_to_markup_alter(&$element, $tag_info, $settings) {
214

    
215
  // Check to see if files embedded in WYSIWYG should be fully rendered. If so,
216
  // replace the simpler Media placeholder with a fully rendered file entity.
217
  if (!empty($settings['wysiwyg']) &&
218
      _media_ckeditor_fully_rendered_files_in_wysiwyg()) {
219
    $file = $tag_info['file'];
220
    $file->override['wysiwyg'] = TRUE;
221
    $view_mode = $tag_info['view_mode'];
222
    $fields = (!empty($settings['fields'])) ? $settings['fields'] : array();
223
    $element = media_ckeditor_render_file_with_overrides($file, $view_mode, $fields);
224
  }
225
}
226

    
227
/**
228
 * Helper function to get a fully rendered file entity with optional overrides.
229
 */
230
function media_ckeditor_render_file_with_overrides($file, $view_mode, $fields = array()) {
231

    
232
  foreach ($fields as $field_name => $value) {
233
    if (isset($file->{$field_name})) {
234
      $file->{$field_name} = $value;
235
    }
236
  }
237

    
238
  $file_array = array($file->fid => $file);
239
  $build = file_entity_metadata_view_file($file_array, $view_mode);
240

    
241
  // Manually remove contextual links.
242
  if (!empty($build['file'][$file->fid]['#contextual_links'])) {
243
    unset($build['file'][$file->fid]['#contextual_links']);
244
  }
245

    
246
  return $build;
247
}
248

    
249
/**
250
 * Implements hook_preprocess_file_entity().
251
 */
252
function media_ckeditor_preprocess_file_entity(&$vars) {
253

    
254
  // If we are rendering this as a fully rendered file entity in a WYSIWYG,
255
  // set $page to TRUE so that the title doesn't display.
256
  // @see file_entity/file_entity.tpl.php.
257
  if (!empty($vars['override']['wysiwyg']) &&
258
      _media_ckeditor_fully_rendered_files_in_wysiwyg()) {
259
    $vars['page'] = TRUE;
260
  }
261
}