Projet

Général

Profil

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

root / drupal7 / sites / all / modules / flexslider / flexslider_fields / flexslider_fields.module @ 87dbc3bf

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
  return 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
}
30

    
31
/**
32
 * Implements hook_field_formatter_settings_form().
33
 *
34
 * Provides display settings form within the manage display page of
35
 * an image field with formatter flexslider.
36
 */
37
function flexslider_fields_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
38
  $display = $instance['display'][$view_mode];
39
  $settings = $display['settings'];
40

    
41
  $form = array();
42

    
43
  // Show select box for the option set
44
  $optionsets = array();
45
  ctools_include('export');
46
  foreach (flexslider_optionset_load_all() as $name => $optionset) {
47
    $optionsets[$name] = check_plain($optionset->title);
48
  }
49

    
50
  $form['optionset'] = array(
51
    '#title' => t('Option set'),
52
    '#type' => 'select',
53
    '#options' => $optionsets,
54
    '#default_value' => $settings['optionset'],
55
  );
56

    
57
  $image_styles = image_style_options(FALSE, PASS_THROUGH);
58
  $form['image_style'] = array(
59
      '#title' => t('Image style'),
60
      '#type' => 'select',
61
      '#default_value' => $settings['image_style'],
62
      '#empty_option' => t('None (original image)'),
63
      '#options' => $image_styles,
64
  );
65

    
66
  // If the image field doesn't have the Title field enabled, tell the user.
67
  if ($instance['settings']['title_field'] == FALSE) {
68
    $form['caption'] = array(
69
      '#title' => t('Use image title as the caption'),
70
      '#type' => 'checkbox',
71
      '#disabled' => TRUE,
72
      '#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'))))),
73
    );
74
  }
75
  else {
76
    $form['caption'] = array(
77
      '#title' => t('Use image title as the caption'),
78
      '#type' => 'checkbox',
79
      '#default_value' => $settings['caption'],
80
    );
81
  }
82

    
83
  return $form;
84
}
85

    
86
/**
87
 * Implements hook_field_formatter_settings_summary().
88
 *
89
 * Displays the summary of the set options of a flexslider formatted image field
90
 */
91
function flexslider_fields_field_formatter_settings_summary($field, $instance, $view_mode) {
92
  $display = $instance['display'][$view_mode];
93
  $settings = $display['settings'];
94

    
95
  $summary = array();
96

    
97
  // Load option set
98
  ctools_include('export');
99
  if (!empty($settings['optionset'])) {
100
    $o = flexslider_optionset_load($settings['optionset']);
101
    if ($o !== NULL) {
102
      $optionset = $o;
103
    }
104
  }
105

    
106
  // Display the selected image style
107
  if (!empty($settings['image_style'])) {
108
    $is = t('Image style: %imagestyle', array('%imagestyle' => $settings['image_style']));
109
  }
110
  else {
111
    $is = t('Image style: None (original image)');
112
  }
113

    
114
  // Build settings summary
115
  $optionset = isset($optionset) ? $optionset->title : t('Default settings');
116
  $summary[] = t('Option set: %optionset', array('%optionset' => $optionset));
117
  $summary[] = $is;
118

    
119
  return implode('<br />', $summary);
120
}
121

    
122
/**
123
 * Implements hook_field_formatter_view().
124
 *
125
 * Prepares a renderable array of images and adds the neccessary JS and CSS
126
 */
127
function flexslider_fields_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
128

    
129
  $element = array();
130
  if (count($items) > 0) {
131

    
132
    foreach ($items as $key => $item) {
133
      $tmp = $item;
134
      $item = array();
135
      $item['item'] = $tmp;
136

    
137
      // Setup the variables for calling theme_image_style
138
      $item['slide']['path'] = $item['item']['uri'];
139
      $item['slide']['style_name'] = $display['settings']['image_style'];
140
      $item['slide']['width'] = $item['item']['width'];
141
      $item['slide']['height'] = $item['item']['height'];
142
      $item['slide']['alt'] = $item['item']['alt'];
143
      $item['slide']['title'] = $item['item']['title'];
144

    
145
      // Render the slide item
146
      // If not style set, we have to call theme_image since theme_image_style
147
      // doesn't auto-fallback to full size image
148
      if (!empty($item['slide']['style_name'])) {
149
        // Generate the HTML for the slide
150
        $item['slide'] = theme('image_style', $item['slide']);
151
      }
152
      else {
153
        // Generate the HTML for the slide
154
        $item['slide'] = theme('image', $item['slide']);
155
      }
156

    
157
      // Check caption settings
158
      if ($display['settings']['caption']) {
159
        $item['caption'] = filter_xss($item['item']['title']);
160
      }
161

    
162
      $items[$key] = $item;
163
    }
164

    
165
    $element = array(
166
      '#theme' => 'flexslider',
167
      '#items' => $items,
168
      '#settings' => $display['settings'],
169
    );
170
  }
171

    
172
  return $element;
173
}