You are here

submitted_by.module in Submitted By 6

Same filename and directory in other branches
  1. 7 submitted_by.module

Take over the "Submitted by" theme function to allow different content types to have different strings.

File

submitted_by.module
View source
<?php

/**
 * @file
 * Take over the "Submitted by" theme function to allow different
 * content types to have different strings.
 */

/**
 * An implementation of hook_theme_registry_alter()
 *
 * Swap in our own replacement for theme_node_submitted(), allowing the
 * site admin to configure the string on a per-nodetype basis.
 **/
function submitted_by_theme_registry_alter(&$theme_registry) {
  if (!empty($theme_registry['form_element'])) {
    $theme_registry['node_submitted']['function'] = 'submitted_by_node_submitted';
    $theme_registry['comment_submitted']['function'] = 'submitted_by_comment_submitted';
  }
}

/**
 * Implementation of hook_form_alter().
 *
 * Add the pattern field to the node edit form for comments.
 */
function submitted_by_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'node_type_form':
      if (isset($form['comment'])) {
        $form['comment']['comment']['#weight'] = -5;
        $type = isset($form['#node_type']->type) ? $form['#node_type']->type : 'default';
        $default = variable_get('submitted_by_comment_' . $type, NULL);
        $form['comment']['appearance'] = array(
          '#type' => 'fieldset',
          '#collapsible' => TRUE,
          '#collapsed' => empty($default),
          '#title' => t('"Submitted by" Appearance'),
        );
        $form['comment']['appearance']['#weight'] = -1;
        $form['comment']['appearance']['submitted_by']['submitted_by_comment'] = array(
          '#type' => 'textfield',
          '#maxlength' => 255,
          '#title' => t("'Submitted by...' text"),
          '#default_value' => $default,
          '#description' => t("When a comment is displayed, text in this box will be used to override the normal attribution and date-posted text."),
        );
        if ($def_default = variable_get('submitted_by_comment_default', NULL)) {
          $form['comment']['appearance']['submitted_by']['submitted_by_comment']['#description'] .= ' ' . t('The system default value is "%deflt."', array(
            '%deflt' => $def_default,
          ));
        }
        $form['comment']['appearance']['submitted_by']['help'] = array(
          '#type' => 'fieldset',
          '#collapsible' => TRUE,
          '#collapsed' => TRUE,
          '#title' => t('Replacement tokens'),
          '#description' => t("The following tokens can be used in the Submitted By... text. They will be replaced with the appropriate values."),
        );
        $form['comment']['appearance']['submitted_by']['help']['tokens'] = array(
          '#value' => theme('token_help', 'comment'),
        );
      }
      break;
    case 'node_configure':

      // Admin > Content > Post settings.
      // Would this be better in system_theme_settings? ['node_info'], system_theme_settings_submit.
      $node_default = variable_get('submitted_by_default', NULL);
      $form['appearance'] = array(
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
        '#collapsed' => empty($node_default),
        '#title' => t('"Submitted by" Appearance'),
      );
      $form['appearance']['submitted_by']['#weight'] = -1;

      // Note: node module will add "_type" to the variable name.
      $form['appearance']['submitted_by']['submitted_by_default'] = array(
        '#type' => 'textfield',
        '#maxlength' => 255,
        '#title' => t("Node 'Submitted by...' text"),
        '#default_value' => $node_default,
        '#description' => t("When a node is displayed, text in this box will be used to override the normal attribution and date-posted text."),
      );
      $form['appearance']['submitted_by']['help'] = array(
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#title' => t('Replacement tokens'),
        '#description' => t("The following tokens can be used in the Submitted By... text. They will be replaced with the appropriate values."),
      );
      $form['appearance']['submitted_by']['help']['tokens'] = array(
        '#value' => theme('token_help', 'node'),
      );
      if (module_exists('comment')) {
        $com_default = variable_get('submitted_by_comment_default', NULL);
        $form['comment']['appearance'] = array(
          '#type' => 'fieldset',
          '#collapsible' => TRUE,
          '#collapsed' => empty($com_default),
          '#title' => t('Comment "Submitted by" Appearance'),
          '#weight' => -1,
        );
        $form['comment']['appearance']['submitted_by']['submitted_by_comment_default'] = array(
          '#type' => 'textfield',
          '#maxlength' => 255,
          '#title' => t("'Submitted by...' text"),
          '#default_value' => $com_default,
          '#description' => t("When a comment is displayed, text in this box will be used to override the normal attribution and date-posted text."),
        );
        $form['comment']['appearance']['submitted_by']['help'] = array(
          '#type' => 'fieldset',
          '#collapsible' => TRUE,
          '#collapsed' => TRUE,
          '#title' => t('Replacement tokens'),
          '#description' => t("The following tokens can be used in the Submitted By... text. They will be replaced with the appropriate values."),
        );
        $form['comment']['appearance']['submitted_by']['help']['tokens'] = array(
          '#value' => theme('token_help', 'comment'),
        );
        $form['buttons']['#weight'] = 99;

        //     drupal_set_message(show_array($form));
        break;
      }
  }
}

