Skip to content

Commit 4f96060

Browse files
committed
Get field settings per view_mode working
1 parent 44c86c0 commit 4f96060

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

cs_date_formatter.module

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,17 @@ function cs_date_formatter_field_formatter_settings_summary($field, $instance, $
170170
return $summary;
171171
}
172172

173+
/**
174+
* Implements hook_field_display_alter().
175+
*
176+
* Hand over the view_mode to cs_date_formatter_field_formatter_view().
177+
*/
178+
function cs_date_formatter_field_display_alter(&$display, $context) {
179+
if ($display['type'] == 'cs_date_format') {
180+
$display['view_mode'] = $context['view_mode'];
181+
}
182+
}
183+
173184
/**
174185
* Implements hook_field_formatter_view().
175186
*/
@@ -183,6 +194,7 @@ function cs_date_formatter_field_formatter_view($entity_type, $entity, $field, $
183194
'#theme' => 'cs_date_formatter_default',
184195
'#element' => $item,
185196
'#field' => $instance,
197+
'#view_mode' => $display['view_mode'],
186198
);
187199

188200
// Attach our Javascript and settings.
@@ -201,9 +213,10 @@ function cs_date_formatter_field_formatter_view($entity_type, $entity, $field, $
201213
$js_settings['date_format'] = 'simple';
202214
$js_settings['extended'] = array();
203215
}
216+
$setting_name = $field['field_name'] . '-' . $display['view_mode'];
204217
$element[$delta]['#attached']['js'][] = array(
205218
'data' => array(
206-
'csDateFormatter' => array($field['field_name'] => $js_settings),
219+
'csDateFormatter' => array($setting_name => $js_settings),
207220
),
208221
'type' => 'setting',
209222
);
@@ -222,6 +235,7 @@ function cs_date_formatter_theme($existing, $type, $theme, $path) {
222235
'variables' => array(
223236
'element' => NULL,
224237
'field' => NULL,
238+
'view_mode' => NULL,
225239
),
226240
),
227241
);
@@ -233,14 +247,16 @@ function cs_date_formatter_theme($existing, $type, $theme, $path) {
233247
function theme_cs_date_formatter_default($variables) {
234248
$values = _cs_date_formatter_get_timestamp($variables['element']);
235249
$tags = array();
250+
251+
$setting_name = $variables['field']['field_name'] . '-' . $variables['view_mode'];
236252
foreach ($values as $key => $value) {
237253
// @todo make fallback format configurable?
238254
$fallback = format_date($value, 'html_datetime');
239255
$attributes = array(
240256
'datetime' => $fallback,
241257
'class' => array('cs-date-format'),
242258
'data-cs-timestamp' => $value,
243-
'data-cs-setting-name' => $variables['field']['field_name'],
259+
'data-cs-setting-name' => $setting_name,
244260
);
245261
$tags[] = '<time ' . backdrop_attributes($attributes) . '>' . $fallback . '</time>';
246262
}

0 commit comments

Comments
 (0)