Allow API to restrict which image styles are available to users

Created on 25 August 2021, about 4 years ago
Updated 14 October 2024, 12 months ago

Problem/Motivation

When adding images in Gutenberg we see all the image styles available. With so many modules adding their own images styles this can be overwhelming for the user.

This experience has been improved with the work done in #3340209, but it's still useful if there's a way to systematically restrict who has access to the styles in a more modular fashion e.g. via permissions.

Proposed resolution

Add a hook in Gutenberg to allow custom modules to restrict image styles shown.

API changes

\Drupal::moduleHandler()->alter('gutenberg_image_styles', $sizes);

Example usage

// remove thumbnail style from Gutenberg
function MYMODULE_gutenberg_image_styles_alter(array &$sizes) {
  // Never allow access to the thumbnail style.
  unset($sizes['thumbnail']);
  $user = \Drupal::currentUser();

  foreach ($sizes as $style => $value) {
    // Check if the current user has custom permission to access this particular style.
    if (!$user->hasPermission('access image style for ' . $style)) {
      unset($sizes[$style]);
    }
  }
}
Feature request
Status

Active

Version

2.0

Component

Code

Created by

🇬🇧United Kingdom GHaddon

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

Merge Requests

Comments & Activities

Not all content is available!

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

  • Provided a MR restricting the loaded image styles.

  • Pipeline finished with Failed
    8 months ago
    Total: 175s
    #429960
  • Pipeline finished with Success
    8 months ago
    Total: 184s
    #429970
  • Pipeline finished with Success
    3 months ago
    #541602
  • Pipeline finished with Success
    3 months ago
    #541627
  • Pipeline finished with Failed
    3 months ago
    #544775
  • Pipeline finished with Success
    3 months ago
    #550287
  • Pipeline finished with Success
    about 1 month ago
    Total: 385s
    #591192
  • Pipeline finished with Success
    about 1 month ago
    #591337
  • Pipeline finished with Success
    about 1 month ago
    #591372
  • Pipeline finished with Success
    27 days ago
    Total: 166s
    #600961
  • Pipeline finished with Success
    27 days ago
    Total: 393s
    #600977
  • Pipeline finished with Failed
    4 days ago
    Total: 2080s
    #623573
Production build 0.71.5 2024