You are here

function uc_role_form_user_profile_form_alter in Ubercart 8.4

Implements hook_form_user_profile_form_alter().

File

uc_role/uc_role.module, line 114
Grants roles upon accepted payment of products.

Code

function uc_role_form_user_profile_form_alter(&$form, FormStateInterface $form_state) {
  if (!\Drupal::currentUser()
    ->hasPermission('administer users')) {
    return;
  }
  $roles_config = \Drupal::config('uc_role.settings');
  $account = $form_state
    ->getFormObject()
    ->getEntity();
  $role_choices = _uc_role_get_choices(array_keys($account
    ->getRoles()));
  $polarity_widget = [
    '#type' => 'select',
    '#options' => [
      'add' => '+',
      'remove' => '-',
    ],
  ];
  $quantity_widget = [
    '#type' => 'textfield',
    '#size' => 4,
    '#maxlength' => 4,
  ];
  $granularity_widget = [
    '#type' => 'select',
    '#options' => [
      'day' => t('day(s)'),
      'week' => t('week(s)'),
      'month' => t('month(s)'),
      'year' => t('year(s)'),
    ],
  ];
  $form['uc_role'] = [
    '#type' => 'details',
    '#title' => t('Ubercart roles'),
    '#weight' => 10,
    '#theme' => 'uc_role_user_new',
  ];
  $form['uc_role']['expirations'] = [
    '#type' => 'fieldset',
    '#title' => t('Pending expirations'),
    '#weight' => 0,
    '#theme' => 'uc_role_user_expiration',
  ];
  $form['uc_role']['expirations']['table']['#tree'] = TRUE;

  // Create the expirations table.
  $connection = \Drupal::database();
  $expirations = $connection
    ->query('SELECT * FROM {uc_roles_expirations} WHERE uid = :uid', [
    ':uid' => $account
      ->id(),
  ]);
  foreach ($expirations as $expiration) {
    $form['uc_role']['expirations']['table'][$expiration->rid] = [
      'name' => [
        '#type' => 'value',
        '#value' => _uc_role_get_name($expiration->rid),
      ],
      'remove' => [
        '#type' => 'checkbox',
      ],
      'expiration' => [
        '#type' => 'value',
        '#value' => $expiration->expiration,
      ],
      'polarity' => $polarity_widget,
      'qty' => $quantity_widget,
      'granularity' => $granularity_widget,
    ];
  }

  // Option to allow temporary roles.
  if (!empty($role_choices)) {
    $form['uc_role']['new_role'] = [
      '#type' => 'checkbox',
      '#title' => t('Add role'),
    ];
    $form['uc_role']['new_role_add'] = [
      '#type' => 'select',
      '#default_value' => $roles_config
        ->get('default_role'),
      '#options' => $role_choices,
    ];
    $form['uc_role']['new_role_add_for'] = [
      '#markup' => ' ' . t('for') . ' ',
    ];
    $form['uc_role']['new_role_add_qty'] = $quantity_widget;
    $form['uc_role']['new_role_add_granularity'] = $granularity_widget;
    if (($default_granularity = $roles_config
      ->get('default_granularity')) != 'never') {
      $form['uc_role']['new_role_add_qty'] = $form['uc_role']['new_role_add_qty'] + [
        '#default_value' => $roles_config
          ->get('default_length'),
      ];
      $form['uc_role']['new_role_add_granularity'] = $form['uc_role']['new_role_add_granularity'] + [
        '#default_value' => $default_granularity,
      ];
    }
  }
  $form['#validate'][] = 'uc_role_user_validate';
  return $form;
}