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: Limit available image styles for image related blocks, 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]);
    }
  }
}

Issue fork gutenberg-3229848

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

GHaddon created an issue. See original summary.

ghaddon’s picture

Title: Image styles should be able to be restricted. » Allow more flexibility over what image styles are available.
bessonweb’s picture

I think it will be a good thing to can restrict the image styles are available.

But if we can do that by the .gutenberg.yml in the theme folder, I think it's better than create a custom module.

I hope somebody could make this.

codebymikey’s picture

Title: Allow more flexibility over what image styles are available. » Allow API to restrict which image styles are available to users
Issue summary: View changes
Related issues: +#3340209: Limit available image styles for image related blocks

codebymikey’s picture

Version: 8.x-2.x-dev » 3.0.x-dev
Issue summary: View changes
Status: Active » Needs review
Related issues: +#3415218: Normalize the gutenberg.settings config and support image sizes filtering

Provided a MR restricting the loaded image styles.