context_reaction.view_mode.inc in Contextual View Modes 7.3
File
plugins/context_reaction.view_mode.inc
View source
<?php
class context_reaction_view_mode extends context_reaction {
function options_form($context) {
$form = parent::options_form($context);
$options = $this
->fetch_from_context($context);
$entity_info = entity_get_info();
unset($entity_info['comment']);
unset($entity_info['taxonomy_vocabulary']);
$form['entity_types'] = array(
'#type' => 'vertical_tabs',
);
foreach ($entity_info as $entity_type => $info) {
$form['entity_types'][$entity_type] = array(
'#title' => check_plain($info['label']),
'#description' => t('View mode selection for ') . $info['label'],
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#group' => 'entity_types',
'#tree' => TRUE,
);
$modes = array(
"none" => t("Not set"),
);
foreach ($info['view modes'] as $mode_key => $mode_info) {
$modes[$mode_key] = $mode_info['label'];
}
foreach ($info['bundles'] as $machine_name => $bundle_info) {
$default = isset($options['entity_types'][$entity_type][$machine_name]) ? $options['entity_types'][$entity_type][$machine_name] : 'none';
$form['entity_types'][$entity_type][$machine_name] = array(
'#title' => $bundle_info['label'] . " " . t("view mode"),
'#description' => t("Select the view mode for this entity bundle type when this context is active"),
'#type' => 'select',
'#options' => $modes,
'#default_value' => $default,
);
}
}
return $form;
}
function execute(&$entity, $type) {
$wrapped = entity_metadata_wrapper($type, $entity);
$bundle = $wrapped
->getBundle();
$contexts = $this
->get_contexts();
foreach ($contexts as $context) {
$options = $this
->fetch_from_context($context);
if (!isset($options['entity_types'][$type][$bundle])) {
continue;
}
if ($options['entity_types'][$type][$bundle] !== "none") {
return $options['entity_types'][$type][$bundle];
}
}
return FALSE;
}
}