How do I delete a relation in rules. I have a rules that creates a relation when a user flags a node. I want to create a Rules that deletes the relation upon unclicking the flag. I can't figure out how to fetch the relation ID or anything. Even fetching property won't work because it won't accept any values.

I'll note that the Unique check box also isn't working as duplicate relations are added even though I have that checked.

Thank you so much. I love the work you guys are doing.

Michael

Comments

erikstarck’s picture

Category: feature » support

I'm having a similar problem and I'm getting this message in the error log:

2.822 ms Evaluating the action entity_delete. [edit]
7.161 ms There is no information about how to delete entities of type relation.

I guess you have to use the Views Bulk Operations-module. Will give it a try.

UPDATE: OK, I added a field to the array in relation_entity_info()
'deletion callback' => 'relation_delete'

Now I can delete using an action.

Erik Starck

chx’s picture

Category: support » feature
chx’s picture

Category: support » feature
Status: Active » Fixed

Yes, that's what I figured too and committed yesterday.

prodigeek’s picture

Can you elaborate on the change you made? I'm not completely sure how to implement what you described.

chx’s picture

relation action delete an entity of type relation just works.

erikstarck’s picture

That's good but now I just upgraded to beta2 and my old actions stopped working. In fact, Drupal crashes.

The web server log says:

Call to undefined function ctools_export_crud_load_multiple() in /var/www/drupal/sites/all/modules/relation/relation.module on line 397, referer: [...]/node/49/edit?destination=admin/content

I get the same error from the new block used to edit relations just when I select the relation type.

The version of Chaos Tools I have installed is the latest RC.

(Sorry if this is getting slightly OT.)

Update: that function is in the latest dev version of Chaos Tools.

Update2: Yupp, installing latest dev of ctools fixed the problem.

drush dl ctools-7.x-1.x-dev





BR Erik

chx’s picture

I am changing the project page to warn you that you need ctools dev for this. Cant help they didnt release for so long.

prodigeek’s picture

Sorry, I still need some clarification on how to delete the relation.

How do I get rules to find the relation, specifically, using the trigger of Unflagging a node. User unflags a node, I want to delete the relation that rules created before (When the flag was initially triggered).

I've tried loading related entities, but that just gives me end points that does seem to accomplish anything (that I can figure out). I've tried just removing an endpoint, but that didn't work either.

Thanks so much for the help.

Michael

chx’s picture

That's a feature request there, but one that I am not inclined to do. Use Views + VBO for that one.

MStrzelecki_’s picture

You can use user->relation or node->relation relationship in views then add rid field and hide it. Then in rules list should be current-item:rid but there is not.

prodigeek’s picture

I'm so sorry I kinda keep asking the same question. Basically, I need a bit more help with the step by steps of grabbing the relations list and then doing anything with it. I tried loading it through a rules set, but I don't know what to do with it. I tried fetching UID from the endpoints, but the endpoints are invalid. Similarly, when I load a VBO, I get a series of errors (see below), and when I try actions upon it, it again, won't check against endpoints and won't make fields in the VBO available to me.

Ideally, I'd want to check the UID and NID to see if that pair exists and delete that relation. I understand this is complicated and am happy to take this off-line into email or IM.

I greatly appreciate your help and advice and thank you for your time.
Michael

naught101’s picture

relation_query('user', $uid)
  ->related('node', $nid)
  ->execute()

will return the ids of all the relation between those two entities. You can restrict it to a relation type with

  ->entityCondition('bundle', $relation_type)

you can also add an index to the relation_query() or related() call if you need a specific direction. see http://drupalcode.org/project/relation.git/blob/refs/heads/7.x-1.x:/rela... for docs

prodigeek’s picture

Thank you this code is awesome, though I still have to ask, what do I do with it. I tried just putting it in Custom PHP code in Rules, but again, I'm not sure what to do with it next.

I am asking for a good deal of hand holding. I'm super thankful for the help you've given. I know this is possible and I'm just not experienced enough to be able to figure it out. Again, I thank you all for your help.

Michael

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

C-3PO’s picture

Component: Code » API
Status: Closed (fixed) » Active

