How to check the value for specified field across all nodes? 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' =&gt; $node));

$query = new EntityFieldQuery();
$query-&gt;entityCondition('entity_type', 'node')
  -&gt;entityCondition('bundle', 'source')
  -&gt;propertyCondition('status', NODE_PUBLISHED)
  -&gt;fieldCondition('faceman', 'faceman_value', '$value',)

$result = $query-&gt;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 &amp; 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-&gt;entityCondition('entity_type', 'node')
-&gt;entityCondition('bundle', 'hannibal')
-&gt;propertyCondition('status', 1)
-&gt;fieldCondition('field_faceman', 'value', $value, '=')
;

// if identical value of field faceman is found, return FALSE to Rules condition

$result = $query-&gt;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 &amp; copy of a Murdock field.
You can make this work by using Rules in combination with the Views Bulk Operations module, as further detailed below.

In what follows, I'm assuming that the machine name of your field is field_optionlist (change to whatever fits).

Step 1 - Create a VBO view

Use the Views Bulk Operations (VBO) module to create a view of all relevant nodes, in which you should:


Add a VBO field to it.
Add a contextual filter for the field_optionlist field for which you want to check that its value is unique. Make sure to correctly configure this contextual filter, similar to what is mentioned in this answer to "How to have a field that counts the nodes submitted by the current user?" (credits to Niall Murphy, merci!).


Include a field like nid for each node (no need for any other fields). Just make sure you get exactly 1 row for each node you want to take into consideration.

Here is an export of such VBO view:

$view = new view();
$view-&gt;name = 'relevant_nodes';
$view-&gt;description = 'Produce a list of node ids that satisfy a specific filter';
$view-&gt;tag = 'default';
$view-&gt;base_table = 'node';
$view-&gt;human_name = 'Relevant nodes';
$view-&gt;core = 7;
$view-&gt;api_version = '3.0';
$view-&gt;disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view-&gt;new_display('default', 'Master', 'default');
$handler-&gt;display-&gt;display_options['title'] = 'Relevant nodes';
$handler-&gt;display-&gt;display_options['use_more_always'] = FALSE;
$handler-&gt;display-&gt;display_options['access']['type'] = 'perm';
$handler-&gt;display-&gt;display_options['cache']['type'] = 'none';
$handler-&gt;display-&gt;display_options['query']['type'] = 'views_query';
$handler-&gt;display-&gt;display_options['exposed_form']['type'] = 'basic';
$handler-&gt;display-&gt;display_options['pager']['type'] = 'full';
$handler-&gt;display-&gt;display_options['pager']['options']['items_per_page'] = '10';
$handler-&gt;display-&gt;display_options['style_plugin'] = 'table';
$handler-&gt;display-&gt;display_options['style_options']['columns'] = array(
  'title' =&gt; 'title',
);
$handler-&gt;display-&gt;display_options['style_options']['default'] = '-1';
$handler-&gt;display-&gt;display_options['style_options']['info'] = array(
  'title' =&gt; array(
    'sortable' =&gt; 0,
    'default_sort_order' =&gt; 'asc',
    'align' =&gt; '',
    'separator' =&gt; '',
    'empty_column' =&gt; 0,
  ),
);
/* Field: Content: Nid */
$handler-&gt;display-&gt;display_options['fields']['nid']['id'] = 'nid';
$handler-&gt;display-&gt;display_options['fields']['nid']['table'] = 'node';
$handler-&gt;display-&gt;display_options['fields']['nid']['field'] = 'nid';
/* Field: Content: Title */
$handler-&gt;display-&gt;display_options['fields']['title']['id'] = 'title';
$handler-&gt;display-&gt;display_options['fields']['title']['table'] = 'node';
$handler-&gt;display-&gt;display_options['fields']['title']['field'] = 'title';
$handler-&gt;display-&gt;display_options['fields']['title']['label'] = '';
$handler-&gt;display-&gt;display_options['fields']['title']['alter']['word_boundary'] = FALSE;
$handler-&gt;display-&gt;display_options['fields']['title']['alter']['ellipsis'] = FALSE;
/* Field: Content: OptionList */
$handler-&gt;display-&gt;display_options['fields']['field_optionlist']['id'] = 'field_optionlist';
$handler-&gt;display-&gt;display_options['fields']['field_optionlist']['table'] = 'field_data_field_optionlist';
$handler-&gt;display-&gt;display_options['fields']['field_optionlist']['field'] = 'field_optionlist';
/* Field: Bulk operations: Content */
$handler-&gt;display-&gt;display_options['fields']['views_bulk_operations']['id'] = 'views_bulk_operations';
$handler-&gt;display-&gt;display_options['fields']['views_bulk_operations']['table'] = 'node';
$handler-&gt;display-&gt;display_options['fields']['views_bulk_operations']['field'] = 'views_bulk_operations';
$handler-&gt;display-&gt;display_options['fields']['views_bulk_operations']['vbo_settings']['display_type'] = '0';
$handler-&gt;display-&gt;display_options['fields']['views_bulk_operations']['vbo_settings']['enable_select_all_pages'] = 1;
$handler-&gt;display-&gt;display_options['fields']['views_bulk_operations']['vbo_settings']['row_clickable'] = 1;
$handler-&gt;display-&gt;display_options['fields']['views_bulk_operations']['vbo_settings']['force_single'] = 0;
$handler-&gt;display-&gt;display_options['fields']['views_bulk_operations']['vbo_settings']['entity_load_capacity'] = '10';
/* Contextual filter: Content: OptionList (field_optionlist) */
$handler-&gt;display-&gt;display_options['arguments']['field_optionlist_value']['id'] = 'field_optionlist_value';
$handler-&gt;display-&gt;display_options['arguments']['field_optionlist_value']['table'] = 'field_data_field_optionlist';
$handler-&gt;display-&gt;display_options['arguments']['field_optionlist_value']['field'] = 'field_optionlist_value';
$handler-&gt;display-&gt;display_options['arguments']['field_optionlist_value']['default_action'] = 'default';
$handler-&gt;display-&gt;display_options['arguments']['field_optionlist_value']['default_argument_type'] = 'raw';
$handler-&gt;display-&gt;display_options['arguments']['field_optionlist_value']['default_argument_options']['index'] = '0';
$handler-&gt;display-&gt;display_options['arguments']['field_optionlist_value']['summary']['number_of_records'] = '0';
$handler-&gt;display-&gt;display_options['arguments']['field_optionlist_value']['summary']['format'] = 'default_summary';
$handler-&gt;display-&gt;display_options['arguments']['field_optionlist_value']['summary_options']['items_per_page'] = '25';
$handler-&gt;display-&gt;display_options['arguments']['field_optionlist_value']['limit'] = '0';
/* Filter criterion: Content: Published */
$handler-&gt;display-&gt;display_options['filters']['status']['id'] = 'status';
$handler-&gt;display-&gt;display_options['filters']['status']['table'] = 'node';
$handler-&gt;display-&gt;display_options['filters']['status']['field'] = 'status';
$handler-&gt;display-&gt;display_options['filters']['status']['value'] = 1;
$handler-&gt;display-&gt;display_options['filters']['status']['group'] = 1;
$handler-&gt;display-&gt;display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Content: Type */
$handler-&gt;display-&gt;display_options['filters']['type']['id'] = 'type';
$handler-&gt;display-&gt;display_options['filters']['type']['table'] = 'node';
$handler-&gt;display-&gt;display_options['filters']['type']['field'] = 'type';
$handler-&gt;display-&gt;display_options['filters']['type']['value'] = array(
  'article' =&gt; 'article',
);

