Score:0

Best practice for getting field values of entity chains

cn flag

I really would like to make this if/else field value check just a little bit more sexy.

What's Best Practice for dealing with nested functions chains to get entity field values (check if they exist / check if they are not empty)

 if($entity->hasField('field_intro_teaser_image_media_')){
      $media_field = $entity->field_intro_teaser_image_media_;

      if($media_field->entity){
        $media_field_entity = $media_field->entity;

        if($media_field_entity->hasField('field_media_image')){
          $media_field_entity_image = $media_field_entity->field_media_image;

          if($media_field_entity_image){
            $media_field_entity_image_entity = $media_field_entity_image->entity;

            if($media_field_entity_image_entity){
              $media_field_entity_image_entity_uri = $media_field_entity_image->entity->getFileUri();

              if($media_field_entity_image_entity_uri){

                // ... yay we got the damn uri!

              }

            }
          }
        }
      }
    }

The one liner would be:

$media_field = $entity->field_intro_teaser_image_media_->entity->field_media_image->entity->getFileUri();



  
cn flag
Not tried it on entity fields yet, but [Null-safe operator](https://php.watch/versions/8.0/null-safe-operator)?
Kevin avatar
in flag
The one liner would fail if anything in the chain does not have the field or relational entity, so you'd want to wrap it in a try catch statement.
4uk4 avatar
cn flag
Or use Twig, see https://drupal.stackexchange.com/questions/271579/get-video-url-from-media-entity-reference-field. Twig is null safe by default, if you don't set strict_variables.
Score:1
us flag

When you access fields by magic methods, you don't actually have to check for their existence by using hasField() method.:

$entity->field_that_does_not_exist->entity; // NULL.
$entity->get('field_that_does_not_exist'); // Exception.
$entity->get('field_that_does_not_exist')->entity; // Exception.

Using $entity->field_intro_teaser_image_media_->entity is sufficient in this case, if the field does not exist, the expression will be simply NULL, without any errors.

But of course you still have to check if referenced ->entity is not NULL. PHP 8.0 Null-safe operator may be a way to do it, as suggested by Clive.

$media_field = $entity
  ->field_intro_teaser_image_media_
  ->entity
  ?->field_media_image
  ->entity
  ?->getFileUri();

In case of using Null-safe operator, it will be probably faster to access entity fields by ->get(), to avoid additional logic.

$media_field = $entity
  ->get('field_intro_teaser_image_media_')
  ?->entity
  ?->get('field_media_image')
  ?->entity
  ?->getFileUri();

mogio avatar
cn flag
thank you . Love the Null Safe Operator. Never used it so far.
Score:1
de flag

Assuming you've built the chain correctly, you can do it as follows:

$media_field = NULL;
try {
  $media_field = $entity->field_intro_teaser_image_media_->entity->field_media_image->entity->getFileUri();
}
catch (\Exception $e) {
  watchdog_exception('Media retrieval');
}

if (!is_null($media_field)) {
  // Do stuff.
}
mogio avatar
cn flag
Thanks. I Did it this way.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.