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
|
if (!empty($instance['settings']['wysiwyg_insert'])) {
|
129
|
$element['#attached']['js'][] = array(
|
130
|
'type' => 'setting',
|
131
|
'data' => array(
|
132
|
'media_ckeditor' => array(
|
133
|
'wysiwyg_insert' => array(
|
134
|
$element['#field_name'] => TRUE,
|
135
|
),
|
136
|
),
|
137
|
),
|
138
|
);
|
139
|
}
|
140
|
return $element;
|
141
|
}
|
142
|
|
143
|
|
144
|
/**
|
145
|
* Helper function to tell whether we should render full entities in WYSIWYGs.
|
146
|
*/
|
147
|
function _media_ckeditor_fully_rendered_files_in_wysiwyg() {
|
148
|
if (variable_get('media_ckeditor_fully_rendered_files', FALSE) &&
|
149
|
user_access('view overridden file entities in wysiwyg')) {
|
150
|
return TRUE;
|
151
|
}
|
152
|
return FALSE;
|
153
|
}
|
154
|
|
155
|
/**
|
156
|
* Implements hook_menu().
|
157
|
*/
|
158
|
function media_ckeditor_menu() {
|
159
|
$items = array();
|
160
|
|
161
|
// Add our AJAX-friendly route for rendering the file entities for use in
|
162
|
// the WYSIWYG editor.
|
163
|
$items['media/rendered-in-wysiwyg'] = array(
|
164
|
'page callback' => 'media_ckeditor_rendered_file_in_wysiwyg',
|
165
|
'access callback' => '_media_ckeditor_fully_rendered_files_in_wysiwyg',
|
166
|
'file' => 'includes/media_ckeditor.pages.inc',
|
167
|
'theme callback' => 'ajax_base_page_theme',
|
168
|
'type' => MENU_CALLBACK,
|
169
|
);
|
170
|
|
171
|
return $items;
|
172
|
}
|
173
|
|
174
|
/**
|
175
|
* A permission to allow users to view overridden files in the WYSIWYG.
|
176
|
*/
|
177
|
function media_ckeditor_permission() {
|
178
|
return array(
|
179
|
'view overridden file entities in wysiwyg' => array(
|
180
|
'title' => t('View overridden file entities in wysiwyg'),
|
181
|
'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.'),
|
182
|
),
|
183
|
);
|
184
|
}
|
185
|
|
186
|
/**
|
187
|
* Implements hook_form_FORM_ID_alter().
|
188
|
*/
|
189
|
function media_ckeditor_form_media_admin_config_browser_alter(&$form, &$form_state) {
|
190
|
|
191
|
// Add our optional settings to the Media Browser admin form.
|
192
|
$form['media_ckeditor'] = array(
|
193
|
'#type' => 'fieldset',
|
194
|
'#title' => t('Ckeditor WYSIWYG configuration'),
|
195
|
'#collapsible' => TRUE,
|
196
|
'#collapsed' => FALSE,
|
197
|
'#weight' => 50,
|
198
|
);
|
199
|
$form['media_ckeditor']['media_ckeditor_fully_rendered_files'] = array(
|
200
|
'#type' => 'checkbox',
|
201
|
'#title' => t('Display fully rendered files in WYSIWYG'),
|
202
|
'#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"'),
|
203
|
'#default_value' => variable_get('media_ckeditor_fully_rendered_files', FALSE),
|
204
|
);
|
205
|
}
|
206
|
|
207
|
/**
|
208
|
* Implements hook_media_wysiwyg_token_to_markup_alter().
|
209
|
*/
|
210
|
function media_ckeditor_media_wysiwyg_token_to_markup_alter(&$element, $tag_info, $settings) {
|
211
|
|
212
|
// Check to see if files embedded in WYSIWYG should be fully rendered. If so,
|
213
|
// replace the simpler Media placeholder with a fully rendered file entity.
|
214
|
if (!empty($settings['wysiwyg']) &&
|
215
|
_media_ckeditor_fully_rendered_files_in_wysiwyg()) {
|
216
|
$file = $tag_info['file'];
|
217
|
$file->override['wysiwyg'] = TRUE;
|
218
|
$view_mode = $tag_info['view_mode'];
|
219
|
$fields = (!empty($settings['fields'])) ? $settings['fields'] : array();
|
220
|
$element = media_ckeditor_render_file_with_overrides($file, $view_mode, $fields);
|
221
|
}
|
222
|
}
|
223
|
|
224
|
/**
|
225
|
* Helper function to get a fully rendered file entity with optional overrides.
|
226
|
*/
|
227
|
function media_ckeditor_render_file_with_overrides($file, $view_mode, $fields = array()) {
|
228
|
|
229
|
foreach ($fields as $field_name => $value) {
|
230
|
if (isset($file->{$field_name})) {
|
231
|
$file->{$field_name} = $value;
|
232
|
}
|
233
|
}
|
234
|
|
235
|
$file_array = array($file->fid => $file);
|
236
|
$build = file_entity_metadata_view_file($file_array, $view_mode);
|
237
|
|
238
|
// Manually remove contextual links.
|
239
|
if (!empty($build['file'][$file->fid]['#contextual_links'])) {
|
240
|
unset($build['file'][$file->fid]['#contextual_links']);
|
241
|
}
|
242
|
|
243
|
return $build;
|
244
|
}
|
245
|
|
246
|
/**
|
247
|
* Implements hook_preprocess_file_entity().
|
248
|
*/
|
249
|
function media_ckeditor_preprocess_file_entity(&$vars) {
|
250
|
|
251
|
// If we are rendering this as a fully rendered file entity in a WYSIWYG,
|
252
|
// set $page to TRUE so that the title doesn't display.
|
253
|
// @see file_entity/file_entity.tpl.php.
|
254
|
if (!empty($vars['override']['wysiwyg']) &&
|
255
|
_media_ckeditor_fully_rendered_files_in_wysiwyg()) {
|
256
|
$vars['page'] = TRUE;
|
257
|
}
|
258
|
}
|