proper way to access a field value in drupal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.