EntityMetadataWrapperException: unknown data property for field

I recently tried updating my code to use object wrappers to access field values. Now I have this:

$wrapper = entity_metadata_wrapper("node", $nid); print($wrapper->field_property_sample()->value()); 

instead of this:

 print($node->field_property_sample[LANGUAGE_NONE][0]["value"]); 

Sometimes I run into a problem:

EntityMetadataWrapperException: unknown data property field_property_sample.

Is there any way around this?

I have about 10 of these fields that can throw this exception, and it really gets ugly

 $wrapper = entity_metadata_wrapper("node", $nid); try { print($wrapper->field_property_sample()->value()); } catch (EntityMetadataWrapperException &e){ print(""); } /** repeat 10 times **/ 

Is there any function that I can more or less call it that?

 $wrapper = entity_metadata_wrapper("node", $nid); print($wrapper->field_property_sample->exists() ? $wrapper->field_property_sample->value() : "" ); /** repeat 10 times **/ 
+4
source share
4 answers

Yes, you can just use existing PHP language features

 try { print($wrapper->field_property_sample->value()); } catch (EntityMetadataWrapperException $e) { // Recover } 

Or, since EntityMetadataWrapper implements __isset() , you can use this:

 print isset($wrapper->field_property_sample) ? $wrapper->field_property_sample->value() : ''; 
+6
source

As for Clive's answer, you can use __isset() as follows:

 print ($wrapper->__isset('field_property_sample') ? $wrapper->field_property_sample->value() : ''; 
+4
source

In nested field collections:

When repeating the list of field collection lists and checking the nonempty field collection nested in the first, isset () does not work. However, I found that the check:

  foreach ($node_wrapper->field_fc_one AS $field_collection) { // Grab a nested field collection, properly wrapped. $nested_fc_wrapper = $field_collection->field_nested_fc; // isset() or $wrapper->__isset('') do not work here, but this does: if(nested_fc_wrapper->getIdentifier()) { // Do some stuff } } 
+1
source

Using field_property_sample() does not make sense because:

  • $wrapper->field_property_sample() used to call the class method
  • $wrapper->field_property_sample used to get the value of a class property

A property is a variable that you want to use, a class method is a function that you want to call.

So using:

 $wrapper->field_property_sample->value(); 

- The correct syntax.

To use Entity metadata wrappers correctly, check: the object metadata wrapper page .

Here is a sample code:

 try { $wrapper = entity_metadata_wrapper('node', $node); $wrapper->field_property_sample = 'some data'; $wrapper->field_multi_sample = array('1st', '2nd'); $wrapper->save(); } catch (EntityMetadataWrapperException $e) { watchdog_exception('my_module', $e); } 

To print, use:

 print($wrapper->field_property_sample->value()); 

or dpm() , dd() from the Devel module.

0
source

Source: https://habr.com/ru/post/1497943/


All Articles