You are here

function publication_date_form_node_form_alter in Publication Date 7.2

Same name and namespace in other branches
  1. 8.2 publication_date.module \publication_date_form_node_form_alter()
  2. 8 publication_date.module \publication_date_form_node_form_alter()
  3. 7 publication_date.module \publication_date_form_node_form_alter()

Implements hook_form_BASE_ID_alter().

Display the publication date on the node edit form.

@note: This won't work where you have Display Suite/REL enabled.

File

./publication_date.module, line 150
Add a field to nodes containing the publication date.

Code

function publication_date_form_node_form_alter(&$form, &$form_state, $form_id) {
  $node = $form["#node"];

  // If this is an existing node then get the currently set publication date.
  $published_at = $form['nid'] == NULL || empty($node->published_at) ? NULL : $node->published_at;

  // Check if the user has permission to edit the publication date.
  $pubdate_access = user_access('set any published on date') || user_access('set ' . $node->type . ' published on date');

  // Use the popup date picker provided by the Date module, if it is enabled and
  // the user has access to edit the publication date.
  if ($pubdate_access && module_exists('date_popup') && variable_get('publication_date_popup_enable', 1)) {
    $form['options']['pubdate'] = array(
      '#type' => 'date_popup',
      '#title' => t('Published on'),
      '#description' => t('Leave blank to use the time of form submission.'),
      '#date_type' => DATE_UNIX,
      '#date_timezone' => date_default_timezone(),
      '#date_format' => PUBLICATION_DATE_FORMAT,
      '#date_increment' => 1,
      '#date_year_range' => '-' . variable_get('publication_date_popup_year_start', '6') . ':+' . variable_get('publication_date_popup_year_end', '1'),
      '#weight' => -1,
      '#access' => TRUE,
      // If there is an existing publication date, set it as the default value.
      // The date popup field requires a date format without the timezone.
      '#default_value' => empty($published_at) ? '' : format_date($published_at, 'custom', PUBLICATION_DATE_FORMAT),
    );
  }
  else {
    $form['options']['pubdate'] = array(
      '#type' => 'textfield',
      '#title' => t('Published on'),
      '#maxlength' => 25,
      '#description' => t('Format: %time. Leave blank to use the time of form submission.', array(
        '%time' => format_date(REQUEST_TIME, 'custom', PUBLICATION_DATE_FORMAT),
      )),
      '#weight' => -1,
      '#access' => $pubdate_access,
      // If there is an existing publication date, set it as the default value.
      '#default_value' => empty($published_at) ? '' : format_date($published_at, 'custom', PUBLICATION_DATE_FORMAT_WITH_TZ),
    );
  }

  // If the user can access pubdate, we need to make sure they also have access
  // to the options group.
  if ($pubdate_access && $form['options']['#access'] == FALSE) {
    $form['options']['#access'] = TRUE;

    // Check all fields in the options group and, if access has not been set,
    // set it to FALSE. We don't want to grant access to any extra fields.
    $children = element_children($form['options']);
    foreach ($children as $value) {
      if (!isset($form['options'][$value]['#access'])) {
        $form['options'][$value]['#access'] = FALSE;
      }
    }
  }

  // Add custom validation and submit handlers.
  $form['#validate'][] = 'publication_date_pubdate_validate';
  $form['#submit'][] = 'publication_date_pubdate_submit';
}