Help setting up Rule with Delete entity error

Created on 28 December 2022, over 1 year ago

Problem/Motivation

I would like to create a Rule that deletes all entities that match a given value in a field when content is updated. I was able to get most of the way there, but am running ino a problem I can't figure out how to resolve.

Steps to reproduce

I was able to use a "Fetch entities by field" action to get all nodes that match my criteria. Unfortunately, without loop working yet in D9, I had to set up multiple "Delete entity actions", calling fetched entity separately (e.g. fetched_entity.0, fetched_entity.1). This works, if there are exactly the number of entities found that match the number of actions I've set up. However, if there is no entity found for one of the Delete entity actions, the rule errors out. I can't predict how many entities will be found, although there should be between 0 and 2 at any point.

Drupal\Core\Entity\EntityStorageException: Unable to apply data selector '0' at '0': Unable to apply data selector '0' at '0' in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 815 of /code/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

Proposed resolution

Is there a way to handle the error, so that if no matching entity is found, the action is just skipped?

Is there a patch available that would let me use a loop that I could try (I looked, but did not find)?

Any other suggestions?

Here is an export of my rule, if that's helpful.

uuid: 9ba0ddee-46e5-4bc9-ac41-878d59621bb3
langcode: en
status: true
dependencies: {  }
id: update_robot_schedules_clone
label: 'Delete Robot Schedules'
events:
  -
    event_name: 'rules_entity_update:node--match'
description: ''
tags: {  }
config_version: '3'
expression:
  id: rules_rule
  uuid: 75f416a1-b33d-4ddb-876e-3bc56576ff2b
  weight: 0
  conditions:
    id: rules_and
    uuid: 739227b4-1b6b-42e1-a6e1-a353ebf2e8f2
    weight: 0
    conditions:
      -
        id: rules_condition
        uuid: d6f09fe1-bf25-4cbb-8086-38ab1a499163
        weight: 0
        condition_id: rules_entity_is_of_bundle
        negate: false
        context_values:
          type: node
          bundle: match
        context_mapping:
          entity: node
        context_processors:
          type:
            rules_tokens: {  }
          bundle:
            rules_tokens: {  }
        provides_mapping: {  }
  actions:
    id: rules_action_set
    uuid: 1f4373b0-0c5a-4edf-a1be-27bdabf26402
    weight: 0
    actions:
      -
        id: rules_action
        uuid: edb1a1d5-ecd6-4c8a-b278-b61f3501654b
        weight: -50
        action_id: rules_entity_fetch_by_field
        context_values:
          type: node
          field_name: field_match_schedule_
          field_value: '{{ node.nid }}'
          limit: '2'
        context_mapping: {  }
        context_processors:
          type:
            rules_tokens: {  }
          field_name:
            rules_tokens: {  }
          field_value:
            rules_tokens: {  }
          limit:
            rules_tokens: {  }
        provides_mapping:
          entity_fetched: existing_schedules
      -
        id: rules_action
        uuid: fbee7734-60c2-4b7c-a3ab-6f4fa90a6694
        weight: -48
        action_id: rules_entity_delete
        context_values: {  }
        context_mapping:
          entity: existing_schedules.0
        context_processors: {  }
        provides_mapping: {  }
      -
        id: rules_action
        uuid: 47d75108-97b4-47fe-8676-162771bcc4a3
        weight: -47
        action_id: rules_entity_delete
        context_values: {  }
        context_mapping:
          entity: existing_schedules.1
        context_processors: {  }
        provides_mapping: {  }

πŸ’¬ Support request
Status

Active

Version

3.0

Component

Rules Core

Created by

πŸ‡ΊπŸ‡ΈUnited States michellezeedru

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.

Production build 0.71.5 2024