2

There are many different ways to get a field value from an entity. For example, a text field:

$entity->get('title')->value;
$entity->get('title')->get(0)->get('value')->getValue();
$entity->get('title')->get(0)->getValue()['value'];
$entity->get('title')->first()->get('value')->getValue();
$entity->get('title')->first()->getValue()['value'];

And so on.

And then, depending on the entity, for the entity title (or name or label), you can sometimes do...

$entity->getName();

What is the best practice? The first expression above (using the "value" property) is most concise -- are there any drawbacks to using it?

Is there any reason to prefer the first() helper instead of get(0)? Why does the first() helper exist? Are there cases when the first value in a field list doesn't have a 0 index?

4
  • You can do even shorter $entity->field_name->value;. This always returns 1st value.
    – No Sssweat
    Mar 15, 2020 at 2:17
  • When you create a list text, you can create your own keys. So 0 isn't always a key.
    – No Sssweat
    Mar 15, 2020 at 2:22
  • 1
    @NoSssweat, field items are always indexed starting at 0. The key of a list text is stored in the field value.
    – 4uk4
    Mar 15, 2020 at 9:13
  • I find that $entity->get('field_name')->getValue() works well for me, particularly in working with Paragraphs.
    – Beau
    Mar 18, 2020 at 5:37

1 Answer 1

2

There's likely no fast rule-of-thumb here because you're describing a few different things that will greatly depend on context (what entity, what fields?).

First, let's disregard ::getName(); it's a Taxonomy specific-wrapper around ::label() (whatever the entity has defined as its readable label in the entity definition it will return it). There's no such things a multiple labels so let's get to the main question being asked here: best practice on pulling field values.

It really depends on what you're trying to do.

If you look into ::get() method on the entity, you'll see its basically pulling data out of the internal $fields property which is is an array of objects that implement FieldItemListInterface. Classes that implement the interface supply the various get(), first(), getValue() methods that you can use when you're dealing with fields that may have one, none, or many values. They do extra overhead of checking for the existence of data or making sure it exists.

If you know for sure the data exists in your class you can skip the chaining of methods/properties and just get to the directly (e.g. $node->title->value), if not, these methods exists when you're dealing with uncertain structure in your entity/fields. Whatever the case, it depends on the specific entity class and the specific implementation of FieldItemListInterface to really decide what is best.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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