Hello,

I have a node content type with a line_item reference field.
On a line item deletion event, i need to delete all the nodes referencing the line_item being deleted.

So I have the following rule :

{ "rules_synchronize_stock_entries_line_item_deleted" : {
    "LABEL" : "Synchronize stock entries (Line item deleted)",
    "PLUGIN" : "reaction rule",
    "TAGS" : [ "stock" ],
    "REQUIRES" : [ "rules", "entity" ],
    "ON" : [ "commerce_line_item_delete" ],
    "IF" : [
      { "NOT data_is_empty" : { "data" : [ "commerce-line-item:order" ] } },
      { "OR" : [
          { "data_is" : { "data" : [ "commerce-line-item:order:state" ], "value" : "completed" } },
          { "data_is" : { "data" : [ "commerce-line-item:order:state" ], "value" : "pending" } }
        ]
      }
    ],
    "DO" : [
      { "entity_query" : {
          "USING" : {
            "type" : "node",
            "property" : "field_stock_entry_line_item",
            "value" : [ "commerce-line-item" ]
          },
          "PROVIDE" : { "entity_fetched" : { "entity_fetched" : "Fetched entity" } }
        }
      },
      { "LOOP" : {
          "USING" : { "list" : [ "entity-fetched" ] },
          "ITEM" : { "list_item" : "Current list item" },
          "DO" : [ { "entity_delete" : { "data" : [ "list-item" ] } } ]
        }
      }
    ]
  }
}

It seems like the "Fetch entity by property" doesn't manage to properly query on the line item reference field.

In entity.property.inc in rules, the function entity_property_query() action function calls
return $info['query callback']($entity_type, $property, $value, $limit);

but $info['query callback'] is set to FALSE by this line earlier in the code, because $properties[$property]['schema field'] doesn't exist
$info = $properties[$property] + array('type' => 'text', 'query callback' => isset($properties[$property]['schema field']) ? 'entity_metadata_table_query' : FALSE);

and $properties is filled up with entity_get_all_property_info() which gathers information for the entity and its bundles
$properties = entity_get_all_property_info($entity_type);

Could this issue be related to #1356006: Fix entity property info of product / customer profile / line item reference fields or #1306106: Can't load nodes via a product reference in a rule ?
Any help is appreciated !

Thanks

Comments

liupascal’s picture

If it can help here is the rule log

pcambra’s picture

Status: Active » Needs review
StatusFileSize
new658 bytes

Can you try if it works with this patch?

liupascal’s picture

it LOOKS like it's not changing anything, I have to do more testing.

I'm starting to think that my rule is not correctly configured ...
Can I fetch all the entity which field value is equal to a variable, then loop over that list of entities and delete them ?

In code it would look like this :

//This is the equivalent of what i am trying to do with my fetch entity by property action
$query = new EntityFieldQuery();
$query
  ->entityCondition('entity_type', 'node', '=')
  ->entityCondition('bundle', 'stock_entry', '=') // This is not taken into account in my current rule setting, but it should still work
  ->fieldCondition('field_stock_entry_line_item', 'line_item_id', $line_item_id_being_delete, '=')
  ->execute();

//Then something like this
foreach ($query as result) 
  entity_delete('node', $result->nid);
}

Does this rule export seems to do the same thing as this code ?

liupascal’s picture

Never mind #3 first line - it is working.
The patch does fix the issue. I still need to work on the rules though...

pcambra’s picture