Révision 59ae487e
Ajouté par Assos Assos il y a presque 7 ans
drupal7/sites/all/modules/media_ckeditor/media_ckeditor.module | ||
---|---|---|
10 | 10 |
*/ |
11 | 11 |
function media_ckeditor_element_info_alter(&$types) { |
12 | 12 |
$types['text_format']['#pre_render'][] = 'media_ckeditor_pre_render_text_format'; |
13 |
$types['media']['#pre_render'][] = 'media_ckeditor_pre_render_media'; |
|
13 | 14 |
} |
14 | 15 |
|
15 | 16 |
/** |
... | ... | |
36 | 37 |
'data' => drupal_get_path('module', 'media_ckeditor') . '/js/plugins/media/library.js', |
37 | 38 |
'type' => 'file', |
38 | 39 |
'scope' => 'footer', |
39 |
'weight' => -20, |
|
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 |
), |
|
51 |
), |
|
52 |
'type' => 'setting', |
|
40 | 53 |
); |
41 | 54 |
|
42 | 55 |
return $element; |
... | ... | |
73 | 86 |
} |
74 | 87 |
} |
75 | 88 |
} |
89 |
|
|
90 |
/** |
|
91 |
* Implements hook_form_FORM_ID_alter(). |
|
92 |
*/ |
|
93 |
function media_ckeditor_form_field_ui_field_edit_form_alter(&$form, &$form_state) { |
|
94 |
// Add a checkbox that marks this field as needing an "Insert" button. |
|
95 |
if (!empty($form['#instance']['widget']['module'])) { |
|
96 |
if ('media' === $form['#instance']['widget']['module']) { |
|
97 |
$form['instance']['settings']['wysiwyg_insert'] = array( |
|
98 |
'#type' => 'checkbox', |
|
99 |
'#title' => t('Show button for inserting files into WYSIWYG editor'), |
|
100 |
'#description' => t('If checked, then an "Insert" button will be available to put attached files into the active WYSIWYG editor.'), |
|
101 |
'#default_value' => isset($form['#instance']['settings']['wysiwyg_insert']) ? $form['#instance']['settings']['wysiwyg_insert'] : FALSE, |
|
102 |
); |
|
103 |
} |
|
104 |
} |
|
105 |
} |
|
106 |
|
|
107 |
/** |
|
108 |
* Pre-render callback for media elements. |
|
109 |
*/ |
|
110 |
function media_ckeditor_pre_render_media($element) { |
|
111 |
// If the media widget has been configured to have an Insert button, we'll |
|
112 |
// communicate that information through javascript settings. |
|
113 |
$instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']); |
|
114 |
if (!empty($instance['settings']['wysiwyg_insert'])) { |
|
115 |
$element['#attached']['js'][] = array( |
|
116 |
'type' => 'setting', |
|
117 |
'data' => array( |
|
118 |
'media_ckeditor' => array( |
|
119 |
'wysiwyg_insert' => array( |
|
120 |
$element['#field_name'] => TRUE, |
|
121 |
), |
|
122 |
), |
|
123 |
), |
|
124 |
); |
|
125 |
} |
|
126 |
return $element; |
|
127 |
} |
|
128 |
|
|
129 |
|
|
130 |
/** |
|
131 |
* Helper function to tell whether we should render full entities in WYSIWYGs. |
|
132 |
*/ |
|
133 |
function _media_ckeditor_fully_rendered_files_in_wysiwyg() { |
|
134 |
if (variable_get('media_ckeditor_fully_rendered_files', FALSE) && |
|
135 |
user_access('view overridden file entities in wysiwyg')) { |
|
136 |
return TRUE; |
|
137 |
} |
|
138 |
return FALSE; |
|
139 |
} |
|
140 |
|
|
141 |
/** |
|
142 |
* Implements hook_menu(). |
|
143 |
*/ |
|
144 |
function media_ckeditor_menu() { |
|
145 |
$items = array(); |
|
146 |
|
|
147 |
// Add our AJAX-friendly route for rendering the file entities for use in |
|
148 |
// the WYSIWYG editor. |
|
149 |
$items['media/rendered-in-wysiwyg'] = array( |
|
150 |
'page callback' => 'media_ckeditor_rendered_file_in_wysiwyg', |
|
151 |
'access callback' => '_media_ckeditor_fully_rendered_files_in_wysiwyg', |
|
152 |
'file' => 'includes/media_ckeditor.pages.inc', |
|
153 |
'theme callback' => 'ajax_base_page_theme', |
|
154 |
'type' => MENU_CALLBACK, |
|
155 |
); |
|
156 |
|
|
157 |
return $items; |
|
158 |
} |
|
159 |
|
|
160 |
/** |
|
161 |
* A permission to allow users to view overridden files in the WYSIWYG. |
|
162 |
*/ |
|
163 |
function media_ckeditor_permission() { |
|
164 |
return array( |
|
165 |
'view overridden file entities in wysiwyg' => array( |
|
166 |
'title' => t('View overridden file entities in wysiwyg'), |
|
167 |
'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.'), |
|
168 |
), |
|
169 |
); |
|
170 |
} |
|
171 |
|
|
172 |
/** |
|
173 |
* Implements hook_form_FORM_ID_alter(). |
|
174 |
*/ |
|
175 |
function media_ckeditor_form_media_admin_config_browser_alter(&$form, &$form_state) { |
|
176 |
|
|
177 |
// Add our optional settings to the Media Browser admin form. |
|
178 |
$form['media_ckeditor'] = array( |
|
179 |
'#type' => 'fieldset', |
|
180 |
'#title' => t('Ckeditor WYSIWYG configuration'), |
|
181 |
'#collapsible' => TRUE, |
|
182 |
'#collapsed' => FALSE, |
|
183 |
'#weight' => 50, |
|
184 |
); |
|
185 |
$form['media_ckeditor']['media_ckeditor_fully_rendered_files'] = array( |
|
186 |
'#type' => 'checkbox', |
|
187 |
'#title' => t('Display fully rendered files in WYSIWYG'), |
|
188 |
'#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"'), |
|
189 |
'#default_value' => variable_get('media_ckeditor_fully_rendered_files', FALSE), |
|
190 |
); |
|
191 |
} |
|
192 |
|
|
193 |
/** |
|
194 |
* Implements hook_media_wysiwyg_token_to_markup_alter(). |
|
195 |
*/ |
|
196 |
function media_ckeditor_media_wysiwyg_token_to_markup_alter(&$element, $tag_info, $settings) { |
|
197 |
|
|
198 |
// Check to see if files embedded in WYSIWYG should be fully rendered. If so, |
|
199 |
// replace the simpler Media placeholder with a fully rendered file entity. |
|
200 |
if (!empty($settings['wysiwyg']) && |
|
201 |
_media_ckeditor_fully_rendered_files_in_wysiwyg()) { |
|
202 |
$file = $tag_info['file']; |
|
203 |
$file->override['wysiwyg'] = TRUE; |
|
204 |
$view_mode = $tag_info['view_mode']; |
|
205 |
$fields = (!empty($settings['fields'])) ? $settings['fields'] : array(); |
|
206 |
$element = media_ckeditor_render_file_with_overrides($file, $view_mode, $fields); |
|
207 |
} |
|
208 |
} |
|
209 |
|
|
210 |
/** |
|
211 |
* Helper function to get a fully rendered file entity with optional overrides. |
|
212 |
*/ |
|
213 |
function media_ckeditor_render_file_with_overrides($file, $view_mode, $fields = array()) { |
|
214 |
|
|
215 |
foreach ($fields as $field_name => $value) { |
|
216 |
if (isset($file->{$field_name})) { |
|
217 |
$file->{$field_name} = $value; |
|
218 |
} |
|
219 |
} |
|
220 |
|
|
221 |
$file_array = array($file->fid => $file); |
|
222 |
$build = file_entity_metadata_view_file($file_array, $view_mode); |
|
223 |
|
|
224 |
// Manually remove contextual links. |
|
225 |
if (!empty($build['file'][$file->fid]['#contextual_links'])) { |
|
226 |
unset($build['file'][$file->fid]['#contextual_links']); |
|
227 |
} |
|
228 |
|
|
229 |
return $build; |
|
230 |
} |
|
231 |
|
|
232 |
/** |
|
233 |
* Implements hook_preprocess_file_entity(). |
|
234 |
*/ |
|
235 |
function media_ckeditor_preprocess_file_entity(&$vars) { |
|
236 |
|
|
237 |
// If we are rendering this as a fully rendered file entity in a WYSIWYG, |
|
238 |
// set $page to TRUE so that the title doesn't display. |
|
239 |
// @see file_entity/file_entity.tpl.php. |
|
240 |
if (!empty($vars['override']['wysiwyg']) && |
|
241 |
_media_ckeditor_fully_rendered_files_in_wysiwyg()) { |
|
242 |
$vars['page'] = TRUE; |
|
243 |
} |
|
244 |
} |
Formats disponibles : Unified diff
Weekly update of contrib modules