Limit available image styles for image related blocks

Created on 8 February 2023, almost 2 years ago
Updated 30 August 2024, 5 months ago

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.

✨ Feature request
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡³πŸ‡΄Norway perandre

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

Merge Requests

Comments & Activities

  • Issue created by @perandre
  • First commit to issue fork.
  • πŸ‡΅πŸ‡ΉPortugal fabioneves

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

  • πŸ‡΅πŸ‡ΉPortugal fabioneves

    Adds missing code to filter image sizes in gutenberg sidebar

  • πŸ‡΅πŸ‡ΉPortugal fabioneves

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

  • 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.

  • πŸ‡΅πŸ‡±Poland kjankowski Warsaw

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

  • Status changed to RTBC 8 months ago
  • πŸ‡³πŸ‡΄Norway TwiiK Trondheim

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

    We've been running that patch for ages now.

  • First commit to issue fork.
  • Pipeline finished with Failed
    6 months ago
    Total: 170s
    #248982
  • Pipeline finished with Failed
    6 months ago
    Total: 176s
    #248983
  • Pipeline finished with Success
    6 months ago
    #248981
  • Pipeline finished with Success
    6 months ago
    Total: 259s
    #248988
  • Pipeline finished with Success
    6 months ago
    #249009
  • Status changed to Fixed 6 months ago
  • πŸ‡΅πŸ‡ΉPortugal marcofernandes

    Finally merged to 2.x and 3.0.x. Great work y'all πŸ˜‰

  • πŸ‡ΊπŸ‡ΈUnited States tkiehne

    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.

  • πŸ‡©πŸ‡ͺGermany szeidler Berlin

    @tkiehne This has fixed in πŸ› Undefined variable $sizes in gutenberg_form_node_form_alter() Fixed .

    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.

  • πŸ‡©πŸ‡ͺGermany szeidler Berlin

    Thanks @marcofernandes

    • szeidler β†’ committed a57338fa on 8.x-2.x
      Issue #3340209: Fix undefined index warning on allowed image styles
      
  • Pipeline finished with Success
    5 months ago
    Total: 1016s
    #256242
  • Pipeline finished with Failed
    5 months ago
    Total: 452s
    #256252
  • Pipeline finished with Failed
    5 months ago
    Total: 545s
    #256257
  • Pipeline finished with Failed
    5 months ago
    Total: 1030s
    #256274
  • Pipeline finished with Success
    5 months ago
    Total: 858s
    #257504
  • Pipeline finished with Success
    5 months ago
    Total: 279s
    #258211
  • Pipeline finished with Skipped
    5 months ago
    #259239
  • Automatically closed - issue fixed for 2 weeks with no activity.

  • Pipeline finished with Failed
    4 months ago
    Total: 1018s
    #288276
  • Pipeline finished with Failed
    4 months ago
    #288293
  • Pipeline finished with Failed
    4 months ago
    Total: 838s
    #288324
  • Pipeline finished with Success
    4 months ago
    Total: 410436s
    #288385
  • Pipeline finished with Success
    4 months ago
    Total: 976s
    #293106
  • Pipeline finished with Success
    4 months ago
    Total: 1016s
    #293159
  • Pipeline finished with Success
    4 months ago
    Total: 166s
    #293256
  • Pipeline finished with Success
    4 months ago
    Total: 144s
    #293306
  • Pipeline finished with Success
    4 months ago
    Total: 138s
    #293462
  • Pipeline finished with Success
    4 months ago
    Total: 227s
    #293504
  • Pipeline finished with Success
    4 months ago
    Total: 130s
    #293542
  • Pipeline finished with Success
    4 months ago
    Total: 149s
    #293570
  • Pipeline finished with Success
    4 months ago
    Total: 133s
    #293982
  • Pipeline finished with Success
    4 months ago
    Total: 150s
    #294115
  • Pipeline finished with Success
    4 months ago
    Total: 215s
    #294170
  • Pipeline finished with Success
    4 months ago
    Total: 1081s
    #300655
  • Pipeline finished with Success
    4 months ago
    Total: 1572s
    #300759
  • Pipeline finished with Success
    4 months ago
    Total: 1763s
    #300795
  • Pipeline finished with Success
    4 months ago
    Total: 1884s
    #300835
  • Pipeline finished with Canceled
    3 months ago
    Total: 226s
    #307908
  • Pipeline finished with Success
    3 months ago
    Total: 280s
    #307910
  • Pipeline finished with Success
    3 months ago
    Total: 315s
    #321634
  • Pipeline finished with Failed
    3 months ago
    Total: 333s
    #321644
  • Pipeline finished with Success
    2 months ago
    Total: 215s
    #342380
  • Pipeline finished with Skipped
    14 days ago
    #389854
Production build 0.71.5 2024