You are here

webform_password_field.module in WebformPasswordField 7

File

webform_password_field.module
View source
<?php

/*
 * Implements hook_webform_component_info().
 */
function webform_password_field_webform_component_info() {
  $component = array();
  $component['password'] = array(
    'type' => 'textfield',
    'label' => t('Password field'),
    'description' => t('Basic password field'),
    'features' => array(
      // Add content to CSV downloads. Defaults to TRUE.
      'csv' => TRUE,
      // Show this field in e-mailed submissions. Defaults to TRUE.
      'email' => TRUE,
      // Allow this field to be used as an e-mail FROM or TO address. Defaults
      // to FALSE.
      'email_address' => FALSE,
      // Allow this field to be used as an e-mail SUBJECT or FROM name. Defaults
      // to FALSE.
      'email_name' => TRUE,
      // This field may be toggled as required or not. Defaults to TRUE.
      'required' => TRUE,
      // If this field can be used as a conditional SOURCE. All fields may
      // always be displayed conditionally, regardless of this setting.
      // Defaults to TRUE.
      'conditional' => TRUE,
      // If this field allows other fields to be grouped within it (like a
      // fieldset or tabs). Defaults to FALSE.
      'group' => FALSE,
      // If this field saves a file that can be used as an e-mail attachment.
      // Defaults to FALSE.
      'attachment' => FALSE,
    ),
    'file' => 'password.inc',
  );
  return $component;
}

/**
 * Implements hook_webform_validation_validators().
 */
function webform_password_field_webform_validation_validators() {
  return array(
    'password' => array(
      'name' => "Compare password values",
      'component_types' => array(
        'password',
      ),
      'min_components' => 2,
      'max_components' => 2,
      'description' => t('Verifies that two password field values are equal.'),
    ),
  );
}

/**
 * Implements hook_webform_validation_validate().
 */
function webform_password_field_webform_validation_validate($validator_name, $items, $components, $rule) {
  if ($items) {
    switch ($validator_name) {
      case 'password':
        $errors = array();
        $first_entry_key = key($items);
        $first_entry = array_shift($items);
        foreach ($items as $key => $val) {
          if ($val !== $first_entry) {
            $errors[$key] = t('%item_checked does not match %item_first.', array(
              '%item_checked' => $components[$key]['name'],
              '%item_first' => $components[$first_entry_key]['name'],
            ));
          }
        }
        return $errors;
        break;
    }
  }
}

/**
 * Implements hook_form_alter().
 */
function webform_password_field_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['type']['#value']) && $form['type']['#value'] == 'password') {
    $form['wpf_placeholder'] = array(
      '#type' => 'textfield',
      '#title' => t('Placeholder'),
      '#description' => t('Enter placeholder for your password field'),
      '#default_value' => variable_get('wpf_placeholder', t('Password')),
    );
  }
  if (strstr($form_id, 'webform_client_form')) {
    if (in_array('password', $form)) {
      $form_key = variable_get('wpf_form_key', t('Password'));
      $form['submitted'][$form_key]['#attributes'] = array(
        'placeholder' => variable_get('wpf_placeholder', t('Password')),
      );
    }
  }
}

/**
 * Implements hook_webform_component_insert().
 */
function webform_password_field_webform_component_insert($component) {
  if ($component['type'] == 'password') {
    $cid = $component['cid'];
    $form_key = $component['form_key'];
    variable_set('wpf_cid', $cid);
    variable_set('wpf_form_key', $form_key);
  }
}

/**
 * Implements hook_webform_component_delete().
 */
function webform_password_field_webform_component_delete($component) {
  variable_del('wpf_cid');
  variable_del('wpf_form_key');
  variable_del('wpf_placeholder');
}