Problem/Motivation

  1. The block_content module is a block factory. It creates blocks plugins by deriving over reusable Block Content entities (\Drupal\block_content\Entity\BlockContent).
  2. The block module mainly offers block placement functionality.

Normally, block_content.module should not need block.module in order to function properly. The blocks produced by block_content can be placed on page by Layout Builder, Display Suite, Panels and even... (core) Block module.

But there are some dependencies on block.module exposed in block_content.module:

  • The administer blocks permission.
  • The block.admin_display route.
  • Unnecessarily installing block in tests
  • Redirects for insert/update with BlockContent entity form
  • Delete form and delete hook
  • ...anything else?

But, I think the above, dependencies were just a compromise, inherited from earlier Drupal versions. I see them only polluting the intra-module relationships by an unneeded dependency.

Proposed resolution

Now that we have decoupled block_content from the block library, I believe we can remove dependency

To be discussed...

  • Move block.admin_display route in core? - Believe this is no longer needed
  • What about the shared permission? - Believe this is no longer needed

Remaining tasks

User interface changes

None

API changes

Move appropriate customisations to Block or Block Content modules.
Add accommodations to testing in Block Content that relies on block placement to avoid block.module

Data model changes

None

Release notes snippet

@todo

CommentFileSizeAuthor
#29 3206952-nr-bot.txt91 bytesneeds-review-queue-bot

Issue fork drupal-3206952

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

claudiu.cristea created an issue. See original summary.

claudiu.cristea’s picture

Title: Why block_content depends on block » Why block_content depends on block?
Related issues: +#3206088: [PP-1] Remove dependency to Block module (may need 2.x)

I have the same issue with the contrib simple_block, see https://www.drupal.org/project/simple_block/issues/3206088

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Postponed » Active

Now that we have moved block_content we may be able to remove the dependency.

smustgrave’s picture

Status: Active » Closed (works as designed)
Related issues: +#3365183: [Meta] Block Content 10.2 sprint

So tested removing block as a dependency and can already see failures happening.

Seems like it would be more effort to remove block. There's also a preDelete call on the entity

    parent::preDelete($storage, $entities);

    /** @var \Drupal\block_content\BlockContentInterface $block */
    foreach ($entities as $block) {
      foreach ($block->getInstances() as $instance) {
        $instance->delete();
      }
    }

Which seems dependent on block.

Going to say this works as designed if anyone disagrees please reopen explaining why

Thanks!

mstrelan’s picture

I think it should still be possible to remove the dependency on block.module, it just may need a bit of refactoring. The \Drupal\block_content\BlockContentInterface::getInstances method (used in preDelete) provides special handling for block.module but completely ignores layout builder. It probably makes more sense to have a separate service for this integration, and the block instance deletion should probably be implemented in a hook. In other words, the BlockContent entity should have no awareness of Block entities.

smustgrave’s picture

Status: Closed (works as designed) » Active

Lets see if anyone picks it up.

smustgrave’s picture

Title: Why block_content depends on block? » Remove block dependency from block_content
Issue summary: View changes
smustgrave’s picture

Status: Active » Needs review
larowlan’s picture

Status: Needs review » Needs work

We haven't addressed #11 and #12 yet.

Which leads me to believe we're missing test coverage for that when block.module is disabled.

\Drupal\block_content\Entity\BlockContent::getInstances should hard fail when the block entity-type doesn't exist.

There's also a reference to block.admin_library in block_content.links.action.yml - it would be good to confirm that doesn't cause any side-effects.

block_content_add_action:
  route_name: block_content.add_page
  title: 'Add content block'
  appears_on:
    - block.admin_library ## here
    - entity.block_content.collection
  class: \Drupal\block_content\Plugin\Menu\LocalAction\BlockContentAddLocalAction
dpi’s picture

Assigned: Unassigned » dpi

Working on this...

acbramley’s picture

acbramley’s picture

dpi’s picture

Created a new MR @ https://git.drupalcode.org/project/drupal/-/merge_requests/12156

  • Generally a lot of reduction and dependency on Block between the two modules, and especially in tests.
  • A new small service for usage.
  • A new core test module to test navigation blocks, without needing block.module. See also #3524985: Reduce dependency on block.module in tests.