/**
 * Implementation of hook_form_FORM_ID_alter().
 *
 * Add the pattern field to the node edit form.
 */
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);
  $form['appearance'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => !$current_value,
    '#title' => t('"Submitted by" Appearance'),
  );
  $form['appearance']['submitted_by']['#weight'] = -1;

  // Note: node module will add "_type" to the variable name.
  $form['appearance']['submitted_by']['submitted_by'] = array(
    '#type' => 'textfield',
    '#maxlength' => 255,
    '#title' => t("'Submitted by...' text"),
    '#default_value' => variable_get('submitted_by_' . $type, NULL),
    '#description' => t("When a node is displayed, text in this box will be used to override the normal attribution and date-posted text."),
  );
  if ($def_default = variable_get('submitted_by_default', NULL)) {
    $form['appearance']['submitted_by']['submitted_by']['#description'] .= ' ' . t('The system default value is "%deflt."', array(
      '%deflt' => $def_default,
    ));
  }
  $form['appearance']['submitted_by']['help'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#title' => t('Replacement tokens'),
    '#description' => t("The following tokens can be used in the Submitted By... text. They will be replaced with the appropriate values."),
  );
  $form['appearance']['submitted_by']['help']['tokens'] = array(
    '#value' => theme('token_help', 'node'),
  );
}

/**
 * Format the "Submitted by username on date/time" for each comment.
 * varying the results by node type.
 */
function submitted_by_comment_submitted($comment) {
  $type = db_result(db_query(db_rewrite_sql("SELECT n.type FROM {node} n WHERE n.nid=%d"), $comment->nid));
  if (variable_get('submitted_by_comment_' . $type, NULL)) {
    $submitted = variable_get('submitted_by_comment_' . $type, NULL);
  }
  else {
    $submitted = variable_get('submitted_by_comment_default', NULL);
  }
  if ($submitted) {
    return filter_xss_admin(token_replace($submitted, 'comment', $comment));
  }
  else {
    return t('Submitted by !username on @datetime', array(
      '!username' => theme('username', $comment),
      '@datetime' => format_date($comment->timestamp),
    ));
  }
}

/**
 * Format the "Submitted by username on date/time" for each node,
 * varying the results by node type.
 */
function submitted_by_node_submitted($node) {
  if (variable_get('submitted_by_' . $node->type, NULL)) {
    $submitted = variable_get('submitted_by_' . $node->type, NULL);
  }
  else {
    $submitted = variable_get('submitted_by_default', NULL);
  }
  if ($submitted) {
    return filter_xss_admin(token_replace(t($submitted), 'node', $node));
  }
  else {
    return t('Submitted by !username on @datetime', array(
      '!username' => theme('username', $node),
      '@datetime' => format_date($node->created),
    ));
  }
}

/**
 * Because the normal submission line offers a quick and easy link to the
 * user's account page, we'll provide a token for that. Token should
 * probably have this built in.
 */
