You are here

function multiple_registration_field_widget_form_alter in Multiple Registration 8

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

Implements hook_field_widget_form_alter().

File

./multiple_registration.module, line 197
Contains multiple_registration.module.

Code

function multiple_registration_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
  $field_definition = $context['items']
    ->getFieldDefinition();
  if ($field_definition
    ->getTargetEntityTypeId() === 'user' && $field_definition instanceof ThirdPartySettingsInterface) {
    $field_roles = $field_definition
      ->getThirdPartySetting('multiple_registration', 'user_additional_register_form') ?? [];
    $field_roles_required = $field_definition
      ->getThirdPartySetting('multiple_registration', 'user_additional_register_form_required') ?? [];

    // If something was selected.
    if ($field_roles !== NULL && max($field_roles) !== 0) {
      $element['#access'] = MultipleRegistrationController::checkFieldAccess($field_roles);
    }
    if (!$element['#required'] && !empty($field_roles_required)) {

      // If something was selected.
      if (max($field_roles_required) !== 0) {
        $required = MultipleRegistrationController::checkFieldAccess($field_roles_required);
        $element['#required'] = $required;
        foreach (Element::children($element) as $child) {
          $element[$child]['#required'] = $required;
        }
      }
    }
  }
}