0

I have list integer field in 3 content types and each have same possible values (from 1 to 6) and I need to disable user to choose the same number for two nodes, so each node with this field filled in need to be unique.

I found Unique field module but it is not working for me, because I have field in each content type and I need to look at all nodes from all content types and this module is on content type base.

Basic example of what I need:

Field field_1 from content type type_1 have value 5, so field_2 from content type type_2 and field_3 from content type type_3 can't have value 5.

Any idea how to achieve this?

2
  • If you have 6 possible values shared between 3 content types that's a total of 6 nodes total.. Could you explain why you are doing this because it doesn't make much sense to me. If you wanted to go down your route you can hook into the validation/node creation (sorry cant remember off top) events and query the DB for all nodes on your content types and loop through them and check if it already exists. if exists then throw error, if doesn't proceed with node creation.
    – DMAC
    Sep 4, 2015 at 20:14
  • Are field_1 field_2 and field_3 the same fields used in 3 content types? You could try to use EntityFieldQuery each time a node is updated in a custom validation handler and check if there are other nodes with the same field value. If there are many nodes to compare consider caching.
    – schlicki
    Sep 6, 2015 at 9:23

1 Answer 1

1

Check out this example

function custom_node_validate($node, $form) {
  if ($node->type == 'page') {
    //retrieve the value for your field using Entity API
    $wrapper = entity_metadata_wrapper('node', $node);
    $value = $wrapper->field_collection->field_unique->value();

    //Execute a count query to see if there's other similar values.
    $query = new EntityFieldQuery;
    $query->entityCondition('entity_type', 'field_collection_item')
      ->fieldCondition('field_unique', 'value', $value)
      ->count();
    $count = $query->execute();
    if ($count > 1) {
      form_set_error('field_unique', 'Unique field value is already used.');
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.