How to retrieve a select box option and set the field’s value to it?

I have a a taxonomy term select box (called “product_keyword”) in a node that gets its options populated via the Reference Field Option Limit module. There are 3 other taxonomy term reference fields in the node that are responsible for limiting the “product_keyword” option list all the way down to a single option, and this part is working beautifully.

The “product_keyword” field is set to Required, so when you actually go in to edit the node, the “product_keyword” field appears to already have this one option selected. However, with the use of the Devel module, I have discovered that this is not the case. There is no #value for the “product_keyword.” I have created a custom module (my very first one ever, in fact) to set this #value, but I am having trouble determining how to choose the right properties to set this. This response by Clive is what I’m trying to wrap my brain around.

My module code is using a hook_form_alter function that involves the use of #after_build:

    //Declare the callback for after_build for page node form function set_keywords_form_alter(&$form, &$form_state, $form_id) {   if ($form_id == 'part_node_form') {     $form['#after_build'][] = 'set_keywords_after_build';   } }  function set_keywords_after_build($form, &$form_state) { 

The overall goal is to simply allow this field to be displayed in a view, but one complication is that these 7000+ nodes are being imported via the Feeds module, and the clients should not have to edit each node manually just to hit the Save button (which is the only way the problem can be solved now – VBO can’t do it, nor can the Resave Nodes module). I’m not even sure if #after_build should be involved, and I am also ok with setting the field’s #default_value instead of or in addition to the #value.

EDIT:

In the page array dropdown, for the keyword’s select list options:

#options (Array, 1 element) 0 (String, 13 characters ) Inker Parts  

But under ‘#value,” I’m just getting:

#value (String, 8 characters ) - None -  

Under another reference field, “Product Family,” where things are functioning normally, I see:

#options (Array, 40 elements)  _none (String, 8 characters ) - None -  1401 (String, 5 characters ) AUTOZ 

…….and so on

and for the #value:

    0 (String, 2 characters ) 1401            $...['content']['system_main']['field_product_family']['und']['#value'][0]  

In this example, “1401” is the tid, and “AUTOZ” is the term name.

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

How to retrieve a select box option and set the field’s value to it?

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.