Projet

Général

Profil

Paste
Télécharger (4,34 ko) Statistiques
| Branche: | Révision:

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

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
  // Add fields from the file, so that we can override them if necessary.
87
  $form['options']['fields'] = array();
88
  foreach ($fields as $field_name => $field_value) {
89
    $file->{$field_name} = $field_value;
90
  }
91
  field_attach_form('file', $file, $form['options']['fields'], $form_state);
92
  $instance = field_info_instances('file', $file->type);
93
  foreach ($instance as $field_name => $field_value) {
94
    $info = field_info_field($field_name);
95
    // Only single valued fields can be overridden.
96
    $allow = $info['cardinality'] == 1 && !empty($instance[$field_name]['settings']['wysiwyg_override']);
97
    $form['options']['fields'][$field_name]['#access'] = $allow;
98
  }
99

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

    
107
  if (!element_children($form['options'])) {
108
    $form['options']['#attributes'] = array('style' => 'display:none');
109
  }
110

    
111
  return $form;
112
}