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
- 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')); } } -
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', ], ]; - Add the field into a view with Fields in its display.
- Try to configure the field. After clicking the field, the views settings won't popup.
| Comment | File | Size | Author |
|---|---|---|---|
| commerce-promotion-ui-config-item-form-alter-1.patch | 960 bytes | sidgrafix |
Comments
Comment #2
ushma commentedComment #3
jsacksick commentedCould you provide reproducible steps? Do I need to the Content moderation module? What else is needed?
Comment #4
omarlopesinoI 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!
Comment #5
omarlopesinoComment #6
omarlopesinoComment #8
jsacksick commentedCommitted a slightly different patch to 3.x that should solve this. 2.x is no longer maintained.