Skip to content

Instantly share code, notes, and snippets.

Created October 23, 2013 22:39
What would you like to do?
proper way to access a field value in drupal
// Check if the value of a field is equal to the $pathway variable.
$pathway = 'category 1';
try {
if (arg(0) == 'node' && is_numeric(arg(1))) {
$wrapper = entity_metadata_wrapper('node', arg(1));
if ($wrapper->field_FIELD_NAME->value()->name != $pathway) {
return TRUE;
} catch (Exception $e) {
drupal_set_message($e->getMessage(), 'error');
Copy link

The down side to this approach is that once an error occurs, none of the other subsequent lines will be run. Therefore, if "field_name" does not exist but "field_date" does, none of the variables you intended to set will be set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment