Skip to content

Instantly share code, notes, and snippets.

@Greg-Boggs
Created October 23, 2013 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Greg-Boggs/7128108 to your computer and use it in GitHub Desktop.
Save Greg-Boggs/7128108 to your computer and use it in GitHub Desktop.
proper way to access a field value in drupal
<?php
// 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');
}
?>
@randomletters
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