I am sorry to reopen the issue. It seems that everyone is happy with the solution, but I just can't figure it out. Drupal.org is where all development takes place and I don't really hope to find a solution elsewhere.

I' m a mere site builder, I know little about PHP and I am very fascinated with all the fun stuff that can be done using Relation and Rules module. Sorry I don't know what to do with the code that closed the issue. Ok, I'm trying to configure VBO view. Instead of one single relation to be deleted I get a list of all relations of this type. Once arguments don't get passed to the VBO, it looks that I need to use the rules to select one particular relation.

Could anyone please please take a few minutes to explain how to do it? The idea is to delete a relation between a node that is being unflagged and and a user who unflags it. I believe there are many site builders that could do with this advise.
Thank you so much for the understanding!

mahmost’s picture

Title: Deleting relation in rules » Fetch Relation by Endpoints in rules
StatusFileSize
new1.35 KB

It is not about deleting a relation .. It is about querying relations using endpoints, and it is still not working in latest dev, the problem is that there is no "query callback" for the property endpoints.

naught101 made a nice starting point in #12, so I tried to make things work with this initial patch.

It worked for my use case and I didn't make a lot of testing for other ones.

mahmost’s picture

Status: Active » Needs review
mikran’s picture

Status: Needs review » Needs work

* - queryable: (optional) Whether a property is queryable with
* EntityFieldQuery. Defaults to TRUE if a 'schema field' is specified, or
* if the deprecated 'query callback' is set to
* 'entity_metadata_field_query'. Otherwise it defaults to FALSE.
* - query callback: (deprecated) A callback for querying for entities
* having the given property value. See entity_property_query().
* Generally, properties should be queryable via EntityFieldQuery. If
* that is the case, just set 'queryable' to TRUE.

This is from hook_entity_property_info documentation.

mikavirt’s picture

Version: 7.x-1.x-dev » 7.x-1.0-rc4

Hi,

I'm a bit lost with the whole thing. I'm also trying to fetch a relation id in order to delete the entity, but I can't. My relations link users to some nodes, and I have relation rc4.
When I implement a "rid" (relation id) field in views, it returns 0 (or null I guess). Same with a relation type field...

How did things evolved since january? How could I manage to get this ID? Would the dev version be a better choice?

Thank you very much!

Michael

miromarchi’s picture

I'm also a non programmer. I tried #12 and #16, without any success.
I wish to fetch a relation by the endpoints, having the endpoint-list.
Exactly tha same stated in issue How can I get a relation by the endpoints using the “Fetch entity by property” action in rules? by ptmkenny.
Have anyone succeed in it?
Thank you very much

marcusx’s picture

Issue summary: View changes
StatusFileSize
new369.58 KB

Even if it is deprecated patch #16 is working fine.

@miromachi

Here is what to do:
1. Create a list of any entiy
2. Put your endpoints in the list
3. Use the endpoint list to fetch the relation

Here is a demo rule export that works with patch #16

{ "rules_fetch_relation_with_endpoints_demo" : {
    "LABEL" : "fetch_relation_with_endpoints_demo",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "devel" ],
    "DO" : [
      { "entity_fetch" : {
          "USING" : { "type" : "eventday", "id" : "1" },
          "PROVIDE" : { "entity_fetched" : { "endpoint_1" : "Endpoint 1" } }
        }
      },
      { "entity_fetch" : {
          "USING" : { "type" : "node", "id" : "350" },
          "PROVIDE" : { "entity_fetched" : { "endpoint_2" : "Endpoint 2" } }
        }
      },
      { "variable_add" : {
          "USING" : { "type" : "list\u003Centity\u003E", "value" : [ "" ] },
          "PROVIDE" : { "variable_added" : { "endpoints" : "Endpoints" } }
        }
      },
      { "list_add" : { "list" : [ "endpoints" ], "item" : [ "endpoint-1" ] } },
      { "list_add" : { "list" : [ "endpoints" ], "item" : [ "endpoint-2" ] } },
      { "entity_query" : {
          "USING" : {
            "type" : "relation",
            "property" : "endpoints",
            "value" : [ "endpoints" ]
          },
          "PROVIDE" : { "entity_fetched" : { "realtion_fetched" : "Relation Fetched" } }
        }
      },
      { "devel_debug" : { "value" : [ "realtion-fetched" ] } }
    ]
  }
}

