The "moderation_state" base field overrides cause install from existing config to fail

Created on 22 April 2020, over 4 years ago
Updated 27 March 2023, over 1 year ago

Problem/Motivation

This issue first got reported in #2321071-38: BaseFieldOverride fails to take into account ContentEntityInterface::bundleFieldDefinitions() when invoking onFieldDefinitionUpdate() .

The moderation_state base field overrides cause the Drupal install from existing config to fail.
See the explanation here #2321071-42: BaseFieldOverride fails to take into account ContentEntityInterface::bundleFieldDefinitions() when invoking onFieldDefinitionUpdate() and also the rest of the comments there.

To reproduce:
1. Install Drupal using the minimal profile
drush site-install minimal
2. Enable Content Moderation and Content Translation
drush en content_moderation content_translation -y
3. Create a CT
4. Create a workflow and enable it on the CT
5. Go to admin/config/regional/content-language and enable "Content", save
6. Export configuration. You should get a config_override file for the moderation_state base field.
drush cex -y
7. Install Drupal using the existing configuration
drush site-install --existing-config
TypeError: Argument 2 passed to Drupal\Core\Entity\ContentEntityStorageBase::onFieldDefinitionUpdate() must implement interface Drupal\Core\Field\FieldDefinitionInterface, null given, called in /home/herve/Documents/commission/drupal/drupal-core/core/lib/Drupal/Core/Field/Entity/BaseFieldOverride.php on line 205 in /home/herve/Documents/commission/drupal/drupal-core/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php on line 463 #0 /home/herve/Documents/commission/drupal/drupal-core/core/lib/Drupal/Core/Field/Entity/BaseFieldOverride.php(205): Drupal\Core\Entity\ContentEntityStorageBase->onFieldDefinitionUpdate(Object(Drupal\Core\Field\Entity\BaseFieldOverride), NULL)

Proposed resolution

1.
-> will not work, see #7

2.
-> will not work, see #12

3.
-> will not work, see #14

4.
-> will not work, see #17

5.
-> may not be ideal, see #19

6. Remove moderation_state from the "Content language" form using hook_form_language_content_settings_form_alter, which would prevent the BFO creation.

🐛 Bug report
Status

Needs review

Version

10.1

Component
Content moderation 

Last updated 24 days ago

Created by

🇧🇪Belgium herved

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

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇦Ukraine Taran2L Lviv

    Updated patch with just a new config archive (the previous one was not sorted correctly). Please review

  • 🇺🇦Ukraine Taran2L Lviv

    Those config archive are hard :)

  • 🇺🇦Ukraine Taran2L Lviv

    Hm, I think test with fixtures should go to the module

  • Status changed to Needs work over 1 year ago
  • 🇺🇸United States smustgrave

    Per #39

    Should we find a way to throw an exception when a base field override is created, to prevent these leaking into installations in other ways? If we're doing this, it'd be nice to confidently say: there is never a BFO for any CM field, on any installation.

    Do we need an update hook to delete the existing ones?

    We're missing test coverage that asserts the option is missing from the UI and no BFO entities are created when submitting the UI.

    Don't think these have been answered yet.

  • 🇷🇴Romania ioana apetri

    I updated the patch for the Drupal 10.3.x compatibility. Thank you!

  • 🇮🇳India pradeepjha

    Last patch doesn't fix the issue.
    Currently this issue only gets fixed if we manually remove `*.moderation_state.yml` from config folder. We might need new patch to fix this issue.
    I've checked in 10.2.x version.

  • 🇧🇪Belgium herved

    #54, see #37, the current patch only prevents moderation_state BFOs to be exported. Existing ones need to be removed manually.
    Let's not continue with patches and create a merge request.

  • Pipeline finished with Failed
    about 1 month ago
    Total: 108s
    #347967
  • Pipeline finished with Success
    about 1 month ago
    #347973
  • Pipeline finished with Success
    about 1 month ago
    #347999
  • leymannx Berlin

    I'm wondering if this shouldn't maybe be fixed on a more dynamic scale for every computed field. Because we currently have the same problem with other fields and they all have ->setComputed(TRUE) in their bundleFieldDefinition.

    So, shouldn't we maybe go with something like this in the after build:

      foreach ($form['#labels'] as $entity_type_id => $label) {
        foreach (\Drupal::service('entity_type.bundle.info')->getBundleInfo($entity_type_id) as $bundle => $bundle_info) {
          foreach (\Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type_id, $bundle) as $field_name => $field_definition) {
            if ($field_definition->isComputed()) {
              if (isset($form['settings'][$entity_type_id][$bundle]['fields'][$field_name])) {
                unset($form['settings'][$entity_type_id][$bundle]['fields'][$field_name]);
                $form_state->unsetValue(['settings', $entity_type_id, $bundle, 'fields', $field_name]);
              }
            }
          }
        }
      }
      return $form;
    
  • leymannx Berlin

    We might rename that issue then to "Prevent the creation of base field overrides for computed fields"

  • leymannx Berlin

    We might rename that issue then to "Prevent the creation of base field overrides for computed fields"

  • leymannx Berlin

    But I'm not sure, as this seems to also hide much more fields than just problematic ones. For example now also URL alias and menu link are hidden.

  • leymannx Berlin

    Hm, that brings me to the point to question myself: What is special about that content_moderation field, while other computed fields work just fine? 🤔

  • 🇧🇪Belgium herved

    What is special about that content_moderation field, while other computed fields work just fine?

    See #2321071-42: BaseFieldOverride fails to take into account ContentEntityInterface::bundleFieldDefinitions() when invoking onFieldDefinitionUpdate() + this issue description.
    It's convoluted...

  • leymannx Berlin

    The way we solved it now is mimicking other unproblematic computed fields like metatags, providing a new field type with an empty schema:

    public static function schema(FieldStorageDefinitionInterface $field_definition) {
      return [];
    }
    
Production build 0.71.5 2024