Here is a unique query with Drupal Developer identical taxonomy time period reference area I discussed in my earlier query.
Customers with Administrator and Coach roles are granted permission to register Scholar customers by way of a customized "Add scholar" kind mode (user_add_student_form). It will be higher for UX if I may set Drupal Developer ['#default_value'] of Drupal Developer select_a_school taxonomy time period reference area to Drupal Developer identical college that Drupal Developer at present logged in consumer belongs to.
I am working with hook_form_alter() in template preprocess as a result of that is how Drupal Developer earlier developer set it up:
/** * Implements hook_form_alter(). */ perform projectname_form_alter(&$kind, &$form_state, $form_id) { // Get Drupal Developer present Person. $current_user_proxy = Drupal::currentUser(); $current_user_id = $current_user_proxy->id(); if (!$current_user_proxy->isAnonymous()) { $currentUser = Person::load($current_user_id); // Get Drupal Developer present Person's college. $currentSchool = $currentUser->field_select_a_school->getValue(); // Deal with Add Scholar kind. if ($form_id == 'user_add_student_form') { // This works. $kind['account']['roles']['#access'] = FALSE; $kind['account']['roles']['#default_value'] = [ 0 => 'authenticated', 1 => 'student', ]; // This doesn't work. $kind['field_select_a_school']['#default_value'] = $currentSchool; }
Setting $kind['account']['roles']['#default_value'] works as anticipated, however Drupal Developer taxonomy time period reference area resists all such makes an attempt.
I’ve tried Drupal Developer following variations:
$kind['field_select_a_school']['#default_value'] = $currentSchool; $kind['field_select_a_school']['#default_value'][] = $currentSchool; $kind['field_select_a_school']['widget']['#default_value'] = $currentSchool; $kind['field_select_a_school']['widget']['#default_value'][] = $currentSchool; dump($currentSchool);
Drupal Developer Symfony VarDumper dump() of $currentSchool appears to be like prefer it accommodates Drupal Developer right knowledge construction:
^ array:1 [▼ 0 => array:1 [▼ "target_id" => "916" ] ]
Right here, for reference, is Drupal Developer corresponding dump() for Drupal Developer goal area in $kind:
"field_select_a_school" => array:8 [▼ "#type" => "container" "#parents" => array:1 [▶] "#attributes" => array:1 [▶] "widget" => array:22 [▼ "#title" => "Select A School*" "#description" => "" "#field_parents" => [] "#required" => true "#delta" => 0 "#weight" => 0 "#element_validate" => array:1 [▶] "#key_column" => "target_id" "#kind" => "choose" "#choices" => array:47 [▶] "#default_value" => [] "#a number of" => false "#shs" => array:7 [▶] "#attributes" => array:1 [▶] "#connected" => array:1 [▶] "#entity_type" => "consumer" "#bundle" => "consumer" "#after_build" => array:1 [▶] "#field_name" => "field_select_a_school" "#mother and father" => array:1 [▶] "#tree" => true "#form_id" => "user_add_student_form" ] "#entry" => true "#weight" => 22 "#cache" => array:3 [▶] "#form_id" => "user_add_student_form" ]
It doesn’t matter what I strive, $kind['field_select_a_school']['widget']['#default_value'] at all times finally ends up containing an empty array. ¯_(ツ)_/¯
What am I lacking right here?