You are here

public function SettingsForm::buildForm in CRM Core 8.3

Same name in this branch
  1. 8.3 src/Form/SettingsForm.php \Drupal\crm_core\Form\SettingsForm::buildForm()
  2. 8.3 modules/crm_core_user_sync/src/Form/SettingsForm.php \Drupal\crm_core_user_sync\Form\SettingsForm::buildForm()
Same name and namespace in other branches
  1. 8 modules/crm_core_user_sync/src/Form/SettingsForm.php \Drupal\crm_core_user_sync\Form\SettingsForm::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/SettingsForm.php, line 33

Class

SettingsForm
Configure crm_core_user_sync settings.

Namespace

Drupal\crm_core_user_sync\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $roles = user_roles(TRUE);
  $types = IndividualType::loadMultiple();
  $config = $this
    ->config('crm_core_user_sync.settings');
  $rules = $config
    ->get('rules');
  uasort($rules, [
    $this,
    'weightCmp',
  ]);
  $form['description'] = [
    '#plain_text' => $this
      ->t('CRM User Synchronization can automatically create contact records associated with user accounts under certain conditions.'),
  ];
  $form['auto_sync_user_create'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Automatically create an associated contact when account is created'),
    '#description' => $this
      ->t('When checked, this checkbox will automatically create new contacts when a new user account is created according to rules listed above. Rules will be processed in order until a new contact is created.'),
    '#default_value' => $config
      ->get('auto_sync_user_create'),
  ];
  $form['auto_sync_user_relate'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Look for existing contact when auto creating contacts.'),
    '#description' => $this
      ->t('When checked, creating accounts will not create duplicates if one existing account is found based on the email address.'),
    '#default_value' => $config
      ->get('auto_sync_user_relate'),
  ];
  $form['contact_load'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Load contact related to the current user'),
    '#description' => $this
      ->t('When checked, contact related to the current user will be loaded as part of the user account object in $account->crm_core["contact"]. In certain situations,  loading contact data as part of a user entity can create performance issues (for instance, when there are hundreds of fields associated with each contact). Uncheck this box if it is creating problems with performance.'),
    '#default_value' => $config
      ->get('contact_load'),
  ];
  $form['contact_show'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Show contact information'),
    '#description' => $this
      ->t('When checked, contact related to the current user will be shown on user profile page. Configurable from "Manage display" page.'),
    '#default_value' => $config
      ->get('contact_show'),
  ];
  $form['rules'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Role'),
      $this
        ->t('Contact type'),
      $this
        ->t('Status'),
      $this
        ->t('Operations'),
      $this
        ->t('Weight'),
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'rule-weight',
      ],
    ],
    '#empty' => $this
      ->t('No rules configured'),
  ];
  foreach ($rules as $key => $rule) {
    $row = [];
    $row['#attributes']['class'][] = 'draggable';
    $row['#weight'] = $rule['weight'];
    $row['role'] = [
      '#plain_text' => $roles[$rule['role']]
        ->label(),
    ];
    $row['contact_type'] = [
      '#plain_text' => $types[$rule['contact_type']]
        ->label(),
    ];
    $row['enabled'] = [
      '#plain_text' => $rule['enabled'] ? 'Enabled' : 'Disabled',
    ];
    $row['operations'] = [
      '#type' => 'operations',
      '#links' => [],
    ];
    $row['weight'] = [
      '#type' => 'weight',
      '#title_display' => 'invisible',
      '#default_value' => $rule['weight'],
      '#attributes' => [
        'class' => [
          'rule-weight',
        ],
      ],
    ];
    $links =& $row['operations']['#links'];
    $links['edit'] = [
      'title' => 'Edit',
      'url' => Url::fromRoute('crm_core_user_sync.rule.edit', [
        'rule_key' => $key,
      ]),
    ];
    $links['delete'] = [
      'title' => 'Delete',
      'url' => Url::fromRoute('crm_core_user_sync.rule.delete', [
        'rule_key' => $key,
      ]),
    ];
    if ($rule['enabled']) {
      $links['disable'] = [
        'title' => 'Disable',
        'url' => Url::fromRoute('crm_core_user_sync.rule.disable', [
          'rule_key' => $key,
        ]),
      ];
    }
    else {
      $links['enable'] = [
        'title' => 'Enable',
        'url' => Url::fromRoute('crm_core_user_sync.rule.enable', [
          'rule_key' => $key,
        ]),
      ];
    }
    $form['rules'][$key] = $row;
  }
  $form['crm_core_user_sync_wrapper'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Sync Current Users'),
  ];
  $form['crm_core_user_sync_wrapper']['user_sync'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Synchronize Users'),
    '#submit' => [
      '::bulkUserSync',
    ],
  ];
  $form['crm_core_user_sync_wrapper']['description'] = [
    '#type' => 'item',
    '#markup' => $this
      ->t('Click this button to apply user synchronization rules to all user accounts that are currently not associated with a contact in the system. It will create an associated contact record for each user according to the rules configured above. Warning: this cannot be undone.'),
  ];
  return parent::buildForm($form, $form_state);
}