-
Notifications
You must be signed in to change notification settings - Fork 11
/
paragraphs.field_formatter.inc
144 lines (125 loc) · 4.56 KB
/
paragraphs.field_formatter.inc
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
<?php
/**
* @file
* Holds relevant functions for paragraph field formatters.
*/
/**
* Implements hook_field_formatter_info().
*/
function paragraphs_field_formatter_info() {
return array(
'paragraphs_view' => array(
'label' => t('Paragraphs items'),
'field types' => array('paragraphs'),
'settings' => array(
'view_mode' => 'full',
),
),
);
}
/**
* Implements hook_field_formatter_settings_form().
*/
function paragraphs_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$elements = array();
if ($display['type'] == 'paragraphs_view') {
$entity_type = entity_get_info('paragraphs_item');
$options = array();
foreach ($entity_type['view modes'] as $mode => $info) {
$options[$mode] = $info['label'];
}
$elements['view_mode'] = array(
'#type' => 'select',
'#title' => t('View mode'),
'#options' => $options,
'#default_value' => $settings['view_mode'],
'#description' => t('Select the view mode'),
);
}
return $elements;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function paragraphs_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$output = array();
if ($display['type'] == 'paragraphs_view') {
$entity_type = entity_get_info('paragraphs_item');
if (!empty($entity_type['view modes'][$settings['view_mode']]['label'])) {
$output[] = t('View mode: @mode', array('@mode' => $entity_type['view modes'][$settings['view_mode']]['label']));
}
else {
$output[] = ' ';
}
}
return implode('<br>', $output);
}
/**
* Implements hook_field_prepare_view().
*
* Adds a dummy value to the paragraphs field to make rendering possible.
*/
function paragraphs_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) {
if ($field['type'] == 'paragraphs') {
foreach ($items as $key => $item) {
foreach ($item as $delta => $instance) {
if (!isset($instance['value'])) {
$items[$key][$delta]['value'] = 'scratch_paragraph';
}
}
}
}
}
/**
* Implements hook_field_formatter_view().
*/
function paragraphs_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
switch ($display['type']) {
case 'paragraphs_view':
$in_preview = &backdrop_static('in_preview');
if (!isset($in_preview)) {
$in_preview = !empty($entity->in_preview);
}
// Prevent displaying useless markup if we don't have any values.
if (empty($items)) {
return $element;
}
// Get view mode from entity.
$display_view_mode = empty($display['settings']['view_mode']) ? 'full' : $display['settings']['view_mode'];
// Get view mode from field instance (if configured).
$view_mode = empty($instance['display'][$display_view_mode]['settings']['view_mode']) ? $display_view_mode : $instance['display'][$display_view_mode]['settings']['view_mode'];
$show_field = FALSE;
foreach ($items as $delta => $item) {
if ($paragraph = paragraphs_field_get_entity($item)) {
if ($paragraph->status) {
$show_field = TRUE;
$paragraph->setHostEntity($entity_type, $entity, $langcode);
if (entity_access('view', 'paragraphs_item', $paragraph)) {
// Enter the paragraph item itself.
$element[$delta]['entity'] = $paragraph->view($view_mode, $langcode);
}
if (!empty($instance['settings']['modal_admin'])) {
$element[$delta]['entity']['paragraphs_item'][$paragraph->item_id]['#modal_admin'] = 1;
}
$element[$delta]['entity']['paragraphs_item'][$paragraph->item_id]['#in_preview'] = $in_preview;
}
}
}
if (!$show_field) {
// There are Paragraphs items, but none of them are visible.
return $element;
}
$element['#theme_wrappers'] = array('paragraphs_items');
$element['#attributes']['class'][] = backdrop_clean_css_identifier('paragraphs-items');
$element['#attributes']['class'][] = backdrop_clean_css_identifier('paragraphs-items-view-mode-' . $view_mode);
$element['#attributes']['class'][] = backdrop_clean_css_identifier('paragraphs-items-field-' . $instance['field_name']);
$element['#paragraphs_items_view_mode'] = $view_mode;
break;
}
return $element;
}