Allow custom URL() to be used for image fields

Created on 3 May 2021, over 3 years ago
Updated 11 May 2023, over 1 year ago

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()]),
      ],
    ]);
Feature request
Status

Active

Version

10.1

Component
Image module 

Last updated 10 days ago

Created by

🇺🇸United States timodwhit

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇩🇪Germany Anybody Porta Westfalica

    Just 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! :)

  • last update over 1 year ago
    29,384 pass
  • 🇩🇪Germany Anybody Porta Westfalica

    In a follow-up also the UI should be provided, adding a 3rd select option "Custom URL" which shows an input field with token support.

Production build 0.71.5 2024