summaryrefslogtreecommitdiffstats
path: root/flexslider_entityreference.module
blob: a6b9d70fcba9147fb6b5f1030ce06ba17b43d05f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
 * Implements hook_field_formatter_info().
 */
function flexslider_entityreference_field_formatter_info() {
  return array(
    'entityreference_entity_flexslider' => array(
      'label' => t('FlexSlider'),
      'description' => t('Displays the rendered entities in a FlexSlider.'),
      'field types' => array('entityreference'),
      'settings' => array(
        'view_mode' => '',
        'view_mode_thumb' => '',
        'optionset' => '',
      ),
    ),
  );
}

/**
 * Implements hook_field_formatter_settings_form().
 */
function flexslider_entityreference_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];

  $form = array();

  // Show select box for the option set
  $optionsets = array();
  ctools_include('export');
  foreach (flexslider_optionset_load_all() as $name => $optionset) {
    $optionsets[$name] = check_plain($optionset->title);
  }
  $form['optionset'] = array(
    '#title' => t('Option set'),
    '#type' => 'select',
    '#options' => $optionsets,
    '#default_value' => $settings['optionset'],
  );

  $entity_info = entity_get_info($field['settings']['target_type']);
  $options = array();
  if (!empty($entity_info['view modes'])) {
    foreach ($entity_info['view modes'] as $view_mode => $view_mode_settings) {
      $options[$view_mode] = $view_mode_settings['label'];
    }
  }
  if (count($options) > 1) {
    $form['view_mode'] = array(
      '#type' => 'select',
      '#options' => $options,
      '#title' => t('Slide view mode'),
      '#default_value' => $settings['view_mode'],
    );
    $form['view_mode_thumb'] = array(
      '#type' => 'select',
      '#options' => array('_empty' => '(' . t('Empty string') . ')') + $options,
      '#title' => t('Thumbnail view mode'),
      '#default_value' => $settings['view_mode_thumb'],
    );
  }
  return $form;
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
function flexslider_entityreference_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];

  $summary = array();

  // Load option set
  ctools_include('export');
  if (!empty($settings['optionset'])) {
    $o = flexslider_optionset_load($settings['optionset']);
    if ($o !== NULL) {
      $optionset = $o;
    }
  }

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

  $entity_info = entity_get_info($field['settings']['target_type']);
  $summary[] = t('Slides rendered as @mode', array('@mode' => isset($entity_info['view modes'][$settings['view_mode']]['label']) ? $entity_info['view modes'][$settings['view_mode']]['label'] : $settings['view_mode']));
  if ($settings['view_mode_thumb'] == '_empty') {
    $summary[] = t('Thumbnails rendered as @mode', array('@mode' => t('empty strings')));
  } else {
    $summary[] = t('Thumbnails rendered as @mode', array('@mode' => isset($entity_info['view modes'][$settings['view_mode_thumb']]['label']) ? $entity_info['view modes'][$settings['view_mode_thumb']]['label'] : $settings['view_mode_thumb']));
  }

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

/**
 * Implements hook_field_formatter_prepare_view().
 */
function flexslider_entityreference_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
  entityreference_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, $items, $displays);
}

/**
 * Implements hook_field_formatter_view().
 */
function flexslider_entityreference_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $result = array();
  $settings = $display['settings'];

  // Rebuild the items list to contain only those with access.
  foreach ($items as $key => $item) {
    if (empty($item['access'])) {
      unset($items[$key]);
    }
  }
  switch ($display['type']) {
    case 'entityreference_entity_flexslider':
      foreach ($items as $delta => $item) {
        // Protect ourselves from recursive rendering.
        static $depth = 0;
        $depth++;
        if ($depth > 20) {
          throw new EntityReferenceRecursiveRenderingException(t('Recursive rendering detected when rendering entity @entity_type(@entity_id). Aborting rendering.', array('@entity_type' => $entity_type, '@entity_id' => $item['target_id'])));
        }

        $entity = clone $item['entity'];
        unset($entity->content);
        $entity_build = entity_view($field['settings']['target_type'], array($item['target_id'] => $entity), $settings['view_mode'], $langcode, FALSE);
        $result[$delta] = array(
          'slide' => drupal_render($entity_build),
          'thumb' => '',
        );
        if ($settings['view_mode_thumb'] !== '_empty') {
          $result[$delta]['thumb'] = drupal_render(entity_view($field['settings']['target_type'], array($item['target_id'] => $entity), $settings['view_mode_thumb'], $langcode, FALSE));
        }
        $depth = 0;
      }
      break;
  }
  $element = array();
  if (count($result) > 0) {
    $element[] = array(
      '#theme' => 'flexslider',
      '#items' => $result,
      '#settings' => $display['settings'],
    );
  }
  return $element;
}