Problem/Motivation

In Gutenberg sidebar, we can get a list of images presets. We don't want to expos the content author to ALL styles, as that causes confusion on complex sites.

Proposed resolution

We don't necessarily need a UI, but some way a themer could limit the list of available styles listed in image related blocks. From what I understand, this is not currently possible because of the order of the settings here https://git.drupalcode.org/project/gutenberg/-/blob/8.x-2.x/js/gutenberg...

I suggest the order is changed, and that a method for limiting the styles is documented.

Issue fork gutenberg-3340209

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

perandre created an issue. See original summary.

fabioneves made their first commit to this issue’s fork.

fabioneves’s picture

fabioneves’s picture

Adds an accordion to the "Gutenberg Experience" settings on content type edit screen with an option to filter allowed image styles.

fabioneves’s picture

Adds missing code to filter image sizes in gutenberg sidebar

fabioneves’s picture

Fixes 'original' being shown when it's not selected in allowed styles

jbaum_13’s picture

Thanks for working on this! This capability would be great!

I installed the patch #6 and got the following error:

Warning: foreach() argument must be of type array|object, null given in gutenberg_form_alter() (line 281 of /var/www/html/web/modules/contrib/gutenberg/gutenberg.module)
#0 /var/www/html/web/core/includes/bootstrap.inc(347): _drupal_error_handler_real(2, 'foreach() argum...', '/var/www/html/w...', 281)
#1 /var/www/html/web/modules/contrib/gutenberg/gutenberg.module(281): _drupal_error_handler(2, 'foreach() argum...', '/var/www/html/w...', 281)
#2 /var/www/html/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(562): gutenberg_form_alter(Array, Object(Drupal\Core\Form\FormState), 'node_type_edit_...')
#3 /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php(838): Drupal\Core\Extension\ModuleHandler->alter('form', Array, Object(Drupal\Core\Form\FormState), 'node_type_edit_...')
#4 /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php(282): Drupal\Core\Form\FormBuilder->prepareForm('node_type_edit_...', Array, Object(Drupal\Core\Form\FormState))
#5 /var/www/html/web/core/lib/Drupal/Core/Controller/FormController.php(73): Drupal\Core\Form\FormBuilder->buildForm(Object(Drupal\node\NodeTypeForm), Object(Drupal\Core\Form\FormState))
#6 [internal function]: Drupal\Core\Controller\FormController->getContentResult(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\RouteMatch))
#7 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array)
#8 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(580): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#9 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#10 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
#11 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(169): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#12 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(81): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#13 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#14 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#15 /var/www/html/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#16 /var/www/html/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#17 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#18 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#19 /var/www/html/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#20 /var/www/html/web/core/lib/Drupal/Core/DrupalKernel.php(718): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#21 /var/www/html/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#22 {main}

The list to select the image styles did work, and in gutenberg the options are only showing what I chose, but the image style is not being applied when i save the page.

kjankowski’s picture

Added a small fix that will prevent showing warnings in the editor whenever a new image style is added.

twiik’s picture

Status: Active » Reviewed & tested by the community
StatusFileSize
new3.12 KB

Here's the patch in #8 rerolled for Gutenberg 3.0.

We've been running that patch for ages now.

marcofernandes made their first commit to this issue’s fork.

  • marcofernandes committed 4a44271e on 3.0.x
    Issue #3340209 by fabioneves: Limit available image styles for image...
marcofernandes’s picture

Status: Reviewed & tested by the community » Fixed

Finally merged to 2.x and 3.0.x. Great work y'all 😉

tkiehne’s picture

Just a notice for anyone who gets a warning message as follows after upgrading to 3.0.3:

Warning: Undefined variable $sizes in gutenberg_form_node_form_alter() (line 765 of modules/contrib/gutenberg/gutenberg.module).

You will need to re-save the content type configuration for all content types using Gutenberg to set the allowed styles and clear this warning.

szeidler’s picture

@tkiehne This has fixed in #3467986: Undefined variable $sizes in gutenberg_form_node_form_alter().

But I have another one as well, if you just updated without saving all node forms again.

 Warning: Trying to access array offset on null in /var/www/html/web/modules/contrib/gutenberg/gutenberg.module on line 

So it's actually better to do the following

$allowed_styles = $config->get($node_type . '_allowed_image_styles') ?? [];

In addition there is the following:

  if (!empty($styles)) {
    foreach ($styles as $style) {
      if ($allowed_styles[$style->getName()]) {
        $sizes[] = [
          'slug' => $style->getName(),
          'name' => $style->label(),
        ];
      }
    }
  }

If the site introduces a new image style after the node form has been saved, this will also lead to a warning.

  • marcofernandes committed 4a44271e on 4.0.x
    Issue #3340209 by fabioneves: Limit available image styles for image...
szeidler’s picture

Thanks @marcofernandes

  • szeidler committed a57338fa on 8.x-2.x
    Issue #3340209: Fix undefined index warning on allowed image styles
    

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

codebymikey’s picture

Following this change, the Original (full) image style is no longer visible in 3.x.