When adding a moderation_state field to a view (any view) causes ajax loading issue and produces this error:

Warning: Undefined array key "moderation_state" in commerce_promotion_form_views_ui_config_item_form_alter() (line 106 of /var/www/html/web/modules/contrib/commerce/modules/promotion/commerce_promotion.module).

Once I dug into see what the issue was I found a related/similar issues reported for commerce in another area; commerce price https://www.drupal.org/project/commerce/issues/3113031

- Which for the most part is identical to this error, just for a different commerce sub module promotion. Based on the patch for issue 3113031 I've attached a patch for this.

Modifying: (line 106)
if ($handler instanceof EntityField && !empty($handler->definition['entity_type'])) {
to:
if ($handler instanceof EntityField && !empty($handler->definition['entity_type']) && !empty($handler->options['entity_field'])) {

Didn't investigate any further than that - there may be other sub modules using $handler instanceof EntityField in a views form alter that should also get updated to prevent issues with moderation_state.

Steps to reproduce

  1. In a module, declare a computed field for a specific bundle, using hook_entity_bundle_info():
      #[Hook('entity_bundle_field_info')]
      public static function entityBundleFieldInfo(ContentEntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
        $fields = [];
    
        $entity_type_id = $entity_type->id();
        if ($entity_type_id === 'group' && $bundle === 'entity') {
          $fields['my_computed_field'] = BaseFieldDefinition::create('string')
            ->setLabel(t('My Computed Field'))
            ->setComputed(TRUE)
            ->setClass(MyComputedField::class)        
            ->setDescription(t('Description'));
        }
      }
    
  2. In hook views data alter, declare that computed field, following instructions defined in change record: https://www.drupal.org/node/3380621
      #[Hook('views_data_alter')]
      public function viewsDataAlter(array &$data) {
        $data['my_table']['my_computed_field] = [
          'title' => $this->t('My Computed field'),
          'group' => $this->t('Computed fields'),
          'field' => [
            'entity_type' => 'node',
            'real field' => 'my_computed_field',
            'id' => 'field',
            'default_formatter' => 'string',
            'field_name' => 'my_computed_field',
          ],
        ];
    
    
  3. Add the field into a view with Fields in its display.
  4. Try to configure the field. After clicking the field, the views settings won't popup.

Comments

sidgrafix created an issue. See original summary.

ushma’s picture

Status: Active » Needs review
jsacksick’s picture

Status: Needs review » Postponed (maintainer needs more info)

Could you provide reproducible steps? Do I need to the Content moderation module? What else is needed?

omarlopesino’s picture

Issue summary: View changes
Status: Postponed (maintainer needs more info) » Needs review

I have this warning in my project. It happens when I create a bundle computed field and add I declare it into views. The problem happens because commerce promotion is trying to get the field information from entity field definitions, and not entity bundle field definitions.

I've added a 'Steps to reproduce' section at the issue.

This patch solves the issue for me. Please review, thanks!

omarlopesino’s picture

Issue summary: View changes
omarlopesino’s picture

Title: Undefined array key moderation_state in commerce promotion view form alter » Commerce promotion warning is preventing editing views field configuration for bundle fields

  • jsacksick committed 026729a7 on 3.x
    Issue #3478603 by sidgrafix, omarlopesino, ushma, jsacksick: Commerce...
jsacksick’s picture

Version: 8.x-2.40 » 3.x-dev
Status: Needs review » Fixed

Committed a slightly different patch to 3.x that should solve this. 2.x is no longer maintained.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.