summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNancy Wichmann2013-02-21 20:40:09 (GMT)
committer Nancy Wichmann2013-02-21 20:40:09 (GMT)
commita13eee02c068942025b7bea16c31f97f3ba539ba (patch)
treed637036b59a0ca1695e4b7450d13ee4f4e6992d9
parentb7ded51ba4b6ec7e0950360291e50b96d88b042c (diff)
Issue #1532246 by NancyDru: Support View Modes.
-rw-r--r--submitted_by.install40
-rw-r--r--submitted_by.module90
2 files changed, 107 insertions, 23 deletions
diff --git a/submitted_by.install b/submitted_by.install
index 66b76bc..6ac9de7 100644
--- a/submitted_by.install
+++ b/submitted_by.install
@@ -15,3 +15,43 @@ function submitted_by_uninstall() {
variable_del('submitted_by_comment_' . $type);
}
}
+
+/**
+ * Convert settings for view modes.
+ */
+function submitted_by_update_7000(&$sandbox) {
+ // Get stuff from entity system.
+ $entity_info = entity_get_info('node');
+ $view_modes = array_keys($entity_info['view modes']);
+
+ // Get all node type names.
+ $types = node_type_get_names();
+
+ // Progress info.
+ $sandbox['max'] = count($types);
+ $sandbox['progress'] = 0;
+
+ // Go through each content type.
+ foreach ($types as $type => $name) {
+ $strings = array();
+ $var = "submitted_by_$type";
+ $current = variable_get($var, NULL);
+
+ // Just in case we didn't finish last time.
+ if (is_string($current)) {
+ // Build a new string array.
+ $strings = array_fill_keys($view_modes, $current);
+
+ // Save the array.
+ variable_set($var, $strings);
+ }
+
+ // Track our progress.
+ $sandbox['progress']++;
+ }
+
+ // Indicate that we're done.
+ $sandbox['#finished'] = 1;
+
+ return t('The Submitted By module is now ready for View modes.');
+}
diff --git a/submitted_by.module b/submitted_by.module
index bcd9d5b..76f39ec 100644
--- a/submitted_by.module
+++ b/submitted_by.module
@@ -11,8 +11,13 @@
*/
function submitted_by_form_node_type_form_alter(&$form, &$form_state) {
$type = isset($form['#node_type']->type) ? $form['#node_type']->type : '';
- $current_value = variable_get('submitted_by_' . $type, NULL);
$enabled = variable_get('node_submitted_' . $type, TRUE);
+ $current_value = variable_get('submitted_by_' . $type, array());
+
+ // Just in case the update hasn't run yet.
+ if (!is_array($current_value)) {
+ $current_value = array('full' => $current_value);
+ }
if (isset($form['type'])) {
$form['display']['submitted_by'] = array(
@@ -24,40 +29,76 @@ function submitted_by_form_node_type_form_alter(&$form, &$form_state) {
),
);
- // Note: node module will add "_type" to the variable name.
- $form['display']['submitted_by']['submitted_by'] = array(
- '#type' => 'textfield',
- '#maxlength' => 255,
- '#title' => t("Byline text"),
- '#default_value' => $enabled ? $current_value : NULL,
- '#description' => t("When a node is displayed, text in this box will be used to override the normal byline attribution and date-posted text. Default is \"Submitted by [node:author] on [node:created]\""),
- '#element_validate' => array('token_element_validate'),
- '#token_types' => array('node'),
- );
+ // Get info about view_modes.
+ $entity_info = entity_get_info('node');
+
+ $form['display']['submitted_by']['view_modes'] = array(
+ '#type' => 'value',
+ '#value' => $entity_info['view modes'],
+ );
+
+ foreach ($entity_info['view modes'] as $mode => $info) {
+ // Note: node module will add "_type" to the variable name.
+ $form['display']['submitted_by']["submitted_by_$mode"] = array(
+ '#type' => 'textfield',
+ '#maxlength' => 255,
+ '#title' => t("Byline text - @mode view mode", array('@mode' => $info['label'])),
+ '#default_value' => $enabled ? (isset($current_value[$mode]) ? $current_value[$mode] : '') : NULL,
+ '#description' => t('When a node is displayed in the "@mode" view mode, the text in this box
+ will be used to override the normal byline attribution and date-posted text.
+ Default is "Submitted by [node:author] on [node:created]"', array('@mode' => $info['label'])),
+ '#element_validate' => array('token_element_validate'),
+ '#token_types' => array('node'),
+ );
+ }
+
$form['display']['submitted_by']['token_help'] = array(
'#theme' => 'token_tree',
'#token_types' => array('node'),
- );
+ );
if (isset($form['comment'])) {
$form['comment']['submitted_by'] = array(
'#type' => 'container',
- );
+ );
$form['comment']['submitted_by']['submitted_by_comment'] = array(
'#type' => 'textfield',
'#maxlength' => 255,
'#title' => t("Byline text"),
'#default_value' => variable_get('submitted_by_comment_' . $type, NULL),
'#description' => t("When a comment is displayed, text in this box will be used to override the normal byline attribution and date-posted text. Default is \"Submitted by [comment:author] on [comment:created]\""),
- '#element_validate' => array('token_element_validate'),
- '#token_types' => array('comment'),
- );
+ '#element_validate' => array('token_element_validate'),
+ '#token_types' => array('comment'),
+ );
$form['comment']['submitted_by']['token_help'] = array(
'#theme' => 'token_tree',
'#token_types' => array('comment'),
- );
+ );
}
+
+ // Provide a submit handler to clean up our variables.
+ if (isset($form['#submit'])) {
+ array_unshift($form['#submit'], 'submitted_by_node_form_submit');
+ }
+ else {
+ $form['#submit'] = array('submitted_by_node_form_submit');
+ }
+ }
+}
+
+/**
+ * Submission handler to aggregate the patterns into an array.
+ */
+function submitted_by_node_form_submit($form, &$form_state) {
+ $strings = array();
+ // Get all the values.
+ foreach ($form_state['values']['view_modes'] as $mode => $info) {
+ $var = "submitted_by_$mode";
+ $strings[$mode] = $form_state['values'][$var];
+ unset($form_state['values'][$var]);
}
+ // Save the settings.
+ variable_set('submitted_by_' . $form_state['values']['type'], $strings);
}
/**
@@ -100,7 +141,7 @@ function submitted_by_node_view($node, $view_mode, $langcode) {
$fields = field_extra_fields_get_display('node', $node->type, $view_mode);
if (isset($fields['submitted_by']['visible']) && $fields['submitted_by']['visible']) {
$node->content['submitted'] =
- array('#markup' => '<span class="submitted-by">' . submitted_by_do_replace($node) . '</span>');
+ array('#markup' => '<span class="submitted-by">' . submitted_by_do_replace($node, $view_mode) . '</span>');
}
else {
$node->content['submitted'] = array(array());
@@ -109,12 +150,15 @@ function submitted_by_node_view($node, $view_mode, $langcode) {
return;
}
-function submitted_by_do_replace($node) {
- $submitted_by = variable_get('submitted_by_' . $node->type, NULL);
- if ($submitted_by) {
+/**
+ * Helper function to call token_replace().
+ */
+function submitted_by_do_replace($node, $view_mode = '') {
+ $submitted_by = variable_get('submitted_by_' . $node->type, array());
+ if ($submitted_by && !empty($submitted_by[$view_mode])) {
// Get node tokens.
// Note that we do not suppress un-replaced tokens.
- $output = token_replace($submitted_by, array('node' => $node));
+ $output = token_replace($submitted_by[$view_mode], array('node' => $node));
// Get whatever else can be found, like user stuff.
$output = token_replace($output);
@@ -130,7 +174,7 @@ function submitted_by_do_replace($node) {
*/
function submitted_by_process_node(&$variables) {
$node = $variables['node'];
- $variables['submitted'] = submitted_by_do_replace($node);
+ $variables['submitted'] = submitted_by_do_replace($node, $variables['view_mode']);
// Override the regular submitted variable.
if (isset($variables['content']['submitted_by'])) {