You are here

function _option_cfs_newadmin_validate in Node and Comments Form Settings 7.3

Same name and namespace in other branches
  1. 7.2 commentformsettings/includes/option_cfs_newadmin.inc \_option_cfs_newadmin_validate()

Function callback to handle comment form submit and save new fields data.

Parameters

$form Array: An array of form elements

$form_state: An array of form state elements

1 string reference to '_option_cfs_newadmin_validate'
_option_cfs_newadmin in commentformsettings/includes/option_cfs_newadmin.inc
@file Hide the Revision log field.

File

commentformsettings/includes/option_cfs_newadmin.inc, line 71
Hide the Revision log field.

Code

function _option_cfs_newadmin_validate($form, &$form_state) {

  // Set author override.
  $author = trim($form_state['input']['author']);

  // Make sure we have an author information entered by user.
  // If author is empty, the current user is taken in case of comment add.
  // If author is empty, the current value remains in case of comment edit.
  if ($author) {

    // For anonymous user, uid is zero.
    if (strtolower($author) == 'anonymous') {
      $form_state['values']['uid'] = 0;
      $form_state['values']['name'] = $author;
    }
    else {

      // If auth user, load the user by name.
      $users = user_load_multiple(array(), array(
        'name' => $author,
        'status' => '1',
      ));

      // Get account of this user.
      $account = reset($users);

      // If entered author is not available, then show invalid user error.
      if (!empty($account->uid)) {
        $form_state['values']['uid'] = $account->uid;
        $form_state['values']['name'] = $account->name;
      }
      else {
        form_set_error('author', t('Please enter a valid user'));
      }
    }
  }

  // If date is empty, the current time is taken in case of comment add.
  // If date is empty, the current value remains in case of comment edit.
  // Set date override.
  if (trim($form_state['input']['date'])) {

    // Get date in timestamp format.
    $date = strtotime($form_state['input']['date']);

    // Validate for a valid date.
    if ($date && $date > 0 && is_numeric($date)) {
      $form_state['values']['created'] = $date;
      $form_state['values']['changed'] = $date;
      $form_state['values']['date'] = $form_state['input']['date'];

      // Comment created date is taken from comment object and
      // not from form_state['values'], so filling the comment object here.
      $form_state['comment']->date = $form_state['input']['date'];
    }
    else {
      form_set_error('date', t('Please enter a valid date'));
    }
  }

  // Set status override always.
  $form_state['values']['status'] = $form_state['input']['status'];
}