Projet

Général

Profil

Paste
Télécharger (9,27 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / media / modules / media_wysiwyg / includes / media_wysiwyg.pages.inc @ e4215af7

1
<?php
2

    
3
/**
4
 * @file
5
 * Common pages for the Media WYSIWYG module.
6
 */
7

    
8
/**
9
 * Form callback used when embedding media.
10
 *
11
 * Allows the user to pick a format for their media file.
12
 * Can also have additional params depending on the media type.
13
 */
14
function media_wysiwyg_format_form($form, &$form_state, $file) {
15
  $form_state['file'] = $file;
16

    
17
  // Allow for overrides to the fields.
18
  $query_fields = isset($_GET['fields']) ? drupal_json_decode($_GET['fields']) : array();
19
  $fields = media_wysiwyg_filter_field_parser(array('fields' => $query_fields), $file);
20

    
21
  $options = media_wysiwyg_get_file_view_mode_options($file);
22

    
23
  if (!count($options)) {
24
    throw new Exception('Unable to continue, no available formats for displaying media.');
25
  }
26

    
27
  // Generate all the previews.
28
  if (!isset($form_state['storage']['view_mode_previews'])) {
29
    $form_state['storage']['view_mode_previews'] = array();
30
    foreach ($options as $view_mode => $view_mode_label) {
31
      $view_mode_preview = media_wysiwyg_get_file_without_label($file, $view_mode, array('wysiwyg' => TRUE));
32
      $form_state['storage']['view_mode_previews'][$view_mode] = drupal_render($view_mode_preview);
33
    }
34
  }
35

    
36
  // Add the previews back into the form array so they can be altered.
37
  $form['#formats'] = &$form_state['storage']['view_mode_previews'];
38

    
39
  // Allow for overrides to the display format.
40
  $default_view_mode = is_array($query_fields) && isset($query_fields['format']) ? $query_fields['format'] : variable_get('media_wysiwyg_wysiwyg_default_view_mode', 'full');
41
  if (!isset($options[$default_view_mode])) {
42
    reset($options);
43
    $default_view_mode = key($options);
44
  }
45

    
46
  // Add the previews by reference so that they can easily be altered by
47
  // changing $form['#formats'].
48
  $settings['media']['formatFormFormats'] = &$form_state['storage']['view_mode_previews'];
49
  $form['#attached']['js'][] = array('data' => $settings, 'type' => 'setting');
50

    
51
  // Add the required libraries, JavaScript and CSS for the form.
52
  $form['#attached']['library'][] = array('media', 'media_base');
53
  $form['#attached']['library'][] = array('system', 'form');
54
  $form['#attached']['css'][] = drupal_get_path('module', 'media_wysiwyg') . '/css/media_wysiwyg.css';
55
  $form['#attached']['js'][] = drupal_get_path('module', 'media_wysiwyg') . '/js/media_wysiwyg.format_form.js';
56

    
57
  $form['title'] = array(
58
    '#markup' => t('Embedding %filename', array('%filename' => $file->filename)),
59
  );
60

    
61
  $preview = media_get_thumbnail_preview($file);
62

    
63
  $form['preview'] = array(
64
    '#type' => 'markup',
65
    '#markup' => drupal_render($preview),
66
  );
67

    
68
  // These will get passed on to WYSIWYG.
69
  $form['options'] = array(
70
    '#type' => 'fieldset',
71
    '#title' => t('options'),
72
  );
73

    
74
  // @TODO: Display more verbose information about which formatter and what it
75
  // does.
76
  $form['options']['format'] = array(
77
    '#type' => 'select',
78
    '#title' => t('Display as'),
79
    '#options' => $options,
80
    '#default_value' => $default_view_mode,
81
    '#description' => t('Choose the type of display you would like for this
82
      file. Please be aware that files may display differently than they do when
83
      they are inserted into an editor.'),
84
  );
85

    
86
  // If necessary, display the alignment widget.
87
  if (variable_get('media_wysiwyg_alignment', FALSE)) {
88
    $align_default = empty($query_fields['alignment']) ? '' : $query_fields['alignment'];
89
    $align_options = array(
90
      '' => t('None'),
91
      'left' => t('Left'),
92
      'right' => t('Right'),
93
      'center' => t('Center'),
94
    );
95
    if (!isset($align_options[$align_default])) {
96
      // Safety code for a malformed token.
97
      $align_default = '';
98
    }
99
    $form['options']['alignment'] = array(
100
      '#type' => 'select',
101
      '#title' => t('Alignment'),
102
      '#options' => $align_options,
103
      '#description' => t('Choose how you would like the media to be aligned with surrounding content.'),
104
      '#default_value' => $align_default,
105
    );
106
  }
107
  // Add fields from the file, so that we can override them if necessary.
108
  $form['options']['fields'] = array();
109
  foreach ($fields as $field_name => $field_value) {
110
    $file->{$field_name} = $field_value;
111
  }
112
  // Get the external url from the fid array.
113
  $external_url = empty($query_fields['external_url']) ? NULL : $query_fields['external_url'];
114
  // Field to attach external url's to files for linking.
115
  if (variable_get('media_wysiwyg_external_link', FALSE)) {
116
    if ($file->type == 'image') {
117
      $form['options']['external_url'] = array(
118
        '#type' => 'textfield',
119
        '#title' => t('Link Image'),
120
        '#description' => t('Enter a URL to turn the image into a link.'),
121
        '#maxlength' => 2048,
122
        '#default_value' => $external_url,
123
      );
124
    }
125
  }
126
  field_attach_form('file', $file, $form['options']['fields'], $form_state);
127
  $instance = field_info_instances('file', $file->type);
128
  foreach ($instance as $field_name => $field_value) {
129
    $info = field_info_field($field_name);
130
    $allow = !empty($instance[$field_name]['settings']['wysiwyg_override']);
131
    // Only single valued fields can be overridden normally, unless Media is
132
    // configured otherwise with "media_wysiwyg_wysiwyg_override_multivalue".
133
    if ($allow && $info['cardinality'] != 1) {
134
      $allow = variable_get('media_wysiwyg_wysiwyg_override_multivalue', FALSE);
135
    }
136
    $form['options']['fields'][$field_name]['#access'] = $allow;
137
  }
138

    
139
  // Add view mode preview.
140
  media_wysiwyg_format_form_view_mode($form, $form_state, $file);
141

    
142
  // Similar to a form_alter, but we want this to run first so that
143
  // media.types.inc can add the fields specific to a given type (like alt tags
144
  // on media). If implemented as an alter, this might not happen, making other
145
  // alters not be able to work on those fields.
146
  // @todo: We need to pass in existing values for those attributes.
147
  drupal_alter('media_wysiwyg_format_form_prepare', $form, $form_state, $file);
148

    
149
  if (!element_children($form['options'])) {
150
    $form['options']['#attributes'] = array('style' => 'display:none');
151
  }
152

    
153
  return $form;
154
}
155

    
156
/**
157
 * Add ajax preview when selecting view mode in wysiwyg editor.
158
 */
159
function media_wysiwyg_format_form_view_mode(&$form, $form_state, $file) {
160
  // Check to see if a view mode ("format") has already been specified for
161
  // this media item. First, check for a standard form-submitted value.
162
  if (!empty($form_state['values']['format'])) {
163
    $view_mode = $form_state['values']['format'];
164
  }
165
  // Second, check the request for a JSON-encoded value.
166
  elseif (isset($_GET['fields'])) {
167
    $query_fields = drupal_json_decode($_GET['fields']);
168
    if (isset($query_fields['format'])) {
169
      $view_mode = $query_fields['format'];
170
    }
171
  }
172
  // If we were unable to determine a view mode, or we found a view mode
173
  // that does not exist in the list of format options presented on this
174
  // form, use the default view mode.
175
  if (!isset($view_mode) || !array_key_exists($view_mode, $form['options']['format']['#options'])) {
176
    $view_mode = variable_get('media_wysiwyg_wysiwyg_default_view_mode', 'full');
177
  }
178

    
179
  $link_options = array(
180
    'attributes' => array(
181
      'class' => 'button',
182
      'title' => t('Use for replace fox or edit file fields.'),
183
    ),
184
    'query' => drupal_get_destination(),
185
  );
186
  if (!empty($_GET['render'])) {
187
    $link_options['query']['render'] = $_GET['render'];
188
  }
189

    
190
  $form['preview'] = array();
191
  $form['preview']['#prefix'] = '<div class="media-preview-group"><div class="media-item"><div class="media-thumbnail">';
192
  $form['preview']['#suffix'] = '</div><div class="label-wrapper"><label class="media-filename">' . check_plain($file->filename) . '</label></div></div><div class="edit-file-link">' . l(t('Edit file'), 'file/' . $file->fid . '/edit', $link_options) . '</div></div>';
193
  $form['preview']['thumbnail'] = file_view_file($file, $view_mode);
194
  $form['preview']['thumbnail']['#prefix'] = '<div id="media-preview">';
195
  $form['preview']['thumbnail']['#suffix'] = '</div>';
196

    
197
  if (!isset($form['options']['format']['#default_value'])) {
198
    $form['options']['format']['#default_value'] = $view_mode;
199
  }
200
  $form['options']['format']['#ajax'] = array(
201
    'callback' => 'media_wysiwyg_format_form_preview',
202
    'wrapper' => 'media-preview',
203
  );
204

    
205
  $wysiwyg_view_mode = db_query('SELECT view_mode FROM {media_view_mode_wysiwyg} WHERE type = :type', array(':type' => $file->type))->fetchField();
206
  $view_modes = media_wysiwyg_get_wysiwyg_allowed_view_modes($file);
207
  $formats = $options = array();
208

    
209
  foreach ($view_modes as $view_mode => $view_mode_info) {
210
    $options[$view_mode] = $view_mode_info['label'];
211

    
212
    if (!empty($wysiwyg_view_mode)) {
213
      $element = media_wysiwyg_get_file_without_label($file, $wysiwyg_view_mode, array('wysiwyg' => TRUE));
214
    }
215
    else {
216
      $element = media_wysiwyg_get_file_without_label($file, $view_mode, array('wysiwyg' => TRUE));
217
    }
218

    
219
    $formats[$view_mode] = drupal_render($element);
220
  }
221

    
222
  $form['#formats'] = $formats;
223
  $form['options']['format']['#options'] = $options;
224
}
225

    
226
/**
227
 * AJAX callback to select portion of format form to be updated with a preview.
228
 *
229
 * @param array $form
230
 *   An associative array containing the structure of the form.
231
 * @param array $form_state
232
 *   An associative array containing the current state of the form.
233
 *
234
 * @return array
235
 *   The preview form item.
236
 */
237
function media_wysiwyg_format_form_preview($form, $form_state) {
238
  return $form['preview']['thumbnail'];
239
}