2

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)

and...

$id = $entity["id"];

Fatal error: Cannot use object of type Entity as array

Can someone tell me what Im doing wrong?

closed as off-topic by kiamlaluno May 31 '16 at 17:30

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on programming, PHP, SQL, etc. that do not relate directly to Drupal are off-topic here, but can be asked on Stack Overflow." – kiamlaluno
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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
3

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.

  • 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.