Projet

Général

Profil

Paste
Télécharger (5,62 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / media / modules / media_wysiwyg_view_mode / media_wysiwyg_view_mode.module @ ca0757b9

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5
 * Primarily Drupal hooks.
6
 */
7
8
/**
9
 * Implements hook_permission().
10
 */
11
function media_wysiwyg_view_mode_permission() {
12
  return array(
13
    'administer media wysiwyg view mode' => array(
14
      'title' => t('Administer Media WYSIWYG View Mode'),
15
    ),
16
  );
17
}
18
19
/**
20
 * Implements hook_menu().
21
 */
22
function media_wysiwyg_view_mode_menu() {
23
  $items['admin/config/media/wysiwyg-view-mode'] = array(
24
    'title' => 'Media WYSIWYG View Mode',
25
    'description' => 'Configure view mode settings for files embedded into and displayed inside of the WYSIWYG editor.',
26
    'page callback' => 'drupal_get_form',
27
    'page arguments' => array('media_wysiwyg_view_mode_configuration_form'),
28
    'access arguments' => array('administer media wysiwyg view mode'),
29
    'file' => 'media_wysiwyg_view_mode.admin.inc',
30
  );
31
32
  return $items;
33
}
34
35
/**
36
 * Implements hook_help().
37
 */
38
function media_wysiwyg_view_mode_help($path, $arg) {
39
  switch ($path) {
40
    case 'admin/config/media/wysiwyg-view-mode':
41
      $output = '';
42
      $output .= '<p>' . t('Configure view modes for files displayed inside of the WYSIWYG editor.') . '</p>';
43
      $output .= '<p>' . t('View modes can be configured per file type. Only enabled view modes are selectable.') . '</p>';
44
      return $output;
45
  }
46
}
47
48
/**
49
 * Implements hook_entity_info_alter().
50
 */
51
function media_wysiwyg_view_mode_entity_info_alter(&$entity_info) {
52
  $entity_info['file']['view modes'] += array(
53
    'wysiwyg' => array(
54
      'label' => t('WYSIWYG'),
55
      'custom settings' => TRUE,
56
    ),
57
  );
58
}
59
60
/**
61 ca0757b9 Assos Assos
 * Implements hook_media_wysiwyg_wysiwyg_allowed_view_modes_alter().
62 85ad3d82 Assos Assos
 */
63 ca0757b9 Assos Assos
function media_wysiwyg_view_mode_media_wysiwyg_wysiwyg_allowed_view_modes_alter(&$view_modes, &$file) {
64 85ad3d82 Assos Assos
  if (variable_get("media_wysiwyg_view_mode_{$file->type}_wysiwyg_restricted_view_modes_status", FALSE) == TRUE) {
65
    $restricted_view_modes = variable_get("media_wysiwyg_view_mode_{$file->type}_wysiwyg_restricted_view_modes", array());
66
67
    foreach ($restricted_view_modes as $restricted_view_mode) {
68
      if (array_key_exists($restricted_view_mode, $view_modes)) {
69
        unset($view_modes[$restricted_view_mode]);
70
      }
71
    }
72
  }
73
}
74
75
/**
76 ca0757b9 Assos Assos
 * Implements hook_media_wysiwyg_token_to_markup_alter().
77 85ad3d82 Assos Assos
 */
78 ca0757b9 Assos Assos
function media_wysiwyg_view_mode_media_wysiwyg_token_to_markup_alter(&$element, $tag_info, $settings) {
79 85ad3d82 Assos Assos
  if (!empty($settings['wysiwyg'])) {
80
    $file = $tag_info['file'];
81
82
    if (variable_get("media_wysiwyg_view_mode_{$file->type}_file_wysiwyg_view_mode_status", FALSE) == TRUE) {
83 ca0757b9 Assos Assos
      $element = media_wysiwyg_get_file_without_label($file, variable_get("media_wysiwyg_view_mode_{$file->type}_file_wysiwyg_view_mode", 'wysiwyg'), $settings);
84 85ad3d82 Assos Assos
    }
85
    else {
86 ca0757b9 Assos Assos
      $element = media_wysiwyg_get_file_without_label($file, $tag_info['view_mode'], $settings);
87 85ad3d82 Assos Assos
    }
88
  }
89
}
90
91
/**
92
 * Implements hook_form_alter().
93
 */
94
function media_wysiwyg_view_mode_form_alter(&$form, $form_state, $form_id)  {
95
  switch ($form_id)  {
96 ca0757b9 Assos Assos
    case 'media_wysiwyg_format_form':
97
      $file = $form_state['file'];
98 85ad3d82 Assos Assos
99 ca0757b9 Assos Assos
      // Check to see if a view mode ("format") has already been specified for
100
      //  this media item. First, check for a standard form-submitted value.
101
      if (!empty($form_state['values']['format'])) {
102
        $view_mode = $form_state['values']['format'];
103
      }
104
      // Second, check the request for a JSON-encoded value.
105
      elseif (isset($_GET['fields'])) {
106
        $query_fields = drupal_json_decode($_GET['fields']);
107
        if (isset($query_fields['format'])) {
108
          $view_mode = $query_fields['format'];
109
        }
110
      }
111
      // If we were unable to determine a view mode, or we found a view mode
112
      //  that does not exist in the list of format options presented on this
113
      //  form, use the default view mode.
114
      if (!isset($view_mode) || !array_key_exists($view_mode, $form['options']['format']['#options'])) {
115
        $view_mode = variable_get('media_wysiwyg_wysiwyg_default_view_mode', 'full');
116
      }
117 85ad3d82 Assos Assos
118
      $form['preview'] = file_view_file($file, $view_mode);
119
      $form['preview']['#prefix'] = '<div id="preview">';
120
      $form['preview']['#suffix'] = '</div>';
121
122
      if (!isset($form['options']['format']['#default_value'])) {
123
        $form['options']['format']['#default_value'] = $view_mode;
124
      }
125
      $form['options']['format']['#ajax'] = array(
126
        'callback' => 'media_format_form_preview',
127
        'wrapper' => 'preview',
128
      );
129
130 ca0757b9 Assos Assos
      $view_modes = media_wysiwyg_get_wysiwyg_allowed_view_modes($file);
131 85ad3d82 Assos Assos
      $formats = $options = array();
132
      foreach ($view_modes as $view_mode => $view_mode_info) {
133
        //@TODO: Display more verbose information about which formatter and what it does.
134
        $options[$view_mode] = $view_mode_info['label'];
135
136
        if (variable_get("media_wysiwyg_view_mode_{$file->type}_file_wysiwyg_view_mode_status", FALSE) == TRUE) {
137 ca0757b9 Assos Assos
          $element = media_wysiwyg_get_file_without_label($file, variable_get("media_wysiwyg_view_mode_{$file->type}_file_wysiwyg_view_mode", 'wysiwyg'), array('wysiwyg' => TRUE));
138 85ad3d82 Assos Assos
        }
139
        else {
140 ca0757b9 Assos Assos
          $element = media_wysiwyg_get_file_without_label($file, $view_mode, array('wysiwyg' => TRUE));
141 85ad3d82 Assos Assos
        }
142
143
        // Make a pretty name out of this.
144
        $formats[$view_mode] = drupal_render($element);
145
      }
146
147
      $form['#formats'] = $formats;
148
      break;
149
  }
150
}
151
152
/**
153
 * AJAX callback to select the portion of the format form to be updated with a preview.
154
 *
155
 * @param array $form
156
 *   An associative array containing the structure of the form.
157
 * @param array $form_state
158
 *   An associative array containing the current state of the form.
159
 *
160
 * @return array
161
 *   The preview form item.
162
 */
163
function media_format_form_preview($form, $form_state) {
164
  return $form['preview'];
165
}