You are here

function multiple_registration_form_alter in Multiple Registration 8

Same name and namespace in other branches
  1. 8.2 multiple_registration.module \multiple_registration_form_alter()
  2. 3.x multiple_registration.module \multiple_registration_form_alter()

Implements hook_form_alter().

File

./multiple_registration.module, line 172
Contains multiple_registration.module.

Code

function multiple_registration_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $storage = $form_state
    ->getStorage();
  if (!empty($storage['form_display']) && is_object($storage['form_display'])) {
    $form_display = $storage['form_display'];
    if ($form_display instanceof EntityFormDisplay && $form_display
      ->getTargetEntityTypeId() === 'user') {

      // Show only appropriate fields.
      $field_definitions = \Drupal::service('entity_field.manager')
        ->getFieldDefinitions('user', 'user');
      foreach ($field_definitions as $field_name => $field_definition) {
        if (isset($form[$field_name]) && $field_definition instanceof ThirdPartySettingsInterface) {
          $field_roles = $field_definition
            ->getThirdPartySetting('multiple_registration', 'user_additional_register_form');
          if (!empty($field_roles)) {

            // If something was selected.
            if (!(max($field_roles) === 0)) {
              $form[$field_name]['#access'] = MultipleRegistrationController::checkFieldAccess($field_roles);
            }
          }
        }
      }
    }
  }
}