Projet

Général

Profil

Paste
Télécharger (7,73 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / flexslider / flexslider_fields / flexslider_fields.module @ 05237dd8

1
<?php
2

    
3
/**
4
 * @file
5
 * Adds fields integration with FlexSlider
6
 *
7
 * @author jepedo
8
 * @author Mathew Winstone <mwinstone@coldfrontlabs.ca>
9
 */
10

    
11
/**
12
 * Implements hook_field_formatter_info().
13
 *
14
 * Adds the flexslider format option within the manage display form of
15
 * of an image field.
16
 */
17
function flexslider_fields_field_formatter_info() {
18
  $info = array(
19
    'flexslider' => array(
20
      'label' => t('flexslider'),
21
      'field types' => array('image', 'media'),
22
      'settings' => array(
23
        'optionset' => 'default',
24
        'image_style' => '',
25
        'caption' => FALSE,
26
      ),
27
    ),
28
  );
29
  // Integrate with file entity / media.
30
  if (module_exists('file_entity')) {
31
    $info['flexslider_file_entity'] = array(
32
      'label' => t('FlexSlider File Entity'),
33
      'description' => t('Render files in a specific view mode.'),
34
      'field types' => array('image', 'media', 'file'),
35
      'settings' => array(
36
        'optionset' => 'default',
37
        'file_view_mode' => 'default',
38
        'caption' => FALSE,
39
      ),
40
    );
41
  }
42
  return $info;
43
}
44

    
45
/**
46
 * Implements hook_field_formatter_settings_form().
47
 *
48
 * Provides display settings form within the manage display page of
49
 * an image field with formatter flexslider.
50
 */
51
function flexslider_fields_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
52
  $display = $instance['display'][$view_mode];
53
  $settings = $display['settings'];
54

    
55
  $form = array();
56

    
57
  // Show select box for the option set
58
  $optionsets = array();
59
  ctools_include('export');
60
  foreach (flexslider_optionset_load_all() as $name => $optionset) {
61
    $optionsets[$name] = check_plain($optionset->title);
62
  }
63

    
64
  $form['optionset'] = array(
65
    '#title' => t('Option set'),
66
    '#type' => 'select',
67
    '#options' => $optionsets,
68
    '#default_value' => $settings['optionset'],
69
  );
70

    
71
  if ($display['type'] == 'flexslider') {
72
    $image_styles = image_style_options(FALSE, PASS_THROUGH);
73
    $form['image_style'] = array(
74
      '#title' => t('Image style'),
75
      '#type' => 'select',
76
      '#default_value' => $settings['image_style'],
77
      '#empty_option' => t('None (original image)'),
78
      '#options' => $image_styles,
79
    );
80
  }
81
  elseif ($display['type'] == 'flexslider_file_entity') {
82
    $form['file_view_mode'] = array(
83
      '#title' => t('File view mode'),
84
      '#type' => 'select',
85
      '#default_value' => $settings['file_view_mode'],
86
      '#options' => file_entity_view_mode_labels(),
87
    );
88
  }
89

    
90
  if (!empty($instance['settings'])) {
91
    // If the image field doesn't have the Title field enabled, tell the user.
92
    if ($instance['settings']['title_field'] == FALSE and $instance['bundle'] != 'ctools') {
93
      $form['caption'] = array(
94
        '#title' => t('Choose a caption source'),
95
        '#type' => 'select',
96
        '#disabled' => TRUE,
97
        '#options' => array(
98
          0 => t('None'),
99
          1 => t('Image title'),
100
          'alt' => t('Image ALT attribute'),
101
        ),
102
        '#description' => t('You need to <a href="@url">enable the Title field</a> for this image field to be able use it as a caption.', array('@url' => url('admin/structure/types/manage/' . $instance['bundle'] . '/fields/' . $instance['field_name'], array('fragment' => 'edit-instance-settings-title-field', 'query' => array('destination' => 'admin/structure/types/manage/' . $instance['bundle'] . '/display'))))),
103
      );
104
    }
105
    else {
106
      $form['caption'] = array(
107
        '#title' => t('Choose a caption source'),
108
        '#type' => 'select',
109
        '#options' => array(
110
          0 => t('None'),
111
          1 => t('Image title'),
112
          'alt' => t('Image ALT attribute'),
113
        ),
114
        '#default_value' => $settings['caption'],
115
      );
116
    }
117
  }
118

    
119
  return $form;
120
}
121

    
122
/**
123
 * Implements hook_field_formatter_settings_summary().
124
 *
125
 * Displays the summary of the set options of a flexslider formatted image field
126
 */
