0

I developed a custom module in Drupal 7. I asked a similar question here: Drupal 7 Get field value by machine name and pass to javascript

But the difference in this question is, instead of a taxonomy term, I want to get the field value of a regular text field.

Here is the code that I use to get the field value of a taxonomy term to pass to javascript, which currently resides in my theme's hook_process_page function:

if ($node = menu_get_object('node')) {
  $tid = $node->field_test['und'][0]['tid'];
  $term = taxonomy_term_load($tid);
  drupal_add_js(array('mymodule' => array('test' => $term->name)), 'setting');
}

Some things that I can see might ned to be changed right away are "tid," "taxonomy_term_load."

How can I change this code to instead get the text value of a field instead of a taxonomy term, to pass to mymodule?

EDIT:

Per a suggested change, I've modified the code, though I'm not sure this is what was meant:

if ($node = menu_get_object('node')) {
  $fieldValue = $node->field_test['und'][0]['value'];
  $term = taxonomy_term_load($fieldValue);
  drupal_add_js(array('jsblock' => array('test' => $fieldValue->name)), 'setting');
}

The in response to Drupal.settings.jsblock.test, the console outputs null.

Here is the Drupal notice:

property of non-object

Devel results:

enter image description here

How can I get the field value and pass it to my jsblock module?

0

The suggestion you wrote for your code

$fieldValue = $node->field_test['und'][0]['value'];  
$term = taxonomy_term_load($fieldValue);  

If you require only value then your purpose gets solved at this line of code

$fieldValue = $node->field_test['und'][0]['value'];

Secondly the function taxonomy_term_load() expects tid as parameter and you are sending a value string, hence it will throw an error. See the API document. https://api.drupal.org/api/drupal/modules!taxonomy!taxonomy.module/function/taxonomy_term_load/7.x

You should just send the variable to javascript file as this

drupal_add_js(array('jsblock' => array('test' => $fieldValue)), 'setting');
0

For text fields you can use

$value = $node->field_my_field_name['und'][0]['value'];

or, if you want sanitized value, use ['safe_value'].

  • thanks for the answer! I updated my code in post post after "EDIT:" I'm still getting some errors. I'm not sure if I did what you meant. Any suggestions are appreciated. – lane Dec 17 '17 at 21:02
  • in your modified/repurposed code, you no longer need $term = taxonomy_term_load($fieldValue);, and the error is a result of calling the "name" value of your variable, rather than the variable itself array('test' => $fieldValue) – Anson W Han Dec 17 '17 at 21:22
  • Thank you to you also @Anson for clarifying the drupal_add_js function! – lane Dec 17 '17 at 23:46
  • 2
    Note: Using ['und'] will not work if you enable translation for that field. See Best practice for language key for “und” in hook_form_alter – No Sssweat Dec 18 '17 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.