0

I'm using the Stock Photo Search module, for particular image fields, and it has its own field type with interesting properties.

As far as I can tell, the only way to access the URI of the image is content.field_stock_photo[0]['#uri']. There is no node.field_stock_photo.0.entity.uri.value or similar. The only way to access the URI is through the rendered entity.

The problem I'm running into is when using a reference to another node that has the stock_photo_search field, I have no way to access this rendered entity from a reference. node.reference.0.entity.field_stock_photo.value doesn't do it, and as far as I can tell node.reference.0.content.field_stock_photo doesn't work.

How do I go about getting properties from a rendered entity of a reference in Twig?

  • 1
    The question doesn't make sense, because this is not connected to entity references. The field type is not an image field type (which would be a file reference). It is a simple string value field, which is processed in the field formatter into a url for an image style. You could make a feature request in the issue queue to get the code refactored, moving it to the field item where it is accessible in Twig. When the method name follows the twig sandbox rules, for example getImageUrl()), you can use {{ node.field_stock_photo.0.imageurl }}. – 4k4 Jan 3 at 13:08
  • The problem is accessing the rendered content type when accessing FROM a referenced entity. E.g. node (business) has a reference to a node type of (logo) which has a (stock photo search) field. I can access the uri directly through content.field_stock_photo[0]['#uri'], when in the logo template, but when in the business template I have no way to access those content attributes. – Matt Jan 3 at 13:16
  • OK, now I see the connection to reference fields. You still need the refactoring to access the image like this {{ node.reference.0.entity.field_stock_photo.0.imageurl }}. The build array of a nested entity doesn't exist yet when the parent is rendered, so this is too early to access the output of the field formatter in {{ content }}. – 4k4 Jan 3 at 13:28
  • You can use view() in PHP though, to build the render array earlier. See the answer. – 4k4 Jan 3 at 13:39
1

In PHP you could force to build the render array of the field of a referenced entity early:

$build = $node->reference->entity->field_stock_photo->view();

And then look for '#uri' in the build array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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