I have a image "sample.jpg" (In media library) and I used this image in multiple custom blocks of different block types.

Now I need an information of all the custom blocks which are using "sample.jpg" image.


2 Answers 2


You can use the entityTypeManager, like this:

$blocks = \Drupal::entityTypeManager()->getStorage('block')->loadByProperties(['field_media_1' => $media_id, 'field_media_2' => $media_id]);

Where $media_id the the is of your media entity.

That will give you all the blocks that references that media entity.

  • Sorry it is not working, I am getting NULL
    – Ashi
    Commented May 31, 2019 at 10:41
  • You have to replace field_media with the field name if the media field, it has to be the same everywhere for it to work
    – user72672
    Commented May 31, 2019 at 10:47
  • Oh Okay, Thank you. But I have different field names, Is there any solution for that case ?
    – Ashi
    Commented May 31, 2019 at 10:49
  • Look at updated answer
    – user72672
    Commented May 31, 2019 at 11:08

Thank you @Jdrupal, with reference to your answer I have updated 'block' to 'block_content' and it is working fine.

        $blocks = \Drupal::entityTypeManager()
      ->loadByProperties(['logo1' => $media_id_1, 'logo2' => $media_id_2]);

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.