Set [‘#default_value’] of taxonomy time period reference area by way of hook_form_alter()?

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?

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Set [‘#default_value’] of taxonomy time period reference area by way of hook_form_alter()?

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.