0

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();



  
3
  • Not tried it on entity fields yet, but Null-safe operator?
    – Clive
    Apr 26, 2022 at 15:55
  • 1
    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.
    – Kevin
    Apr 26, 2022 at 16:42
  • Or use Twig, see drupal.stackexchange.com/questions/271579/…. Twig is null safe by default, if you don't set strict_variables.
    – 4uk4
    Apr 27, 2022 at 7:12

2 Answers 2

2

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();

1
  • 1
    thank you . Love the Null Safe Operator. Never used it so far.
    – mogio
    May 17, 2022 at 11:16
1

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.
}
1
  • Thanks. I Did it this way.
    – mogio
    May 17, 2022 at 11:17

Your Answer

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

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