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

Comments

timodwhit created an issue. See original summary.

timodwhit’s picture

Title: Allow URL() to be used from image_link » Allow custom URL() to be used for image fields
StatusFileSize
new2.36 KB

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

anybody’s picture

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

anybody’s picture

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.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.