You are here

function role_expire_add_expiration_input in Role Expire 7

Same name and namespace in other branches
  1. 8 role_expire.module \role_expire_add_expiration_input()
  2. 6 role_expire.module \role_expire_add_expiration_input()
  3. 2.x role_expire.module \role_expire_add_expiration_input()

Add form elements to the given form that accept the role expiration times.

Parameters

array $form: The user edit form to which role expire input elements will be added.

3 calls to role_expire_add_expiration_input()
role_expire_form_context_admin_user_create_menu_render_form_alter in ./role_expire.module
Implements hook_form_FORM-ID_alter().
role_expire_form_user_profile_form_alter in ./role_expire.module
Implements hook_form_FORM-ID_alter().
role_expire_form_user_register_form_alter in ./role_expire.module
Implements hook_form_FORM-ID_alter().

File

./role_expire.module, line 596
Role Expire module

Code

function role_expire_add_expiration_input(array &$form) {

  // Role delegation module adds a separate "roles_change" array to manage
  // access. This is merged into the "roles" array prior to saving.
  $roles_key = module_exists('role_delegation') && !user_access('administer permissions') ? 'roles_change' : 'roles';

  // Add expiration fields to each checkbox, based on permissions.
  if ((user_access('administer users') || user_access('administer role expire')) && !empty($form['account'][$roles_key])) {

    // Get $uid of currently viewed user.
    $uid = !empty($form['#user']) && is_object($form['#user']) && !empty($form['#user']->uid) ? $form['#user']->uid : NULL;

    // Attach CSS class and JavaScript file to the form field.
    $form['account'][$roles_key]['#attributes']['class'][] = 'role-expire-roles';
    $form['account'][$roles_key]['#attached']['js'][] = drupal_get_path('module', 'role_expire') . '/role_expire.js';

    // Loop through each roles checkbox and add an expiration field.
    foreach ($form['account'][$roles_key]['#options'] as $rid => $role) {
      $expiry_timestamp = $uid !== NULL ? role_expire_get_user_role_expiry_time($uid, $rid) : '';
      $form['role_expire_' . $rid] = array(
        '#title' => t("%role role expiration date/time", array(
          '%role' => drupal_ucfirst($role),
        )),
        '#type' => 'textfield',
        '#default_value' => !empty($expiry_timestamp) ? date("Y-m-d H:i:s", $expiry_timestamp) : '',
        '#attributes' => array(
          'class' => array(
            'role-expire-role-expiry',
          ),
        ),
        '#description' => t("Leave blank for default role expiry (never, or the duration you have set for the role), enter date and time in format: <em>yyyy-mm-dd hh:mm:ss</em> or use relative time i.e. 1 day, 2 months, 1 year, 3 years."),
      );
    }
    $form['#validate'][] = '_role_expire_validate_role_expires';
  }
}