Add option to skip all blocks from block_content

Created on 1 December 2023, 7 months ago
Updated 24 April 2024, 2 months ago

Problem/Motivation

Relates to issue: https://www.drupal.org/project/drupal/issues/2756331 Custom blocks cannot be properly exported and imported Active

Drupal core doesn't currently have a sophisticated mechanism for handling block content created by the core block_content module. As written config_ignore does provide a way to ignore those items individually (if you know the config name for each) but because there is no naming differentiation between block config created by block_content and block config created by other modules, there isn't an easy way to wildcard ignore all of them. This sort of functionality would be helpful in situations where a site is using block_content in production as a way for end users to create blocks and we don't want to track those items in config (as they are being used as content)

Proposed resolution

Ideally this would be a configurable option (with a checkbox or something similar) so an admin can tell config_ignore to pay no attention to any block config generated by block_content. For now I've written a patch to skip these items without a configuration option. With feedback from the maintainers I'm happy to work on further iterations.

Feature request
Status

Needs work

Version

3.0

Component

Code

Created by

🇨🇦Canada JayDarnell Guelph, Ontario

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

Merge Requests

Comments & Activities

  • Issue created by @JayDarnell
  • 🇨🇦Canada JayDarnell Guelph, Ontario
  • Merge request !20Issue #3405605: skip block_content blocks → (Closed) created by JayDarnell
  • Pipeline finished with Success
    7 months ago
    Total: 203s
    #58112
  • 🇨🇦Canada JayDarnell Guelph, Ontario

    The initial merge request takes care of not tracking these items for export, but I need to update it so the custom blocks aren't deleted on import.

  • Pipeline finished with Success
    7 months ago
    Total: 204s
    #58126
  • 🇨🇭Switzerland bircher 🇨🇿

    I am sympathetic to this request. But I would rather not have any code for specific config in the event subscriber. It is complicated enough as it is.

    Especially if dealing with the issue could easily be done with the new hook we added. So the easiest is to create a module which only implements the hook and adds an ignore pattern for all the content block placements.
    Then by virtue of installing that specific module you say that you want to ignore all the content blocks. You could then on your site even add a pattern to not ignore a specific content block.

    I can provide feedback on the hook implementation of course. If you want me to maintain it as a sub module of config ignore then it needs to have test coverage, if you want to maintain it as a standalone module you are welcome to do so too. I am happy to help with the hook in either case.

  • 🇨🇦Canada JayDarnell Guelph, Ontario

    Hi bircher, first of all thank you for this awesome module. I wasn't quite sure how to utilize the new hook and would greatly appreciate any guidance in that respect. I couldn't figure out how to create a pattern that only ignores block config from the block_content module because the naming convention for block_content blocks and blocks from a different module all follow the same naming convention: block.block.
    _.yml

    Examples:

    We have a custom block type called "Text" that is simply a block description and body field. I created one of these with the block description "Carl Sagan" and config management wanted to export the file: block.block.mytheme_carlsagan.yml.

    A block from the book module for book navigation is exported as the file: block.block.mytheme_booknavigation.yml.

    In our environment end users could create an infinite number of custom text blocks so we don't want to track any of them, but with the way these ymls are named, I can't tell the difference between a block created by block_content and other blocks. The only difference I could find was the settings->provider value within each of their yaml files (which tells us the module responsible for creating them)

    Thank you in advance for your help. If we can avoid using a one off patch and can work with the module as provided that would be preferable.

  • 🇨🇭Switzerland bircher 🇨🇿

    Hi sorry for the wait.

    The hook should look something like:

    /**
     * Implements hook_config_ignore_ignored_alter().
     */
    function mymodule_config_ignore_ignored_alter(\Drupal\config_ignore\ConfigIgnoreConfig $ignored) {
      $patterns = [];
      // We ignore only the content blocks that exist on the site.
      // But because we ignore them also when exporting they should not get into the sync directory.
      foreach(\Drupal::configFactory()->listAll('block.block') as $name) {
        $block = \Drupal::config($name);
        if ($block->get('settings.provider') === 'block_content') {
          $patterns[] = $name;
        }
      }
      if (empty($patterns)) {
        // There were no content blocks.
        return;
      }
    
      // This is the easiest way to add patterns to the ConfigIgnoreConfig object.
      $blockIgnore = new \Drupal\config_ignore\ConfigIgnoreConfig('simple', $patterns);
      $ignored->mergeWith($blockIgnore);
    }
    

    Like I said I would be ok adding this as a sub-module of config ignore because it would serve as a great example of such a hook implementation, but someone needs to write a test for it.

  • 🇨🇦Canada JayDarnell Guelph, Ontario

    Thanks so much bircher. I need some practice writing tests as it is. If you want I could turn this issue into a feature request and create a new branch and PR to add this as a sub module with a suitable test.

  • Assigned to JayDarnell
  • 🇨🇦Canada JayDarnell Guelph, Ontario
  • Issue was unassigned.
  • 🇮🇳India sakthi_dev

    @JayDarnell, As there is no activity more than 2 months I unassigned you from this issue.

  • Pipeline finished with Failed
    3 months ago
    Total: 197s
    #129467
  • Status changed to Needs review 3 months ago
  • 🇮🇳India sakthi_dev

    Hi @bircher and @JayDarnell, created the sub module for ignoring block content along with the test. Please review.

  • Status changed to Needs work 2 months ago
  • 🇨🇭Switzerland bircher 🇨🇿

    This should be rebased.
    But more importantly the test added in this issue is not doing its job. The test needs to assert that blocks are indeed ignored.

  • Pipeline finished with Canceled
    2 months ago
    Total: 64s
    #154411
  • Status changed to Needs review 2 months ago
  • 🇮🇳India sakthi_dev

    @bircher, Rebased and updated the test. Please review.

  • Pipeline finished with Success
    2 months ago
    Total: 207s
    #154414
  • Status changed to Needs work 2 months ago
  • 🇨🇭Switzerland bircher 🇨🇿

    The test does still not test what this module is made to do.

Production build 0.69.0 2024