Drupal: get media url with imagestyle



    Andrea Pistarini

    The $foto variable holds a reference to the entity associated with the field "field_foto" in the $ip object. The $foto_uri variable holds the URI of the file associated with the entity. The $foto_url variable holds the URL of the file.

    Library: Drupal

    $foto = $ip->get('field_foto')->referencedEntities();
    $foto_uri = $foto->field_media_image->entity->getFileUri(); // It is for image media.
    $foto_url = ImageStyle::load('mi_image_style')->buildUrl($foto_uri);
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.