View source
<?php
DEFINE('CVM_FIELD_NAME', 'field_cvm_cvm');
function cvm_field_field_info() {
return array(
'cvm_field_cvm' => array(
'label' => t('Contextual View Modes'),
'description' => t('Allows users to select a view mode and a context to apply it on'),
'settings' => array(),
'instance_settings' => array(
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
),
'default_widget' => 'cvm_field_widget',
'default_formatter' => 'cvm_field_no_output',
'no_ui' => TRUE,
),
);
}
function cvm_field_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
$found = array();
foreach ($items as $delta => $item) {
if (!empty($item['context']) && $item['context'] !== "none") {
if (in_array($item['context'], $found)) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'cvm_field_invalid',
'message' => t("Contextual View Mode Error: %itm context already in use!", array(
"%itm" => $item['context'],
)),
);
}
$found[] = $item['context'];
}
}
}
function cvm_field_field_is_empty($item, $field) {
if (empty($item['context']) || $item['context'] == "none") {
return TRUE;
}
if (empty($item['view_mode']) || $item['view_mode'] == "none") {
return TRUE;
}
return FALSE;
}
function cvm_field_field_formatter_info() {
return array(
'cvm_field_default' => array(
'label' => t('Default'),
'field types' => array(
'cvm_field_cvm',
),
),
);
}
function cvm_field_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
$settings = $display['settings'];
switch ($display['type']) {
case 'cvm_field_default':
foreach ($items as $delta => $item) {
$element[$delta] = array(
'#data' => $item['context'],
'#markup' => cvm_field_default_markup($entity_type, $entity, $field, $instance, $langcode, $items, $display),
);
}
break;
}
return $element;
}
function cvm_field_default_markup($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
if (empty($items)) {
return t('No contextual view modes set.');
}
$data = array();
foreach ($items as $k => $opts) {
$data[] = t('When context') . " <strong>" . $opts['context'] . "</strong> " . t('is true display with view mode') . ": <strong>" . $opts['view_mode'] . "</strong>";
}
$output = theme_item_list(array(
'items' => $data,
'type' => 'ul',
'title' => "",
'attributes' => array(),
));
return $output;
}
function cvm_field_field_widget_info() {
return array(
'cvm_field_picker' => array(
'label' => t('Contextual View Modes Picker'),
'field types' => array(
'cvm_field_cvm',
),
'settings' => array(
'progress_indicator' => 'throbber',
),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
'default value' => FIELD_BEHAVIOR_NONE,
),
),
);
}
function cvm_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$contexts = array(
'none' => '- Pick a context -',
);
$contexts += context_context_list();
foreach ($contexts as $key => $name) {
$contexts[$key] = ucwords(str_replace('_', " ", $name));
}
$ds_modes = ds_entity_view_modes('node');
$ds_modes_formatted = array(
'none' => '- None -',
);
foreach ($ds_modes as $name => $values) {
$ds_modes_formatted[$name] = ucwords(str_replace('_', " ", $values['label']));
}
$default_context = isset($items[$delta]['context']) ? $items[$delta]['context'] : '';
$default_view_mode = isset($items[$delta]['view_mode']) ? $items[$delta]['view_mode'] : '';
$widget = $element;
$widget['#delta'] = $delta;
$widget += array(
'#type' => 'fieldset',
'#element_validate' => array(
'cvm_field_picker_validate',
),
'#delta' => $delta,
'#attached' => array(
'css' => array(
drupal_get_path('module', 'cvm_field') . '/cvm_field.css',
),
),
);
$widget['context'] = array(
'#type' => 'select',
'#title' => t("When this context: "),
'#options' => $contexts,
'#default_value' => $default_context,
);
$widget['view_mode'] = array(
'#type' => 'select',
'#title' => t("Use this view mode: "),
'#options' => $ds_modes_formatted,
'#default_value' => $default_view_mode,
);
$element = $widget;
return $element;
}
function cvm_field_picker_validate($element, &$form_state) {
$delta = $element['#delta'];
$field = $form_state['field'][$element['#field_name']][$element['#language']]['field'];
$field_name = $field['field_name'];
if (isset($form_state['values'][$field_name][$element['#language']][$delta]['cvm'])) {
}
}
function cvm_field_field_widget_error($element, $error, $form, &$form_state) {
switch ($error['error']) {
case 'cvm_field_invalid':
form_error($element, $error['message']);
break;
}
}