And a screenshot of the rule:

Screenshot Rules UI

Hope this helps.

miromarchi’s picture

@marcusx That's it! Thanks so much! It works now. Your 3 steps workflow in #21 is working for me. Thanks

miromarchi’s picture

Working with "fetch relation by endpoints" action in rules, I found out that it fetches all the relations for the endpoints in the chosen list, even if the list contains only one endpoint.

In this (sub)case, the action fetches all relations pointing to the selected endpoint, whatever the other endpoint.

I believe this behavior is very similar to the action fetch entity by property used with entityreference module. I previously used that one to fetch all nodes referencing a chosen node.

Since I wasn't aware of this ability of the relation module, and indeed I thought only entityreference module had it, I write it here, for others to know.

Paul B’s picture

Status: Needs work » Needs review
StatusFileSize
new2.03 KB

I've tried to implement this functionality without the deprecated query_callback by using hook_entity_query_alter similar to the patch for Commerce in #1345890: Allow to query order by states from Rules.

Here it is.

miromarchi’s picture

Hi there. I've tried your patch in #24 and it works.
The only point I got relation reporting a "field validation failed" error. Which doesn't mess up with my rule, nor with the expected behavior.
Rules log says nothing.
Watchdog just says
error relation Field validation failed.
Sorry I don't know how to help better.
Anybody else?

Edit: my field validation error was totally unrelated to this issue.

mikran’s picture

Version: 7.x-1.0-rc4 » 7.x-1.x-dev
Status: Needs review » Needs work

This needs tests

Ravenight’s picture

I manually patched #24 into 7.x-1.0-rc4 and it worked for me without any messages in watchdog. Thank you so much for this as I was going crazy trying to get this to work.

Yuri’s picture

With regard to the 'fetched' state of the relation:
It appears that "fetch relation by endpoints" only makes the rid available, not the custom fields of the relation through 'entity has field'.
Only after another 'fetch entity by id' then 'entity has field' will show the custom field.
Is this normal behavior or a bug?

Paul B’s picture

"Fetch entity by property" returns a list of entities, not one entity. If you add a loop over the list, you can add an 'entity has field' check on the current list item in the loop.

jantoine’s picture

#24 works great with the latest 7.x-1.x-dev.

gvincke’s picture

Using 7.x-1.0-rc5 nor patch #16 neither #24 worked for me, following method described in #21.
Still unable to fetch relations using one endpoint.
Some advices ?

Patching using patch #24 ( git apply relation-query_endpoints-1302788-24.patch ) gave me :
relation-query_endpoints-1302788-24.patch:13: new blank line at EOF.
+
warning: 1 line adds whitespace errors.

Do you have explanations ?

Thanks !!

EDIT : #16 worked, but not #24. First fail was due to PHP cache unrefreshed. So my fault :(

Anonymous’s picture

StatusFileSize
new48.44 KB

First comment here on drupal.org.

Thanks for the interesting Relations module :)

Figured a way to work around the problem. Needs Conditional Rules-module to work.

My case was to separate friends (relation type "friends"). Relation was created with Flags. Separating friends happens when either user Unflags the friendship.

My relation has one extra field "uids" which is Integer-type of field. uids = flagging-user[uid]+flagged-user[uid].
Value is added by the rule that creates the friend-relation.

Logic:
Unflag the flagging user from flagged user >> calculate [uid]+[uid] >> loop through flagging-users friends list calculating [uid]+[uid] from all friends on list >> compare result to first calculation-result >> if match is found, fetch entity by property(uids_field value) and delete entity. >> else, "sorry, problems"

Here is the rule:

{ "rules_relation_separate_friends" : {
    "LABEL" : "relation - separate friends",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "flag", "rules", "rules_conditional" ],
    "ON" : { "flag_unflagged_flag_as_friend" : [] },
    "DO" : [
      { "flag_unflaguser" : {
          "flag" : "flag_as_friend",
          "user" : [ "flagging-user" ],
          "flagging_user" : [ "flagged-user" ],
          "permission_check" : 1
        }
      },
      { "data_calc" : {
          "USING" : {
            "input_1" : [ "flagged-user:uid" ],
            "op" : "+",
            "input_2" : [ "flagging-user:uid" ]
          },
          "PROVIDE" : { "result" : { "result" : "Calculation result" } }
        }
      },
      { "LOOP" : {
          "USING" : { "list" : [ "flagging-user:relation-flagged-friend-user" ] },
          "ITEM" : { "list_item" : "Current list item" },
          "DO" : [
            { "data_calc" : {
                "USING" : {
                  "input_1" : [ "flagged-user:uid" ],
                  "op" : "+",
                  "input_2" : [ "flagging-user:uid" ]
                },
                "PROVIDE" : { "result" : { "result_uids_from_own_friends" : "Uids from own friends result" } }
              }
            },
            { "CONDITIONAL" : [
                {
                  "IF" : { "data_is" : { "data" : [ "result-uids-from-own-friends" ], "value" : [ "result" ] } },
                  "DO" : [
                    { "entity_query" : {
                        "USING" : {
                          "type" : "relation",
                          "property" : "field_uids",
                          "value" : [ "result-uids-from-own-friends" ],
                          "limit" : "1"
                        },
                        "PROVIDE" : { "entity_fetched" : { "relation_by_uids_fetched" : "Fetched relation by uids" } }
                      }
                    },
                    { "entity_delete" : { "data" : [ "relation-by-uids-fetched:0" ] } }
                  ]
                },
                { "ELSE" : [
                    { "drupal_message" : {
                        "message" : "Something went wrong here, sorry about that. For some reason we were unable to remove your friendship relation. Please contact site admin.",
                        "type" : "error"
                      }
                    }
                  ]
                }
              ]
            }
          ]
        }
      },
      { "drupal_message" : { "message" : "You are no longer friends with [flagged-user:name]" } }
    ]
  }
}

Rule fetch relation printscreen

Found this issue after multiple trials and errors, didn't try either of the patches.
Thought I'll post this here if it helps anyone with the same problem.

Please let me know if my logic is all wrong there ^^

Paul B’s picture

Status: Needs work » Needs review
StatusFileSize
new2.03 KB

Re-roll of the patch in #24

miromarchi’s picture

I've recently updated my relation 7.x-1.0-rc4 and rules 7.x-2.6 modules to the latests (relation 7.x-1.0 and rules 7.x-2.9).
I've reapplyed the patch in #33.

patch -p1 < relation-query_endpoints-1302788-33.patch 
patching file relation.module
Hunk #2 succeeded at 1012 with fuzz 2 (offset 16 lines).

I'm writing here to inform that my rules fire correctly and the website is working well.
Anyway the patch has some fuzz and offset against latest relation module.

Thanks once again for the patch!

WidgetsBurritos’s picture

Works perfectly for me as well.

oksana-c’s picture

Applied patch #33 to Relation 7.x-1.0. And followed steps below from #21 to fetch the relation:
1. Create a list of any entiy
2. Put your endpoints in the list
3. Use the endpoint list to fetch the relation.

Happily reporting that relation is fetched successfully and fields that are attached to the relation are accessible within the rule.
BIG thank you for the patch!

javiprada1’s picture

Hello!!!

I am very happy because thanks to this issues I solved the following problem:

expects parameter 1 to be array, null given en rules_action_entity_query

Thanks oksana-c to simplify the soluction, thanks Paul B for share with us the patch.

Now, I can finish the new rules.
Regards, Javier.

:)

mzvast’s picture

Thank you guys!Your patch #33 solved my problem
PS:don't forget to clear cache after patch!

socialnicheguru’s picture

Status: Needs review » Reviewed & tested by the community
mikran’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new2.01 KB

I still think this needs tests. However here is a reroll, current patch did not apply anymore.

Why it is not using the related() method of RelationQuery?