function submitted_by_token_values($type, $object = NULL, $options = array()) {
  $tokens = array();
  switch ($type) {
    case 'node':
      $node = $object;
      $tokens['author-link'] = theme('username', $node);
      $tokens['created-since'] = format_interval(time() - $node->created);

      // Conditional last edit tokens. See http://drupal.org/node/377726
      $editor_uid = db_result(db_query("SELECT uid FROM {node_revisions} WHERE vid=%d", $object->vid));
      $editor = user_load(array(
        'uid' => $editor_uid,
      ));
      $tokens['last-editor'] = theme('username', $editor);
      if ($object->changed > $object->created) {
        $last_edit_date = $object->changed;
        $last_edit_type = t('changed');
        $tokens['created-updated'] = t('created !created, updated !updated', array(
          '!created' => format_date($object->created, 'small'),
          '!updated' => format_date($object->changed, 'small'),
        ));
      }
      else {
        $last_edit_date = $object->created;
        $last_edit_type = t('created');
        $tokens['created-updated'] = t('created !created', array(
          '!created' => format_date($object->created, 'small'),
        ));
      }
      $tokens['last-edit-type'] = $last_edit_type;
      $tokens += _submitted_by_token_date($last_edit_date, 'last-edit');
      break;
    case 'comment':
      $tokens['comment-since'] = $object->timestamp ? format_interval(time() - $object->timestamp) : t('Never');
      $tokens['author-link'] = theme('username', $object);
      break;
  }
  return $tokens;
}
function submitted_by_token_list($type = 'all') {
  $tokens = array();
  if ($type == 'node') {
    $tokens['node']['author-link'] = t('Link to author.');
    $tokens['node']['created-since'] = t('Node created date - interval.');

    // Conditional last edit tokens. See http://drupal.org/node/377726
    $tokens['node']['last-editor'] = t('The last user to edit the node.');
    $tokens['node']['last-edit-type'] = t('The last edit type (created, changed) for the node.');
    $tokens['node']['last-edit-small'] = t('Node last edit - small');
    $tokens['node']['last-edit-medium'] = t('Node last edit - medium');
    $tokens['node']['last-edit-large'] = t('Node last edit - large');
    $tokens['node']['last-edit-since'] = t('Node last edit - interval');
    $tokens['node']['last-edit-yyyy'] = t('Node last edit year (four digit)');
    $tokens['node']['last-edit-yy'] = t('Node last edit year (two digit)');
    $tokens['node']['last-edit-month'] = t('Node last edit month (full word)');
    $tokens['node']['last-edit-mon'] = t('Node last edit month (abbreviated)');
    $tokens['node']['last-edit-mm'] = t('Node last edit month (two digit, zero padded)');
    $tokens['node']['last-edit-m'] = t('Node last edit month (one or two digit)');
    $tokens['node']['last-edit-ww'] = t('Node last edit week (two digit)');

    // ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0)
    if (version_compare(PHP_VERSION, '5.1.0', '>')) {
      $tokens['node']['last-edit-date'] = t('Node last edit date (day of week)');
    }
    $tokens['node']['last-edit-day'] = t('Node last edit day (full word)');
    $tokens['node']['last-edit-ddd'] = t('Node last edit day (abbreviation)');
    $tokens['node']['last-edit-dd'] = t('Node last edit day (two digit, zero-padded)');
    $tokens['node']['last-edit-d'] = t('Node last edit day (one or two digit)');
    $tokens['node']['last-edit-hh'] = t('Node last edit hour in 12-hour format(two digit, zero-padded)');
    $tokens['node']['last-edit-ii'] = t('Node last edit minute (two digit, zero-padded)');
    $tokens['node']['last-edit-ss'] = t('Node last edit second (two digit, zero-padded)');
    $tokens['node']['created-updated'] = t('Node created and updated dates (system small date)');
  }
  else {
    if ($type == 'comment') {
      $tokens['comment']['comment-since'] = t('Comment create date - interval');
      $tokens['comment']['author-link'] = t('Link to author.');
    }
  }
  return $tokens;
}
function _submitted_by_token_date($date, $prefix = NULL) {
  $date = (int) $date;
  $tz = variable_get('date_default_timezone', 0);
  if ($prefix) {
    $prefix .= '-';
  }
  $values = array(
    $prefix . 'small' => format_date($date, 'small', '', $tz),
    $prefix . 'medium' => format_date($date, 'medium', '', $tz),
    $prefix . 'large' => format_date($date, 'large', '', $tz),
    $prefix . 'since' => $date ? format_interval(time() - $date) : t('Never'),
    $prefix . 'yyyy' => format_date($date, 'custom', 'Y', $tz),
    $prefix . 'yy' => format_date($date, 'custom', 'y', $tz),
    $prefix . 'month' => t(format_date($date, 'custom', 'F', $tz)),
    $prefix . 'mon' => t(format_date($date, 'custom', 'M', $tz)),
    $prefix . 'mm' => format_date($date, 'custom', 'm', $tz),
    $prefix . 'm' => format_date($date, 'custom', 'n', $tz),
    $prefix . 'ww' => format_date($date, 'custom', 'W', $tz),
    $prefix . 'date' => format_date($date, 'custom', 'N', $tz),
    $prefix . 'day' => t(format_date($date, 'custom', 'l', $tz)),
    $prefix . 'ddd' => t(format_date($date, 'custom', 'D', $tz)),
    $prefix . 'dd' => format_date($date, 'custom', 'd', $tz),
    $prefix . 'd' => format_date($date, 'custom', 'j', $tz),
    $prefix . 'hh' => format_date($date, 'custom', 'h', $tz),
    $prefix . 'ii' => format_date($date, 'custom', 'i', $tz),
    $prefix . 'ss' => format_date($date, 'custom', 's', $tz),
  );
  return $values;
}

Functions

Namesort descending Description
submitted_by_comment_submitted Format the "Submitted by username on date/time" for each comment. varying the results by node type.
submitted_by_form_alter Implementation of hook_form_alter().
submitted_by_form_node_type_form_alter Implementation of hook_form_FORM_ID_alter().
submitted_by_node_submitted Format the "Submitted by username on date/time" for each node, varying the results by node type.
submitted_by_theme_registry_alter An implementation of hook_theme_registry_alter()
submitted_by_token_list
submitted_by_token_values Because the normal submission line offers a quick and easy link to the user's account page, we'll provide a token for that. Token should probably have this built in.
_submitted_by_token_date