Trigger Rule with Filefield 'when field has changed' in Drupal 7

862 views Asked by At

I've got a set of different rules that check if various field types have been updated 'after updating existing content'. The problem is that each one works fine, except for the only filefield type which will not work. The condition used is a 'NOT Data comparison' on the field checking the 'node-unchanged' version against the new 'node' version. This works with every other type of field and actions appropriately (that I have used at least), just not the filefield type; the rule just fires regardless of changes or not on the filefield.

I also found this post about a very similar problem: https://www.drupal.org/node/1011014#comment-10040082

I think I have the makings of a workaround, but I just wanted to check this with some fellow developers first as my PHP isn't the best.

If I were to enable the PHP module then add a condition in rules that checks the 'source' attribute to see if a new file has been added... would this work? The code I have is:

if (isset($object->field_FILEFIELD_NAME[0]['source'])) { //Check for new files }

I believe that $object is the node passed on by rule function as argument.

Is this a good idea/best approach? Any ideas or workarounds would be great.

1

There are 1 answers

1
TheodorosPloumis On

Yes, you can use PHP for filefield checks as it is more clear (but always less secure). Here are my suggestions:

  1. Create a Rule component (of type "Condition set (AND)") instead of a rule to do this check.
  2. Use a parameter with your rule component (of type Node) so you have the $node available.
  3. Use php to do the checks. Get the file data from the filefield using $node.
  4. Create a normal rule (eg with event Before Saving Content) where you will use this component as a condition among others.

As you said you need to check for differences not for empty values, right? So instead of using isset you need to see if there is a different fid (which normally changes when there is a different file). Other methods are available and if you want to see which data can change do a dpm() to the filefield using devel module.

In order to get the unchanged value of the filefield use the same component on you main Rule but with the $unchanged node as parameter.