/* Display: Page */
$handler = $view-&gt;new_display('page', 'Page', 'page');
$handler-&gt;display-&gt;display_options['path'] = 'relevant-nodes';


Step 2 - Create a Rule

Create a rule like so:


Rules Event: something like "Before saving content of type Article" seems most appropriate, though you can use any variation that fits.
Rules Condition: "Check number of results returned by a VBO view". And select the VBO view created in the previous step. In doing so, you will be asked to "specify arguments to pass to the view, one per line. You may use token replacement patterns.". The token to be entered there is node:field-optionlist. If the number of results is 1 or more, then you know the value already exists. So within Minimum number of results you should enter the value 1.
Rules Action: this is pretty much up to your own imagination. For example if the Rules Condition is TRUE, you could use "Set a data value", to restore the value of that field to what it was before, i.e. node-unchanged:field-optionlist. Maybe also display an appropriate message, such as (when trying to change the value from Advanced to Basic):


  The value "Basic" has already been used in at least 1 other node ... (field value remains "Advanced").



Here is an export of a Rule that does exactly what is described above (if you have the Rules UI enabled, and a field with the machine name field_optionlist, you should be able to just import this rule):

{ "rules_prevent_multiple_nodes_with_same_field_value" : {
    "LABEL" : "Prevent multiple nodes with same field value",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "views_bulk_operations", "rules" ],
    "ON" : { "node_presave--article" : { "bundle" : "article" } },
    "IF" : [
      { "views_bulk_operations_condition_result_count" : {
          "view" : "relevant_nodes|page",
          "args" : [ "node:field-optionlist" ],
          "minimum" : "1"
        }
      }
    ],
    "DO" : [
      { "drupal_message" : {
          "message" : "The value \u0022[node:field-optionlist]\u0022 has already been used in at least 1 other node ... (field value remains \u0022[node-unchanged:field-optionlist]\u0022).",
          "type" : "error"
        }
      },
      { "data_set" : {
          "data" : [ "node:field-optionlist" ],
          "value" : [ "node-unchanged:field-optionlist" ]
        }
      }
    ]
  }
}


Note that my Rules Event is limited to "Article" (change to whatever fits), and replace (prior to trying to import this rule) the machine name of field_optionlist to whatever other machine name your field.

Done!