Project

General

Profile

Paste
Download (5.41 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / media / modules / media_wysiwyg_view_mode / media_wysiwyg_view_mode.module @ 1f623f01

1
<?php
2

    
3
/**
4
 * @file
5
 * Primarily Drupal hooks.
6
 */
7

    
8
/**
9
 * Implements hook_entity_info_alter().
10
 */
11
function media_wysiwyg_view_mode_entity_info_alter(&$entity_info) {
12
  $entity_info['file']['view modes'] += array(
13
    'wysiwyg' => array(
14
      'label' => t('WYSIWYG'),
15
      'custom settings' => TRUE,
16
    ),
17
  );
18
}
19

    
20
/**
21
 * Implements hook_media_wysiwyg_allowed_view_modes_alter().
22
 */
23
function media_wysiwyg_view_mode_media_wysiwyg_allowed_view_modes_alter(&$view_modes, &$file) {
24
  if (variable_get("media_wysiwyg_view_mode_{$file->type}_wysiwyg_restricted_view_modes_status")) {
25
    $restricted_view_modes = variable_get("media_wysiwyg_view_mode_{$file->type}_wysiwyg_restricted_view_modes", array());
26
    foreach ($restricted_view_modes as $restricted_view_mode) {
27
      if (array_key_exists($restricted_view_mode, $view_modes)) {
28
        unset($view_modes[$restricted_view_mode]);
29
      }
30
    }
31
  }
32
}
33

    
34
/**
35
 * Implements hook_entity_view_mode_alter().
36
 */
37
function media_wysiwyg_view_mode_entity_view_mode_alter(&$view_mode, $context) {
38
  if ($context['entity_type'] == 'file' && !empty($context['entity']->override['wysiwyg'])) {
39
    if (variable_get("media_wysiwyg_view_mode_{$context['entity']->type}_file_wysiwyg_view_mode_status")) {
40
      $view_mode = variable_get("media_wysiwyg_view_mode_{$context['entity']->type}_file_wysiwyg_view_mode", 'wysiwyg');
41
    }
42
  }
43
}
44

    
45
/**
46
 * Implements hook_form_FORM_ID_alter().
47
 */
48
function media_wysiwyg_view_mode_form_media_wysiwyg_format_form_alter(&$form, $form_state) {
49
  // Find the current selected view mode.
50
  if (!empty($form_state['values']['format'])) {
51
    $view_mode = $form_state['values']['format'];
52
  }
53
  else {
54
    $view_mode = $form['options']['format']['#default_value'];
55
  }
56

    
57
  // Display an actual preview instead of media_get_thumbnail_preview().
58
  // @todo Should this code be merged up to media_wysiwyg_format_form?
59
  $form['preview']['#prefix'] = '<div id="preview">';
60
  $form['preview']['#suffix'] = '</div>';
61
  $form['preview']['#markup'] = $form_state['storage']['view_mode_previews'][$view_mode];
62

    
63
  // Change the preview when the user changes the format.
64
  $form['options']['format']['#ajax'] = array(
65
    'callback' => 'media_wysiwyg_view_mode_form_media_wysiwyg_format_form_preview',
66
    'wrapper' => 'preview',
67
  );
68
}
69

    
70
/**
71
 * AJAX callback to select the portion of the format form to be updated with a preview.
72
 *
73
 * @param array $form
74
 *   An associative array containing the structure of the form.
75
 * @param array $form_state
76
 *   An associative array containing the current state of the form.
77
 *
78
 * @return array
79
 *   The preview form item.
80
 */
81
function media_wysiwyg_view_mode_form_media_wysiwyg_format_form_preview($form, $form_state) {
82
  return $form['preview'];
83
}
84

    
85
/**
86
 * Implements hook_form_FORM_ID_alter().
87
 */
88
function media_wysiwyg_view_mode_form_file_entity_file_type_form_alter(&$form, $form_state) {
89
  $type = $form_state['build_info']['args'][0];
90
  $view_mode_options = media_wysiwyg_get_file_type_view_mode_options($type->type);
91

    
92
  $form['media_wysiwyg_view_mode'] = array(
93
    '#type' => 'fieldset',
94
    '#title' => t('Media WYSIWYG View Modes'),
95
    '#collapsible' => TRUE,
96
    '#collapsed' => TRUE,
97
    '#tree' => TRUE,
98
  );
99

    
100
  $form['media_wysiwyg_view_mode']["media_wysiwyg_view_mode_{$type->type}_wysiwyg_restricted_view_modes_status"] = array(
101
    '#type' => 'checkbox',
102
    '#title' => t('Restrict allowed view modes when embedding with the WYSIWYG', array('%type' => $type->label)),
103
    '#default_value' => variable_get("media_wysiwyg_view_mode_{$type->type}_wysiwyg_restricted_view_modes_status", FALSE),
104
  );
105
  $form['media_wysiwyg_view_mode']["media_wysiwyg_view_mode_{$type->type}_wysiwyg_restricted_view_modes"] = array(
106
    '#type' => 'checkboxes',
107
    '#title' => t('Select which view modes <em>cannot</em> be selected'),
108
    '#options' => $view_mode_options,
109
    '#default_value' => variable_get("media_wysiwyg_view_mode_{$type->type}_wysiwyg_restricted_view_modes", array()),
110
    '#states' => array(
111
      'visible' => array(
112
        ':input[name="media_wysiwyg_view_mode[media_wysiwyg_view_mode_' . $type->type . '_wysiwyg_restricted_view_modes_status]"]' => array('checked' => TRUE),
113
      ),
114
    ),
115
  );
116
  $form['media_wysiwyg_view_mode']["media_wysiwyg_view_mode_{$type->type}_file_wysiwyg_view_mode_status"] = array(
117
    '#type' => 'checkbox',
118
    '#title' => t('Use a specific view mode when the file is previewed in the WYSIWYG'),
119
    '#default_value' => variable_get("media_wysiwyg_view_mode_{$type->type}_file_wysiwyg_view_mode_status", 0),
120
  );
121
  $form['media_wysiwyg_view_mode']["media_wysiwyg_view_mode_{$type->type}_file_wysiwyg_view_mode"] = array(
122
    '#type' => 'select',
123
    '#title' => t('View mode to always use in the WYSIWYG'),
124
    '#options' => $view_mode_options,
125
    '#default_value' => variable_get("media_wysiwyg_view_mode_{$type->type}_file_wysiwyg_view_mode", 'wysiwyg'),
126
    '#states' => array(
127
      'visible' => array(
128
        ':input[name="media_wysiwyg_view_mode[media_wysiwyg_view_mode_' . $type->type . '_file_wysiwyg_view_mode_status]"]' => array('checked' => TRUE),
129
      ),
130
    ),
131
  );
132

    
133
  $form['#submit'][] = 'media_wysiwyg_view_mode_form_file_entity_file_type_form_submit';
134
}
135

    
136
function media_wysiwyg_view_mode_form_file_entity_file_type_form_submit($form, $form_state) {
137
  $values = $form_state['values']['media_wysiwyg_view_mode'];
138
  foreach ($values as $key => $value) {
139
    if (is_array($value)) {
140
      $value = array_filter($value);
141
    }
142
    variable_set($key, $value);
143
  }
144
}