TransWikia.com

Condition on current moderation state

Drupal Answers Asked by paul cappucci on November 23, 2021

I would like to show a message when content has changed moderation states from review to published. I set this up below but it does not work. Is there a rules debug log in Drupal 8?

Event: When content is update.

Condition: Data Comparison.

  • node.moderation_state
  • Operator: ==
  • data value: published

Action: Send System message, with

  • The message type
  • drupal_get_message(‘status’);

But this doesn’t work. I get this error:

error php InvalidArgumentException: Cannot set a list with a non-array value. in DrupalCoreTypedDataPluginDataTypeItemList->setValue() (line 59 of /var/www/html/d815/core/lib/Drupal/Core/TypedD

2 Answers

The following reaction rule will do what you asked. Import this configuration then enable the rule at /admin/config/workflow/rules

Note it is not sufficient just to check the current moderation state; if your goal is to show a message when the moderation state is changed to Published then you need to check the state before the update and compare it to the current state - you only want to send the message when the current state is Published and the previous state was NOT Published.

There IS a debug log now in D8 Rules, but it wasn't available when you asked your question.

In the future, please include an export of your Rule - it is impossible to know exactly what you configured wrong without seeing the export.

langcode: en
status: true
dependencies:
  module:
    - content_moderation
  enforced:
    module:
      - content_moderation
id: content_moderation_state_is_changed_to_published
label: 'Show a message when content moderation state is changed to Published'
events:
  -
    event_name: 'rules_entity_update:node'
description: 'This Reaction Rule displays a system message when the moderation state of any piece of content is changed to Published'
tags: {  }
config_version: '3'
expression:
  id: rules_rule
  uuid: 563de663-2c5a-4bad-892d-017e322b67ba
  weight: 0
  conditions:
    id: rules_and
    uuid: c65b7edb-5f6b-41b8-8775-8a880e66b8c0
    weight: 0
    conditions:
      -
        id: rules_condition
        uuid: 384eb508-4970-4210-b427-b9c2d09f25a4
        weight: 0
        context_values:
          operation: '=='
        context_mapping:
          data: node.moderation_state.value
          value: node_unchanged.moderation_state.value
        context_processors:
          operation:
            rules_tokens: {  }
        provides_mapping: {  }
        condition_id: rules_data_comparison
        negate: true
      -
        id: rules_condition
        uuid: 5528f7d4-302c-4a72-b583-943dacb4c117
        weight: 0
        context_values:
          operation: '=='
          value: published
        context_mapping:
          data: node.moderation_state.value
        context_processors:
          operation:
            rules_tokens: {  }
          value:
            rules_tokens: {  }
        provides_mapping: {  }
        condition_id: rules_data_comparison
        negate: false
  actions:
    id: rules_action_set
    uuid: 6cd47db2-335b-4647-8083-b0c0d90b63bf
    weight: 0
    actions:
      -
        id: rules_action
        uuid: 5520606c-4d0f-4af1-9c55-c95f910477ac
        weight: 0
        context_values:
          message: 'Content moderation state was changed to Published'
          type: status
          repeat: false
        context_mapping: {  }
        context_processors:
          message:
            rules_tokens: {  }
          type:
            rules_tokens: {  }
          repeat:
            rules_tokens: {  }
        provides_mapping: {  }
        action_id: rules_system_message

Answered by anonymous on November 23, 2021

Part 1 - Understanding the problem

I'm not sure if you won't run into other problems in trying to get this to work in D8 (as per the current status of the Rules module for D8), but IMO the first issue you need to resolve is that "...Cannot set a list with a non-array value ..." (as contained in your error message).

It seems to me that you should use something like what in D7 is called "List contains item" as your Rules Condition. For way more details on that, refer to the answer to the (D7 related) question about "Loop through all entities with rules".

Part 2 - More info that may help to get it to work

Your issue appears to be related to the (fixed) issue (= a task, not a bug ...), from Klausi, about "Support multiple contexts in the UI" which states:

Currently we only use a simple textfield for contexts in conditions and actions. Conditions such as the UserHasRole condition have their Roles context defined as multiple, which means the user should specify more than one value in a text field. A default implementation could just display a textarea where one line is one item in the list.

The fix for this issue appears to be included in this pull request.

Disclaimer: I have not QA-reviewed this (yet) (but that's what I would go for to get it to work).

Answered by Pierre.Vriens on November 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP