You are here

function multiple_registration_preprocess_user in Multiple Registration 8

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

Disabling of displaying fields assigned for special user roles at user page.

Parameters

array $variables: Provides theme hook variables array.

File

./multiple_registration.module, line 128
Contains multiple_registration.module.

Code

function multiple_registration_preprocess_user(array &$variables) {
  $user = $variables['elements']['#user'];

  /* @var \Drupal\user\Entity\User $user */
  $user_roles = $user
    ->getRoles();
  $fields = $user
    ->getFields();
  foreach ($fields as $field_name => $field_data) {

    /* @var Drupal\Core\Field\FieldItemList $field_data */
    $field_definition = $field_data
      ->getFieldDefinition();
    if (\property_exists($field_definition, 'third_party_settings')) {

      /* @var Drupal\field\Entity\FieldConfig $field_definition */
      $third_party_settings = $field_definition
        ->get('third_party_settings');
      if (\array_key_exists('multiple_registration', $third_party_settings)) {
        $allowed_roles = $third_party_settings['multiple_registration']['user_additional_register_form'];
        $found_roles = array_intersect($user_roles, $allowed_roles);
        if (empty($found_roles)) {
          if ($field_name !== 'user_picture') {
            unset($variables['elements'][$field_name], $variables['content'][$field_name]);
          }
        }
      }
    }
  }
}