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:
Devel results:
How can I get the field value and pass it to my jsblock module?