51

I'm starting to use this form to access data in my module. (See comment #1.)

$node->field_test[$node->language][0]['value']

I thought that looked like a pretty good solution, but further down that same question I found this:

Don't assume that 'und' is for fields of entities without language, it is also for fields that are not translatable, and without the entity translation module that is all fields. Moreover, there are differences in this between the various 7.x versions of Drupal.
Better to use the field_get_items() function which will sort out for you under what language code the data is stored.

And now I don't know if what I'm using could break something down the road.

39

I find that using the Entity API module is a great help and also makes code more readable. The above code will not always work, since the language of the node and the language of the field can be different.

With the entity API module, and its wrapper, you can use the following code:

 $node_wrapper = entity_metadata_wrapper('node', $node);
 $field_val = $node_wrapper->field_test->value();

This should be bullet proof. One thing about using the entity module is that if you try to access a field that doesn't exist, you will get a nasty error and an exception thrown instead of a notice and wrong behavior.

To avoid this, you could do try/catch like this

try {
  $field_val = $node_wrapper->field_doesnt_exist->value();
} catch (EntityMetadataWrapperException $e) {
  $field_val = 'default/fallback value';
}

Or you can use isset() which EntityMetadataWrapper handles internally:

$field_val = 'default/fallback value';
if (isset($node_wrapper->field_doesnt_exist)) {
  $field_val = $node_wrapper->field_doesnt_exist->value();
}
  • Has this function entity_metadata_wrapper() been deprecated? I tried calling this in my module, and I got Fatal error: Call to undefined function entity_metadata_wrapper() - I also did a source search on my Drupal 7.12 installation in Dreamweaver, and came up with 0 results anywhere else in the code! – Aditya M P Apr 11 '12 at 12:41
  • 1
    aditya - this is in the Entity API module - not in core. – lazysoundsystem Apr 16 '12 at 10:44
  • 1
    @adityamenon Like lazy says this is not in core... yet, it probably will be for Drupal 8. The entity APIs will at least be improve greatly. There wasn't really time do create all the APIs needed for the entity system for Drupal 7, so that is what the entity API module tries to achieve. – googletorp Apr 16 '12 at 18:37
  • Thank you guys, I was stupid not to read the answer properly and follow the link to the Entity API project page :) – Aditya M P Apr 19 '12 at 5:57
  • 1
    When I look over the source code for entity_metadata_wrapper and follow the rabbit trail through all of the classes that are instantiated and extended to give me easier field manipulation, I wonder if it's all worth it. Adding another 3k+ lines of code to my bootstrap and taking up more memory to deal with all of the variable assignments... is there anything more lightweight? It just seems that $node->field_name[LANGUAGE_NONE][0]['value'] = 'foo'; is really the most efficient way. – Charlie Schliesser Jun 1 '12 at 2:46
19

For reading, you should always be able to use field_get_items(), which will pick the correct language for you and also check if the field has any values.

Unfortunately, the field API is very limited in 7.x, there is no way to get e.g. the first field item, don't even dare to ask about getting the value with a single function call... And there is no field_set_items() counterpart.

So yes, entity API module does provide a nicer API with the drawback that it also comes with quite a bit of overhead (It basically converts every single value to wrapper objects which have tons of nested property info arrays attached to them). Trying to dump an entity wrapper will usually get you either nothing or a wall of unreadable arrays.

  • 1
    I get the impression from what you're saying that this sort of thing miight be improved in Drupal 8? If so how does one go about finding out how these sorts of things are progressing? Aside from the module pages D.o is like a maze to me! :) – Clive Mar 6 '12 at 20:00
  • 1
    Well, there is always hope :) And it's hard to keep even a high level overview of what's going on in Drupal 8, one way is to follow the iniatives. This isn't a direct target of the existing directives, though. Parts of the entity API module are moved/being moved into core (There is Entity class now and existing entities are converted to the new system). So there is a chance that we will e.g. get methods directly on those classes to deal with fields. For commited changes, a good resource is the new change record system, see drupal.org/list-changes/drupal. – Berdir Mar 6 '12 at 20:20
  • Brilliant, that's exactly the sort of thing I was looking for thanks very much! :) Hope you don't mind my asking I know it's not really on-topic for the site...I'd love to get involved in contributing to core but having never really been involved in open-source I find the whole thing a bit daunting...it's nice to have a good place to start :) – Clive Mar 6 '12 at 20:26
  • Clive: Check out drupalofficehours.org - it's precisely to help people get started with contributing. More resources there. I'd also recommend going to a user group meeting that core devs frequent or more likely a DrupalCamp where a core dev or two will be. Find your local group on groups.drupal.org and you should be able to work it out. drupical.org can be useful too. – wizonesolutions Nov 6 '12 at 21:24
-2
$node = node_load($lot_id);
$field_language = field_language('node', $node, 'field_name');
$node->field_name[$field_language][0]['value'] = $custom_value;
  • Can you add a short explanation to why this answers the question? – Free Radical Jun 4 '14 at 13:22

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.