Initial commit of module code.
[project/flexslider_entityreference.git] / flexslider_entityreference.module
1 <?php
2 /**
3 * Implements hook_field_formatter_info().
4 */
5 function flexslider_entityreference_field_formatter_info() {
6 return array(
7 'entityreference_entity_flexslider' => array(
8 'label' => t('FlexSlider'),
9 'description' => t('Displays the rendered entities in a FlexSlider.'),
10 'field types' => array('entityreference'),
11 'settings' => array(
12 'view_mode' => '',
13 'view_mode_thumb' => '',
14 'optionset' => '',
15 ),
16 ),
17 );
18 }
19
20 /**
21 * Implements hook_field_formatter_settings_form().
22 */
23 function flexslider_entityreference_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
24 $display = $instance['display'][$view_mode];
25 $settings = $display['settings'];
26
27 $form = array();
28
29 // Show select box for the option set
30 $optionsets = array();
31 ctools_include('export');
32 foreach (flexslider_optionset_load_all() as $name => $optionset) {
33 $optionsets[$name] = check_plain($optionset->title);
34 }
35 $form['optionset'] = array(
36 '#title' => t('Option set'),
37 '#type' => 'select',
38 '#options' => $optionsets,
39 '#default_value' => $settings['optionset'],
40 );
41
42 $entity_info = entity_get_info($field['settings']['target_type']);
43 $options = array();
44 if (!empty($entity_info['view modes'])) {
45 foreach ($entity_info['view modes'] as $view_mode => $view_mode_settings) {
46 $options[$view_mode] = $view_mode_settings['label'];
47 }
48 }
49 if (count($options) > 1) {
50 $form['view_mode'] = array(
51 '#type' => 'select',
52 '#options' => $options,
53 '#title' => t('Slide view mode'),
54 '#default_value' => $settings['view_mode'],
55 );
56 $form['view_mode_thumb'] = array(
57 '#type' => 'select',
58 '#options' => array('_empty' => '(' . t('Empty string') . ')') + $options,
59 '#title' => t('Thumbnail view mode'),
60 '#default_value' => $settings['view_mode_thumb'],
61 );
62 }
63 return $form;
64 }
65
66 /**
67 * Implements hook_field_formatter_settings_summary().
68 */
69 function flexslider_entityreference_field_formatter_settings_summary($field, $instance, $view_mode) {
70 $display = $instance['display'][$view_mode];
71 $settings = $display['settings'];
72
73 $summary = array();
74
75 // Load option set
76 ctools_include('export');
77 if (!empty($settings['optionset'])) {
78 $o = flexslider_optionset_load($settings['optionset']);
79 if ($o !== NULL) {
80 $optionset = $o;
81 }
82 }
83
84 // Build settings summary
85 $optionset = isset($optionset) ? $optionset->title : t('Default settings');
86 $summary[] = t('Option set: %optionset', array('%optionset' => $optionset));
87
88 $entity_info = entity_get_info($field['settings']['target_type']);
89 $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']));
90 if ($settings['view_mode_thumb'] == '_empty') {
91 $summary[] = t('Thumbnails rendered as @mode', array('@mode' => t('empty strings')));
92 } else {
93 $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']));
94 }
95
96 return implode('<br />', $summary);
97 }
98
99 /**
100 * Implements hook_field_formatter_prepare_view().
101 */
102 function flexslider_entityreference_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
103 entityreference_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, $items, $displays);
104 }
105
106 /**
107 * Implements hook_field_formatter_view().
108 */
109 function flexslider_entityreference_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
110 $result = array();
111 $settings = $display['settings'];
112
113 // Rebuild the items list to contain only those with access.
114 foreach ($items as $key => $item) {
115 if (empty($item['access'])) {
116 unset($items[$key]);
117 }
118 }
119 switch ($display['type']) {
120 case 'entityreference_entity_flexslider':
121 foreach ($items as $delta => $item) {
122 // Protect ourselves from recursive rendering.
123 static $depth = 0;
124 $depth++;
125 if ($depth > 20) {
126 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'])));
127 }
128
129 $entity = clone $item['entity'];
130 unset($entity->content);
131 $entity_build = entity_view($field['settings']['target_type'], array($item['target_id'] => $entity), $settings['view_mode'], $langcode, FALSE);
132 $result[$delta] = array(
133 'slide' => drupal_render($entity_build),
134 'thumb' => '',
135 );
136 if ($settings['view_mode_thumb'] !== '_empty') {
137 $result[$delta]['thumb'] = drupal_render(entity_view($field['settings']['target_type'], array($item['target_id'] => $entity), $settings['view_mode_thumb'], $langcode, FALSE));
138 }
139 $depth = 0;
140 }
141 break;
142 }
143 $element = array();
144 if (count($result) > 0) {
145 $element[] = array(
146 '#theme' => 'flexslider',
147 '#items' => $result,
148 '#settings' => $display['settings'],
149 );
150 }
151 return $element;
152 }