node_unchanged the context 'entity' in 'rules_entity_is_of_bundle' requires a value.

Created on 8 February 2023, over 1 year ago
Updated 16 August 2023, 11 months ago

Problem/Motivation

Drupal 9.5.3
php 8.1.14
rules dev-3.x 9595fca

Steps to reproduce

I add a condition: entity is of bundle
entity: node_unchanged
type: node
value: mycontent_type_id

Then when I save a new node I get these error messages

Type: node
Drupal\rules\Exception\EvaluationException: The context for 'entity' is NULL, but the context 'entity' in 'rules_entity_is_of_bundle' requires a value. in Drupal\rules\Plugin\RulesExpression\ConditionExpression->prepareContext() (line 96 of /var/www/d9/web/modules/contrib/rules/src/Context/ContextHandlerTrait.php)

Type: php
Drupal\Core\Entity\EntityStorageException: The context for 'entity' is NULL, but the context 'entity' in 'rules_entity_is_of_bundle' requires a value. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 815 of /var/www/d9/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ’¬ Support request
Status

Fixed

Version

3.0

Component

Conditions

Created by

πŸ‡ΊπŸ‡ΈUnited States gscerb

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

Comments & Activities

  • Issue created by @gscerb
  • Status changed to Postponed: needs info over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States TR Cascadia

    What is your event? Please post an export of your rule so we can see how it is configured.

  • πŸ‡ΊπŸ‡ΈUnited States gscerb

    The actual bundle is different from my original post which I tried to simplify. It is "race_pdf_txt_xls_txt"

    uuid: 58ee75da-b482-4c88-bd26-47227c64451f
    langcode: en
    status: true
    dependencies: { }
    id: email_staff_change
    label: 'Email staff change'
    events:
    -
    event_name: 'rules_entity_presave:node--race_pdf_txt_xls_txt'
    description: ''
    tags: { }
    config_version: '3'
    expression:
    id: rules_rule
    uuid: afeae29e-8e8a-44ae-993e-ee107623c21c
    weight: 0
    conditions:
    id: rules_and
    uuid: 05f3e980-a292-435d-958a-80832b382191
    weight: 0
    conditions:
    -
    id: rules_condition
    uuid: 30c24ebe-a39e-4d06-beff-b053db63c540
    weight: 0
    condition_id: rules_entity_is_of_bundle
    negate: false
    context_values:
    type: node
    bundle: race_pdf_txt_xls_txt
    context_mapping:
    entity: node
    context_processors:
    type:
    rules_tokens: { }
    bundle:
    rules_tokens: { }
    provides_mapping: { }
    -
    id: rules_condition
    uuid: b2e6ccbd-6740-4ad4-96bc-3d6098f85b71
    weight: 0
    condition_id: rules_entity_is_of_bundle
    negate: false
    context_values:
    type: node
    bundle: race_pdf_txt_xls_txt
    context_mapping:
    entity: node_unchanged
    context_processors:
    type:
    rules_tokens: { }
    bundle:
    rules_tokens: { }
    provides_mapping: { }
    -
    id: rules_condition
    uuid: 5d913850-f698-4bd3-aa0a-594dc3f763cc
    weight: 0
    condition_id: rules_data_comparison
    negate: true
    context_values:
    operation: CONTAINS
    context_mapping:
    data: node_unchanged.field_staff.target_id
    value: node.field_staff.target_id
    context_processors:
    operation:
    rules_tokens: { }
    provides_mapping: { }
    actions:
    id: rules_action_set
    uuid: 1aa91da2-2574-4161-ab87-ae3ae72c2218
    weight: 0
    actions:
    -
    id: rules_action
    uuid: d9fc3b1c-68e1-4557-ab34-77ece46e8177
    weight: 0
    action_id: rules_system_message
    context_values:
    message: 'email sent'
    type: status
    repeat: false
    context_mapping: { }
    context_processors:
    message:
    rules_tokens: { }
    type:
    rules_tokens: { }
    repeat:
    rules_tokens: { }
    provides_mapping: { }
    -
    id: rules_action
    uuid: 75a6d0f3-3f9e-41b3-8757-a7235a14a56c
    weight: 0
    action_id: rules_send_email
    context_values:
    to:
    - '{{ node.field_staff.0.entity.mail }}'
    - '{{ node.field_staff.1.entity.mail }}'
    - '{{ node.field_staff.2.entity.mail }}'
    - '{{ node.field_staff.3.entity.mail }}'
    - '{{ node.field_staff.4.entity.mail }}'
    - '{{ node.field_staff.5.entity.mail }}'
    - '{{ node.field_staff.6.entity.mail }}'
    - '{{ node.field_staff.7.entity.mail }}'
    - '{{ node.field_staff.8.entity.mail }}'
    - '{{ node.field_staff.9.entity.mail }}'
    subject: 'Staff additions to {{ node.title.value }} - {{ node.field_date1.value }}'
    message: 'Staff additions to {{ node.title.value }} - {{ node.field_date1.value }}

    http://gsrs.com/node/{{ node.nid }}'
    reply: ''
    language: ''
    context_mapping: { }
    context_processors:
    to:
    rules_tokens: { }
    subject:
    rules_tokens: { }
    message:
    rules_tokens: { }
    reply:
    rules_tokens: { }
    language:
    rules_tokens: { }
    provides_mapping: { }

  • πŸ‡ΊπŸ‡ΈUnited States gscerb

    Here is the 2nd rule.

    uuid: 6b03a8fc-c615-41b8-b6a4-31d2d2ed60f0
    langcode: en
    status: true
    dependencies: { }
    id: race_list_initialed
    label: 'Race list initialed'
    events:
    -
    event_name: 'rules_entity_presave:node--race_pdf_txt_xls_txt'
    description: ''
    tags: { }
    config_version: '3'
    expression:
    id: rules_rule
    uuid: b77039d6-f5fc-4fa8-91e9-db173edcffe2
    weight: 0
    conditions:
    id: rules_and
    uuid: 75ffbe36-8c5b-4e44-846f-b72f32b445f9
    weight: 0
    conditions:
    -
    id: rules_condition
    uuid: 7d340fdb-8ac8-414e-9b4b-90eec6e80ef0
    weight: -50
    condition_id: rules_entity_is_of_bundle
    negate: false
    context_values:
    type: node
    bundle: race_pdf_txt_xls_txt
    context_mapping:
    entity: node
    context_processors:
    type:
    rules_tokens: { }
    bundle:
    rules_tokens: { }
    provides_mapping: { }
    -
    id: rules_condition
    uuid: 0f73999f-4a12-49b8-adb7-dcb4e8e9302e
    weight: -49
    condition_id: rules_entity_is_of_bundle
    negate: false
    context_values:
    type: node
    bundle: race_pdf_txt_xls_txt
    context_mapping:
    entity: node_unchanged
    context_processors:
    type:
    rules_tokens: { }
    bundle:
    rules_tokens: { }
    provides_mapping: { }
    -
    id: rules_condition
    uuid: 8ec98379-6f59-43ea-abbb-7043be96c48c
    weight: -48
    condition_id: rules_data_comparison
    negate: false
    context_values:
    operation: '=='
    value: '1'
    context_mapping:
    data: node.field_electrical_power.value
    context_processors:
    operation:
    rules_tokens: { }
    value:
    rules_tokens: { }
    provides_mapping: { }
    -
    id: rules_condition
    uuid: 88c749a7-7995-477a-9db7-73ea7da8cf96
    weight: -47
    condition_id: rules_data_comparison
    negate: false
    context_values:
    operation: '=='
    value: '1'
    context_mapping:
    data: node.field_terms_and_conditions.value
    context_processors:
    operation:
    rules_tokens: { }
    value:
    rules_tokens: { }
    provides_mapping: { }
    -
    id: rules_condition
    uuid: 9d58df06-5324-489e-a150-82a252e8cd25
    weight: -46
    condition_id: rules_data_comparison
    negate: false
    context_values:
    operation: '=='
    value: '1'
    context_mapping:
    data: node.field_email_list_ok.value
    context_processors:
    operation:
    rules_tokens: { }
    value:
    rules_tokens: { }
    provides_mapping: { }
    -
    id: rules_condition
    uuid: c4485610-8e02-46a5-a394-1b62888efc7d
    weight: -44
    condition_id: rules_data_is_empty
    negate: true
    context_values: { }
    context_mapping:
    data: node.field_enter_your_initials_to_acc.value
    context_processors: { }
    provides_mapping: { }
    -
    id: rules_condition
    uuid: 8bb2bfbb-5f8a-4439-85cc-0513c5d1c8fb
    weight: 0
    condition_id: rules_data_comparison
    negate: true
    context_values:
    operation: '=='
    context_mapping:
    data: node_unchanged.field_terms_and_conditions.value
    value: node.field_terms_and_conditions.value
    context_processors:
    operation:
    rules_tokens: { }
    provides_mapping: { }
    actions:
    id: rules_action_set
    uuid: f7edf033-77a4-42d1-ba02-67364b626bd8
    weight: 0
    actions:
    -
    id: rules_action
    uuid: 77b4c529-6612-4974-9fab-a5490899424c
    weight: 0
    action_id: rules_system_message
    context_values:
    message: 'message: race initialed email sent'
    type: status
    repeat: false
    context_mapping: { }
    context_processors:
    message:
    rules_tokens: { }
    type:
    rules_tokens: { }
    repeat:
    rules_tokens: { }
    provides_mapping: { }
    -
    id: rules_action
    uuid: 1286e5d8-f16a-40df-9c8d-3a05738a9ca6
    weight: 0
    action_id: rules_send_email
    context_values:
    to:
    - gscerb@gmail.com
    subject: '{{ node.title }} has been initialed. http://gsrs.com/node/{{ node.nid }}'
    message: '{{ node.title }} has been initialed. http://gsrs.com/node/{{ node.nid }}'
    reply: greg@bluebassdesign.com
    language: ''
    context_mapping: { }
    context_processors:
    to:
    rules_tokens: { }
    subject:
    rules_tokens: { }
    message:
    rules_tokens: { }
    reply:
    rules_tokens: { }
    language:
    rules_tokens: { }
    provides_mapping: { }

  • πŸ‡ΊπŸ‡ΈUnited States TR Cascadia

    So here, you're reacting on the event "Before saving a content item entity of type ..."

    This event adds two context variables: node and node_unchanged

    The node_unchanged variable has a value only when the node already exists. In that case, the node context variable contains the values you're about to save, and the node_unchanged context variable contains the previous values that the node had when it was read in from the database.

    If you have a new node, it won't have previous values, and node_unchanged will be empty.

    I'm not sure why you're using node_unchanged to test the entity bundle - it seems to be you could just as easily use node. Likewise, I'm not sure why you're using "Before saving" instead of "After saving" or "After updating". It seems to me if you want to notify people of *changes* then you only want to fire on updated nodes, not on new nodes - that implies "After updating" to me.

    With "Before saving", and in fact with ALL the entity events, Rules uses core Drupal entity hooks directly. This means that Rules events have the same restrictions as core Drupal entity hooks. In the case of "Before saving", this allows you to "Act on an entity before it is created or updated." with the caveat that "You can get the original entity object from $entity->original when it is an update of the entity.". With Rules, $entity->original is stored in node_unchanged, which is only available for an updated node. See https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21...

  • πŸ‡ΊπŸ‡ΈUnited States gscerb

    Changing the reaction rule to "After updating a content item entity" did the trick.

    I appreciate your help!

  • Status changed to Fixed 11 months ago
  • πŸ‡ΊπŸ‡ΈUnited States TR Cascadia
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024