You are here

function protected_node_node_form_alter in Protected Node 7

Same name and namespace in other branches
  1. 6 protected_node.settings.inc \protected_node_node_form_alter()
  2. 1.0.x protected_node.settings.inc \protected_node_node_form_alter()

Actual implementation of the protected_node_form_alter() function.

It was moved here to optimize the module (so the .module is smaller) This is really only necessary when some edits a node.

@param[in,out] $form The form to be altered with the protected node field set.

1 call to protected_node_node_form_alter()
protected_node_form_node_form_alter in ./protected_node.module
Implements hook_form_FORM_ID_alter().

File

./protected_node.settings.inc, line 109
Configuration file for the protected_node module.

Code

function protected_node_node_form_alter(&$form) {
  $protection = variable_get('protected_node_protection_' . $form['type']['#value'], PROTECTED_NODE_PROTECTION_PROTECTABLE);
  if ($protection == PROTECTED_NODE_PROTECTION_NEVER) {

    // Never protecting, do nothing.
    return;
  }

  // Turn off the auto-complete feature on this form since it could otherwise
  // pre-fill the password with the wrong parameter.
  $form['#attributes']['autocomplete'] = 'new-password';
  $node_type = node_type_get_type($form['type']['#value']);
  $fieldset_mode = variable_get('protected_node_fieldset_' . $node_type->type, PROTECTED_NODE_FIELDSET_SMART);
  switch ($fieldset_mode) {
    case PROTECTED_NODE_FIELDSET_OPEN:
      $collapsed = FALSE;
      break;
    case PROTECTED_NODE_FIELDSET_CLOSE:
      $collapsed = TRUE;
      break;

    // Case PROTECTED_NODE_FIELDSET_SMART.
    default:
      if ($protection == PROTECTED_NODE_PROTECTION_ALWAYS || $protection == PROTECTED_NODE_PROTECTION_PROTECTED && empty($form['nid']['#value'])) {
        $collapsed = FALSE;
      }
      else {
        $collapsed = empty($form['#node']->protected_node_is_protected);
      }
      break;
  }
  $form['protected_node'] = array(
    '#type' => 'fieldset',
    '#title' => t('Password protect this @node', array(
      '@node' => $node_type->name,
    )),
    '#collapsible' => TRUE,
    '#collapsed' => $collapsed,
    '#group' => 'additional_settings',
  );
  if ($protection == PROTECTED_NODE_PROTECTION_ALWAYS) {

    // Always protected, don't show anything to the user.
    $form['protected_node']['protected_node_is_protected'] = array(
      '#type' => 'value',
      '#value' => TRUE,
    );
  }
  else {

    // Define the current default value.
    if (empty($form['nid']['#value'])) {

      // A new node is being created, use the protection mode to determine the
      // default.
      // No need to test for NEVER, we're already out of this function in that
      // case.
      // No need to test for ALWAYS, we went through the previous level if() in
      // that case.
      $protected = $protection == PROTECTED_NODE_PROTECTION_PROTECTED;
    }
    else {
      $protected = !empty($form['#node']->protected_node_is_protected);
    }
    $form['protected_node']['protected_node_is_protected'] = array(
      '#type' => 'checkbox',
      '#title' => t('This @node is protected', array(
        '@node' => $node_type->name,
      )),
      '#description' => t('Select this checkbox to password protect this page.'),
      '#default_value' => $protected,
    );
  }
  if (isset($form['#node']->protected_node_show_title)) {
    $show_title = $form['#node']->protected_node_show_title;
  }
  else {
    $show_title = variable_get('protected_node_show_node_titles', FALSE);
  }

  // When custom description text is set there is no use for the Show Title
  // checkbox.
  $content_type_text = variable_get('protected_node_description_' . $form['#bundle'], '');
  $global_text = variable_get('protected_node_description', '');
  if (empty($global_text) && empty($content_type_text)) {
    $form['protected_node']['protected_node_show_title'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show Title'),
      '#description' => t('Show the title to users when requesting the password.'),
      '#default_value' => $show_title,
    );
  }
  else {
    $form['protected_node']['protected_node_show_title'] = array(
      '#type' => 'hidden',
      '#default_value' => $show_title,
    );
  }

  // Define a password field unless only the global password should be used.
  switch (variable_get('protected_node_use_global_password', PROTECTED_NODE_PER_NODE_PASSWORD)) {
    case PROTECTED_NODE_PER_NODE_PASSWORD:
    case PROTECTED_NODE_PER_NODE_AND_GLOBAL_PASSWORD:

      // Description based on the user permissions.
      if (user_access('administer site configuration')) {
        $description = t('Enter a password here to protect this @node or set a global password on <a href="@settings_page">the settings page</a>. Changing the password will prevent all the users who knew the old password from accessing this page.', array(
          '@node' => $node_type->name,
          '@settings_page' => url('admin/config/content/protected_node'),
        ));
      }
      else {
        $description = t('Enter a password here to protect this @node. Changing the password will prevent all the users who knew the old password from accessing this page.', array(
          '@node' => $node_type->name,
        ));
      }
      $form['protected_node']['protected_node_passwd'] = array(
        '#type' => 'password_confirm',
        '#description' => $description,
        '#size' => 20,
        '#after_build' => array(
          'protected_node_autocomplete_off',
        ),
      );
      break;
  }
  if (variable_get('protected_node_allow_hint', FALSE)) {
    if (isset($form['#node']->protected_node_hint)) {
      $hint = $form['#node']->protected_node_hint;
    }
    else {
      $hint = '';
    }
    $form['protected_node']['protected_node_hint'] = array(
      '#type' => 'textarea',
      '#title' => t('Password hint'),
      '#description' => t('Enter a password hint.'),
      '#default_value' => $hint,
      '#cols' => 45,
      '#rows' => 5,
    );
  }

  // Email support.
  $email_activation = variable_get('protected_node_email_activation', FALSE);
  if ($email_activation) {

    // Random password text.
    $random_password_help_text = '';
    if (variable_get('protected_node_random_password', FALSE)) {
      $random_password_help_text = '<br />' . t('<strong>Additional Note</strong>: If a password has not previously been saved for this node, you may leave the password field blank to generate a random password. In this case, you must enter at least one email address so someone knows the password.');
    }
    if (isset($form['#node']->protected_node_emails)) {
      $previous_emails = $form['#node']->protected_node_emails;
    }
    else {
      $previous_emails = '';
    }
    $form['protected_node']['protected_node_emails'] = array(
      '#type' => 'textarea',
      '#title' => t('Emails'),
      '#description' => t('Enter a list of email addresses separated by newlines and/or commas. At the time you save the page, each one of those users will be sent a notification email.') . '<br />' . t('<strong>WARNING</strong>: for an email to be sent you MUST (1) protect the node; (2) re-enter the password; (3) publish the page.') . '<br />' . t('It is necessary to re-enter the password because it is otherwise encrypted in the database.') . $random_password_help_text,
      '#default_value' => $previous_emails,
      '#cols' => variable_get('protected_node_email_box_width', '10'),
      '#rows' => variable_get('protected_node_email_box_height', '2'),
    );
  }
}