Implementation notes

  • BlockContentBrokenTest in block content is testing against \Drupal\Core\Block\Plugin\Block\Broken (in core), this core class relies on administer blocks permission defined by block. So I've created administer_blocks_permission_test test module to also define a administer blocks permission so we can test without block.module. I've created #3524897: [PP-1] Dependency from core block plugin to block module to address this problem where core is depending on a block.module permission, as of writing currently postponed on #1537198: Add a Production/Development toggle
  • A new hooks class added to block.module (BlockBlockContentHooks) which is conditionally enabled depending on whether block_content.module is enabled. This hooks class has form alter and button callbacks, formally located in Block Contents' \Drupal\block_content\BlockContentForm. Associated tests have been moved to Block.module's BlockContentSaveFormRedirectTest.
  • The functionality formerly provided by \Drupal\block_content\Entity\BlockContent::getInstances and \Drupal\block_content\Form\BlockContentDeleteForm moved to the three classes adjacent to \Drupal\block_content\BlockUsage\BlockContentBlockUsage
    • I considered making this a feature of block.module, but decided that BlockContent should still be responsible for this functionality and messages, but split out of the entity class.
    • A service was created, accessible at Drupal\block_content\BlockUsage\BlockContentBlockUsageInterface
    • The service is conditionally available, depending on whether block.module is also installed (BlockContentCompilerPass). See new test coverage at BlockContentBlockUsageTest
    • A multipurpose iterable/countable class provides the necessary block config producing and efficient counting functionality that BlockContentDeleteForm and BlockContent::preDelete require.
    • BlockContentDeleteForm and BlockContent::preDelete are still retain responsibility for calling out to this service.
    • Ive made BlockContentUsageList final for now. It should be un-finalised only if an interface is extracted, in the future.
    • \Drupal\block_content\Entity\BlockContent::getInstances deprecated, change notice at https://www.drupal.org/node/3523951
  • A new hooks class added to block_module.module (BlockContentBlockHooks) which is conditionally enabled depending on whether block.module is enabled. This hooks class provides the existing hooks to modify the block hook_theme/template and modify block config entity operations. Hopefully the block preprocess hook can be moved out when #3003610: Remove block.module dependency from Layout Builder finishes decoupling block hook_theme/template from block.module to core/system.module.
  • BlockContent's #[Hook('help')] modified with various decouplings and variable-izing permission names. One piece of help text used to reference administer blocks, but was incorrect since permission changes in #1975064: Add more granular block content permissions.
  • Changed BlockContentBlock::build to no longer rely on administer blocks, but on Url->access(). Looks like the permission was no longer correct anyway, since permission changes in #1975064: Add more granular block content permissions.
  • Generally, many Block Content Kernel and Functional tests no longer need block.module to be installed, both explicitely and implicitely by \Drupal\Tests\block_content\Functional\BlockContentTestBase.
    • I re-evaluated all Kernel/Functional tests in Block Content, and updated them with the explicit permissions the test user needs, instead of blanket permissions from BlockContentTestBase. This includes administer blocks, among others.
    • There were many uses of placeBlock to place title/actions/tasks on pages, which tests used to click links. However this method requires block.module, which we're trying to prevent unnecessary dependencies on. So I've introduced the navigation_variant_test test module which can render navigation blocks without block.module. I've created #3524985: Reduce dependency on block.module in tests to expand usage of this technique so other parts of core can reduce need of block.module via placeBlock/drupalPlaceBlock.
  • dpi’s picture

    Title: Remove block dependency from block_content » Remove block.module dependency from block_content
    Issue summary: View changes

    Clarifying title: its block module (not block config or theme_hook:block).

    I think though we're not "removing" a dependency, but rather block or block content are conditionally enhancing the other module depending on install state.

    -

    Updated issue summary a bit.

    acbramley’s picture

    I've gone through the MR quite thoroughly and generally +1 this overall approach although it is quite complicated and may be hard for some devs to grok, especially the compiler pass stuff (queue many "why isn't this working?!")

    My main concerns are:
    1. The administer_blocks_permission_test module, although very minor it just doesn't feel right to add a duplicate permission, this is a big nit picky though
    2. navigation_variant_test - now tests that just needed 1 block get all the additional elements rendered. Again this may be minor. I haven't ever looked into DisplayVariant plugins before though so thanks for the learning :)

    dpi’s picture

    Assigned: Unassigned » dpi
    Status: Needs review » Active

    Thanks, some actionables there for me.

    larowlan’s picture

    Left some questions on the MR

    dpi’s picture

    Assigned: dpi » Unassigned

    A couple q's for @larowlan. Remaining open threads I dont see as actionable/my implementation notes.

    dpi’s picture

    Status: Active » Needs review
    needs-review-queue-bot’s picture

    Status: Needs review » Needs work
    StatusFileSize
    new91 bytes

    The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

    acbramley changed the visibility of the branch 3206952-why-blockcontent-depends to hidden.

    Version: 11.x-dev » main

    Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

    Read more in the announcement.