function profile_form_user_register_form_alter in Profile 2 8
Implements hook_form_FORM_ID_alter().
Add available profile forms to the user registration form.
File
- ./
profile.module, line 152 - Support for configurable user profiles.
Code
function profile_form_user_register_form_alter(&$form, FormStateInterface $form_state) {
$attached_profile_form = FALSE;
$weight = 90;
foreach (\Drupal::configFactory()
->listAll('profile.type.') as $config_name) {
$config = \Drupal::config($config_name);
$profile_type = ProfileType::load($config
->get('id'));
$instances = array_filter(\Drupal::entityManager()
->getFieldDefinitions('profile', $config
->get('id')), function ($field_definition) {
return $field_definition instanceof FieldConfigInterface;
});
if ($profile_type->registration === TRUE && count($instances)) {
$id = $profile_type
->id();
$property = [
'profiles',
$id,
];
$profile = $form_state
->get($property);
if (empty($profile)) {
$entity = entity_create('profile', array(
'type' => $id,
'langcode' => $profile_type
->language() ? $profile_type
->language() : \Drupal::languageManager()
->getDefaultLanguage()->id,
));
// Attach profile entity form.
$form_state
->set($property, $entity);
$form_state
->set('form_display_' . $id, EntityFormDisplay::collectRenderDisplay($entity, 'default'));
$form['entity_' . $id] = array(
'#type' => 'details',
'#title' => $profile_type
->label(),
'#tree' => TRUE,
'#parents' => array(
'entity_' . $id,
),
'#weight' => ++$weight,
'#open' => TRUE,
);
$form_state
->get('form_display_' . $id)
->buildForm($entity, $form['entity_' . $id], $form_state);
$attached_profile_form = TRUE;
}
}
}
if ($attached_profile_form) {
$form['actions']['submit']['#validate'][] = 'profile_form_user_register_form_validate';
$form['actions']['submit']['#submit'][] = 'profile_form_user_register_form_submit';
}
}