Field moderation_state is unknown.

Created on 22 March 2019, over 5 years ago
Updated 6 April 2023, over 1 year ago

Hi,

I'm on Drupal 8.5.14 and I'm having some trouble using the drush wbm2cm-migrate command.

drush --uri=mysite.com wbm2cm-migrate
Saving existing moderation states to temporary tables...
Migration failed with source plugin exception: Field moderation_state is unknown.                             [error]
Processed 696 managed faq entities.
 Processed 16375 content items.
  Processed 0 paragraph entities.

Can anyone shed some light on the cause of the Field moderation_state is unknown error?

🐛 Bug report
Status

Needs review

Version

3.0

Component

Code

Created by

🇬🇧United Kingdom Adamation

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

Comments & Activities

Not all content is available!

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

  • 🇦🇺Australia b.ravanbakhsh Adelaide

    In my case also like comment #3 Web-Beest we had some lingering configuration that added workbench_moderation as third-party settings in DB. so I create a code snippet to drop those fields and let the workbench moderation module uninstalled. also in some cases the uninstall was blocked by the field that was missing, ao this snippet adds the missing too.

    $table_names = _add_missing_moderation_state_column();
    foreach ($table_names as $table_name) {
        $schema->dropField($table_name, 'moderation_state');
      }
    
    **
     * Add moderation_state to database entity tables.
     *
     * Some tables in database like linky, block_field_data, .. don't have the
     * moderation_state field, and it makes the uninstallation of
     * workbench moderation module fails. This function adds the field to tables.
     */
    function _add_missing_moderation_state_column() {
      /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */
      $entity_field_manager = \Drupal::service('entity_field.manager');
    
      $entity_type_manager = \Drupal::entityTypeManager();
      $schema = \Drupal::database()->schema();
      $moderationStateColumnSpec = [
        'type' => 'varchar',
        'description' => 'workbench migration',
        'length' => 255,
        'not null' => FALSE,
      ];
      $table_names = [];
      foreach ($entity_type_manager->getDefinitions() as $entity_type) {
        if ($entity_type->entityClassImplements(FieldableEntityInterface::CLASS)) {
          foreach ($entity_field_manager->getFieldStorageDefinitions($entity_type->id()) as $storage_definition) {
            if ($storage_definition->getProvider() == 'workbench_moderation') {
              $table_name = $entity_type->getDataTable() ?? $entity_type->getProvider();
              if ($table_name && $schema->fieldExists($table_name, 'moderation_state') === FALSE) {
                $schema->addField($table_name, 'moderation_state', $moderationStateColumnSpec);
                $table_names[] = $table_name;
              }
            }
          }
        }
      }
      return $table_names;
    }
  • 🇦🇺Australia b.ravanbakhsh Adelaide

    reply to #4 @karenann comment to where put these codes and the sequence. I have a hook_post_update and run the migration like this sequence.

    function ua_post_update_wbm2cm_migrate() {
    $module_installer = \Drupal::service('module_installer');
      $module_installer->install(['wbm2cm']);
      _ua_wbm2cm_save();
      // Clear work bench moderation can be skipped, as drupal uninstall workbench moderation will do the job.
      // _ua_wbm2cm_clear(FALSE);
      $fields = \Drupal::service('wbm2cm.migration_controller')->getOverriddenFields();
      if ($fields) {
        \Drupal::logger('ua')->info('It looks like you have overridden the moderation_state base field. These overrides will be reverted because they are incompatible with Content Moderation. You will also need to delete these from your exported config.');
    
        /** @var \Drupal\Core\Field\Entity\BaseFieldOverride $field */
        foreach ($fields as $field) {
          $field->delete();
          $message = sprintf('Reverted %s. Delete %s.yml from your exported config.', $field->id(), $field->getConfigDependencyName());
          _ua_logger($message);
          \Drupal::logger('ua')->info($message);
        }
      }
      $table_names = _add_missing_moderation_state_column();
      \Drupal::logger('ua')->info('Uninstalling Workbench Moderation...');
      $module_installer->uninstall(['workbench_moderation']);
      foreach ($table_names as $table_name) {
        $schema->dropField($table_name, 'moderation_state');
      }
      $module_installer->install(['workflows']);
      $splitFile = 'workflows.workflow.editorial';
      $config_path = realpath('../config-export');
      $source = new FileStorage($config_path);
      $config_storage->write($splitFile, $source->read($splitFile));
      \Drupal::logger('ua')->info('Installing Content Moderation...');
      $module_installer->install(['content_moderation']);
      _ua_wbm2cm_restore();
      $module_installer->uninstall(['wbm2cm']);
    }
Production build 0.71.5 2024