View source
<?php
function contextual_view_modes_users_form_user_profile_form_alter(&$form, &$form_state) {
$user = isset($form['#user']) ? $form['#user'] : NULL;
list($uid) = entity_extract_ids('user', $user);
$form['contextual_view_modes'] = array(
'#type' => 'fieldset',
'#title' => t('Contextual View Modes'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#access' => user_access('set view modes per user'),
'#weight' => 50,
);
$context_options = contextual_view_modes_get_context_options();
$view_modes_options = contextual_view_modes_get_view_mode_options('user');
$count_context = count($context_options);
if ($count_context <= 1) {
$vm['nothing']['#markup'] = "<p>" . t("Please %context_link before using contextual view modes.", array(
"%context_link" => l('create a context', 'admin/structure/context/add'),
)) . "</p>";
return;
}
$form['contextual_view_modes']['help']['#markup'] = "<p><b>" . t("Instructions") . ":</b><p>" . t("This module uses the context module in order to trigger the view mode change. First, create a context with the conditions you wish to trigger the view mode change with. For example, if I wanted to change the view mode of this user by wether or not the user was logged in I would create a context with the condition of user role of authenticated. There is no need to add anything to the reactions portion of the context configuration. From the options below you can then select the name of that context and the view mode you wish to display to authenticated users. You can create additional view modes using the display suite module.") . "</p>";
$form['contextual_view_modes']['cvm'] = array(
'#type' => 'container',
'#id' => "edit-cvm-wrapper",
'#tree' => TRUE,
);
$vm =& $form['contextual_view_modes']['cvm'];
$settings = variable_get("contextual_view_modes_user", array());
if (empty($form_state['num_contexts'])) {
$form_state['num_contexts'] = isset($settings[$uid]) ? count($settings[$uid]) : 0;
}
for ($delta = 0; $delta <= $form_state['num_contexts']; $delta++) {
$vm['grouping'][$delta] = array(
'#type' => 'fieldset',
'#title' => t('Condition #') . ($delta + 1),
'#collapsed' => FALSE,
'#collapsible' => TRUE,
);
$vm['grouping'][$delta]['context_name'] = array(
'#type' => 'select',
'#options' => $context_options,
'#title' => t('Context'),
'#description' => t("When this context is active and the condition are met display the view mode below."),
'#default_value' => isset($settings[$uid][$delta]['context_name']) ? $settings[$uid][$delta]['context_name'] : "",
);
$vm['grouping'][$delta]['view_mode'] = array(
'#type' => 'select',
'#options' => $view_modes_options,
'#title' => t('View mode'),
'#description' => t("Display this view mode when the above context is active."),
'#default_value' => isset($settings[$uid][$delta]['view_mode']) ? $settings[$uid][$delta]['view_mode'] : "",
);
}
$vm['add_nuther'] = array(
'#type' => 'submit',
'#value' => t('Add another'),
'#name' => 'add-nuther' . $delta,
'#submit' => array(
'contextual_view_modes_user_add_nuther',
),
'#ajax' => array(
'callback' => 'contextual_view_modes_user_add_nuther_callback',
'wrapper' => 'edit-cvm-wrapper',
),
);
array_unshift($form["#submit"], "contextual_view_modes_user_form_user_form_submit");
}
function contextual_view_modes_user_add_nuther_callback($form, $form_state) {
return $form['contextual_view_modes']['cvm'];
}
function contextual_view_modes_user_add_nuther($form, &$form_state) {
$form_state['num_contexts']++;
$form_state['rebuild'] = TRUE;
}
function contextual_view_modes_user_form_user_form_submit($form, &$form_state) {
$values = $form_state['values'];
foreach ($values['cvm']['grouping'] as $delta => $settings) {
if (empty($settings['context_name']) || $settings['context_name'] == "none") {
unset($form_state['values']['cvm']['grouping'][$delta]);
unset($form_state['input']['cvm']['grouping'][$delta]);
continue;
}
if (empty($settings['view_mode']) || $settings['view_mode'] == "none") {
unset($form_state['values']['cvm']['grouping'][$delta]);
unset($form_state['input']['cvm']['grouping'][$delta]);
}
}
sort($form_state['values']['cvm']['grouping']);
$form_state['user']->cvm = $form_state['values']['cvm']['grouping'];
}
function contextual_view_modes_users_user_insert($user) {
contextual_view_modes_users_user_update($user);
}
function contextual_view_modes_users_user_update($user) {
$uid = $user['original']->uid;
$settings = variable_get("contextual_view_modes_user", array());
$settings[$uid] = $user['cvm']['grouping'];
variable_set("contextual_view_modes_user", $settings);
}