127
function flexslider_fields_field_formatter_settings_summary($field, $instance, $view_mode) {
128
  $display = $instance['display'][$view_mode];
129
  $settings = $display['settings'];
130

    
131
  $summary = array();
132

    
133
  // Load option set.
134
  ctools_include('export');
135
  if (!empty($settings['optionset'])) {
136
    $o = flexslider_optionset_load($settings['optionset']);
137
    if ($o !== NULL) {
138
      $optionset = $o;
139
    }
140
  }
141

    
142
  // Display the selected image style.
143
  if ($instance['display'][$view_mode]['type'] == 'flexslider') {
144
    if (!empty($settings['image_style'])) {
145
      $is = t('Image style: %imagestyle', array('%imagestyle' => $settings['image_style']));
146
    }
147
    else {
148
      $is = t('Image style: None (original image)');
149
    }
150
  }
151
  elseif ($instance['display'][$view_mode]['type'] == 'flexslider_file_entity') {
152
    if (!empty($settings['file_view_mode'])) {
153
      $is = t('File view mode: %file_view_mode', array(
154
        '%file_view_mode' => file_entity_view_mode_label($settings['file_view_mode']),
155
      ));
156
    }
157
    else {
158
      $is = t('File view mode: Default');
159
    }
160
  }
161

    
162
  // Build settings summary.
163
  $optionset = isset($optionset) ? $optionset->title : t('Default settings');
164
  $summary[] = t('Option set: %optionset', array('%optionset' => $optionset));
165
  $summary[] = $is;
166

    
167
  return implode('<br />', $summary);
168
}
169

    
170
/**
171
 * Implements hook_field_formatter_view().
172
 *
173
 * Prepares a renderable array of images and adds the necessary JS and CSS.
174
 */
175
function flexslider_fields_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
176

    
177
  // If file entity formatter is selected render all files initially.
178
  if ($display['type'] == 'flexslider_file_entity') {
179
    $file_display = $display;
180
    $file_display['type'] = 'file_rendered';
181
    $file_items = module_invoke('file_entity', 'field_formatter_view', $entity_type, $entity, $field, $instance, $langcode, $items, $file_display);
182
  }
183

    
184
  $element = array();
185
  if (count($items) > 0) {
186

    
187
    foreach ($items as $key => $item) {
188
      if(isset($item['item'])) continue;
189
      $tmp = $item;
190
      $item = array();
191
      $item['item'] = $tmp;
192

    
193
      // Setup the variables for calling theme_image_style
194
      if (isset($item['item']['uri'])) {
195
        $item['slide']['path'] = $item['item']['uri'];
196
      }
197
      if (isset($display['settings']['image_style'])) {
198
        $item['slide']['style_name'] = $display['settings']['image_style'];
199
      }
200
      if (isset($item['item']['width'])) {
201
        $item['slide']['width'] = $item['item']['width'];
202
      }
203
      if (isset($item['item']['height'])) {
204
        $item['slide']['height'] = $item['item']['height'];
205
      }
206
      if (isset($item['item']['alt'])) {
207
        $item['slide']['alt'] = $item['item']['alt'];
208
      }
209
      if (isset($item['item']['title'])) {
210
        $item['slide']['title'] = $item['item']['title'];
211
      }
212

    
213
      // Render the slide item.
214
      if ($display['type'] == 'flexslider') {
215
        // If no style set, we have to call theme_image since theme_image_style
216
        // doesn't auto-fallback to full size image.
217
        if (!empty($item['slide']['style_name'])) {
218
          // Generate the HTML for the slide.
219
          $item['slide'] = theme('image_style', $item['slide']);
220
        }
221
        else {
222
          // Generate the HTML for the slide.
223
          $item['slide'] = theme('image', $item['slide']);
224
        }
225
      }
226
      elseif ($display['type'] == 'flexslider_file_entity') {
227
        $item['slide'] = render($file_items[$key]);
228
      }
229

    
230
      // Check caption settings.
231
      if ($display['settings']['caption'] === '1') {
232
        $item['caption'] = filter_xss($item['item']['title']);
233
      }
234
      elseif ($display['settings']['caption'] === 'alt') {
235
        $item['caption'] = filter_xss($item['item']['alt']);
236
      }
237

    
238
      $items[$key] = $item;
239
    }
240

    
241
    $element[0] = array(
242
      '#theme' => 'flexslider',
243
      '#items' => $items,
244
      '#settings' => $display['settings'],
245
    );
246
  }
247

    
248
  return $element;
249
}