I have minimal Drupal and PHP experience ...
Problem:
I have an autocreate function for a Content Type ('hannibal'), which has one defined field type ('faceman'). When Content Type 'hannibal' is created, it is done automatically through rules by passing the value for field 'murdock' from another content type. 'murdock' and 'faceman' are sometimes identical values (they are also identical formats: text, same formatting, direct copies).
I need to check across all other nodes of type 'hannibal', to see if the value for 'faceman' already exists in that field. I have Unique Field module active, but because the nodes are being created through Rules, it is not functioning in this case (perhaps it only works when in a user entered form).
Issue: A method to check field 'faceman' for the value being entered, in all nodes of type 'hannibal'.
I've already tried a LOT of options for this:
Rules - I have tried setting conditions, but can't find a way to load all nodes of type 'hannibal' to check that field value. I've tried to do it in PHP using snippets taken from elsewhere, but no luck. Also tried setting a rules component, but couldn't make sense of it.
Database query - I'm using SQLite. I've identified the table to check, and tried an EntityFieldQuery but the code I've tried as a condition is just crashing the page:
$value = token_replace('[node:field_murdock]', array('node' => $node));
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'source')
->propertyCondition('status', NODE_PUBLISHED)
->fieldCondition('faceman', 'faceman_value', '$value',)
$result = $query->execute();
if (isset($result['node'])) {
return TRUE;
}
So long as I can create the nodes automatically, and prevent new nodes being created if they have an identical value to a pre-existing value for field_faceman, I'm not that bothered about how it is done (within reason!).
This seems like such a basic function (checking all the values of a given field in the database), I must be missing an easier way to do this. Right?
Any help?
UPDATE & SOLUTION
Managed to solve it with EntityFieldQuery in the end. I'm convinced I can't be the only person with this issue, so for anyone of around my skill level in the same position in future:
Solution is via Rules using the PHP conditional.
Rules trigger is save content event. Conditional 1 is "content has 'faceman' field". Conditional 2 is Execute Custom PHP as below. No link between the two conditionals - trying an AND function broke them.
Custom PHP code:
// display murdock field taken from saved content as plaintext
$value = token_replace('[node:field_murdock]');
// search database for identical values of field faceman
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'hannibal')
->propertyCondition('status', 1)
->fieldCondition('field_faceman', 'value', $value, '=')
;
// if identical value of field faceman is found, return FALSE to Rules condition
$result = $query->execute();
if (isset($result['node'])) {
return FALSE;
}
else {
return TRUE;
}
Therefore the action part of the Rule will only be executed if the value is not already present in the Faceman field somewhere in another node. So long as your existing node types Hannibal have fields Faceman with equivalent values / formats, then a duplicate node will not be created.
My actions rules then create the Faceman field in the new content by directly copying field Murdock from the saved content which triggered the Rule. This ensures that the above logic works, and the node should only be created once, because Faceman will always be a child & copy of a Murdock field.