Problem/Motivation
When using viewField() to render an image field it would be nice to be able to pass a Url() instance. It makes sense to limit the allowed values in the UI and the setting form, but it would be helpful to be able to programatically render the field with a derived URL that is not 'content' or 'file'
Steps to reproduce
Proposed resolution
Add something like the below to viewElements() in ImageFormatter.php:
elseif ($image_link_setting instanceof Url) {
$url = $image_link_setting;
}
Remaining tasks
- Add the code
- Write the unit tests
User interface changes
N/A
API changes
Data model changes
None.
Release notes snippet
Url() instances can now be programmatically passed to Image Fields when using viewField(). Example:
$entityTypeManager = \Drupal::service('entity_type.manager');
$entityTypeManager->getViewBuilder($entity_type);
$build = $view_builder->viewField($user->field_image, [
'label' => 'hidden',
'settings' => [
'image_style' => 'large',
'image_link' => new Url('custom.user.route', ['user' => $user->id()]),
],
]);
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | 3211892-1-add-url-handling-to-custom-rendering.patch | 2.36 KB | timodwhit |
Comments
Comment #2
timodwhit commentedComment #6
anybodyJust ran into the same thing and this is a brilliant idea!
Allowing a third option "Custom URL" would be super helpful, especially, if tokens could be used!
Third-party integrations could also utilize this field formatter setting then to provide just an URL object to link the image somewhere! :)
Comment #7
anybodyIn a follow-up also the UI should be provided, adding a 3rd select option "Custom URL" which shows an input field with token support.