This may be a rookie question, but the docs are quite broad, and im looking for a simple answer.

I am using dpm($entity) to view the entity object, and it gives me the following...

enter image description here

Now I want to access these values in a computed field, I have tried...

$id = $entity=>id;

Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW)


$id = $entity["id"];

Fatal error: Cannot use object of type Entity as array

Can someone tell me what Im doing wrong?

  • It's $entity->id – Alexei Rayu May 31 '16 at 10:38
  • This is a plain PHP question. $entity is an object, and PHP has only a way to access its fields. Drupal doesn't change PHP syntax. – kiamlaluno May 31 '16 at 17:31

You use the -> operator for accessing the data attributes in a PHP object.

Use $entity->id to access the id of the entity.

If you want to access a value of the field (which is an array), you do it like
$entity->field_field_name['und'][0]['value'] depending on the type of field.

| improve this answer | |
  • Maybe you want to also briefly add to that what the "0" stands for? And/or the "und"? – Pierre.Vriens Sep 2 '16 at 9:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.