You are here

public function RuleForm::buildForm in CRM Core 8

Same name and namespace in other branches
  1. 8.3 modules/crm_core_user_sync/src/Form/RuleForm.php \Drupal\crm_core_user_sync\Form\RuleForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

modules/crm_core_user_sync/src/Form/RuleForm.php, line 31

Class

RuleForm
Configure CRM Core User Synchronization settings for this site.

Namespace

Drupal\crm_core_user_sync\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $rule_key = 'new') {
  $config = $this
    ->config('crm_core_user_sync.settings');
  $rules = $config
    ->get('rules');
  if ($rule_key === 'new') {
    $rule = [
      'role' => '',
      'contact_type' => '',
      'enabled' => TRUE,
      'weight' => 0,
    ];
    $form_state
      ->set('rule_key', 'new');
  }
  else {
    if (isset($rules[$rule_key])) {
      $rule = $rules[$rule_key];
      $form_state
        ->set('rule', $rule);
      $form_state
        ->set('rule_key', $rule_key);
    }
    else {
      $rule = [
        'role' => '',
        'contact_type' => '',
        'enabled' => TRUE,
        'weight' => 0,
      ];
      $form_state
        ->set('rule_key', 'new');
    }
  }
  $types_options = [
    '' => $this
      ->t('- Select -'),
  ];
  foreach (IndividualType::loadMultiple() as $type) {
    $types_options[$type
      ->id()] = $type
      ->label();
  }
  $role_options = [
    '' => $this
      ->t('- Select -'),
  ];
  foreach (user_roles(TRUE) as $role) {
    $role_options[$role
      ->id()] = $role
      ->label();
  }
  $form['role'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('User Role'),
    '#options' => $role_options,
    '#default_value' => isset($rule['role']) ? $rule['role'] : '',
    '#required' => TRUE,
  ];
  $form['contact_type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Contact Type'),
    '#options' => $types_options,
    '#default_value' => isset($rule['contact_type']) ? $rule['contact_type'] : '',
    '#required' => TRUE,
  ];
  $form['weight'] = [
    '#type' => 'weight',
    '#title' => $this
      ->t('Weight'),
    '#default_value' => isset($rule['weight']) ? $rule['weight'] : 0,
  ];
  $form['enabled'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enabled'),
    '#default_value' => isset($rule['enabled']) ? $rule['enabled'] : TRUE,
    '#description' => $this
      ->t('When checked, this rule will be used to synchronize user accounts. When unchecked, it will be ignored throughout the system.'),
  ];
  return parent::buildForm($form, $form_state);
}