Skip to main content
replaced http://drupal.stackexchange.com/ with https://drupal.stackexchange.com/
Source Link

You need to implement this in a custom module, here is a sample code to achieve this:

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.');
    }
  }
}

A few disclaimers:

  • I am really stocked by the complexity of Field Field Collection API and it was pretty complicated to find good information about how to retrieve values and make queries.
  • I really enjoy doing SQL queries (see How to change the legnth of field settingsHow to change the legnth of field settings) but I tried to only use the API instead of doing a simple query on the table field_data_field_unique. However it may not yet be perfect so please feel free to chime in.
  • The form_set_error() isn't highliting the field, I was unable to find the correct name.

Here are the documentation I used:

Edit: Following chx advice, to pass directly field_name to fieldCondition().

You need to implement this in a custom module, here is a sample code to achieve this:

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.');
    }
  }
}

A few disclaimers:

  • I am really stocked by the complexity of Field Field Collection API and it was pretty complicated to find good information about how to retrieve values and make queries.
  • I really enjoy doing SQL queries (see How to change the legnth of field settings) but I tried to only use the API instead of doing a simple query on the table field_data_field_unique. However it may not yet be perfect so please feel free to chime in.
  • The form_set_error() isn't highliting the field, I was unable to find the correct name.

Here are the documentation I used:

Edit: Following chx advice, to pass directly field_name to fieldCondition().

You need to implement this in a custom module, here is a sample code to achieve this:

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.');
    }
  }
}

A few disclaimers:

  • I am really stocked by the complexity of Field Field Collection API and it was pretty complicated to find good information about how to retrieve values and make queries.
  • I really enjoy doing SQL queries (see How to change the legnth of field settings) but I tried to only use the API instead of doing a simple query on the table field_data_field_unique. However it may not yet be perfect so please feel free to chime in.
  • The form_set_error() isn't highliting the field, I was unable to find the correct name.

Here are the documentation I used:

Edit: Following chx advice, to pass directly field_name to fieldCondition().

Bounty Ended with 50 reputation awarded by EKK
added 115 characters in body
Source Link
tostinni
  • 9.1k
  • 33
  • 47

You need to implement this in a custom module, here is a sample code to achieve this:

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 Geta fieldcount information
query to see if $fieldthere's =other field_info_field('field_unique');similar values.
    $query = new EntityFieldQuery;
    $query->entityCondition('entity_type', 'field_collection_item')
      ->fieldCondition($field'field_unique', 'value', $value)
      ->count();
    $count = $query->execute();
    if ($count > 1) {
      form_set_error('field_unique', 'Unique field value is already used.');
    }
  }
}

A few disclaimers:

  • I am really stocked by the complexity of Field Field Collection API and it was pretty complicated to find good information about how to retrieve values and make queries.
  • I really enjoy doing SQL queries (see How to change the legnth of field settings) but I tried to only use the API instead of doing a simple query on the table field_data_field_unique. However it may not yet be perfect so please feel free to chime in.
  • The form_set_error() isn't highliting the field, I was unable to find the correct name.

Here are the documentation I used:

Edit: Following chx advice, to pass directly field_name to fieldCondition().

You need to implement this in a custom module, here is a sample code to achieve this:

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();
    
    // Get field information
    $field = field_info_field('field_unique');
    $query = new EntityFieldQuery;
    $query->entityCondition('entity_type', 'field_collection_item')
      ->fieldCondition($field, 'value', $value)
      ->count();
    $count = $query->execute();
    if ($count > 1) {
      form_set_error('field_unique', 'Unique field value is already used.');
    }
  }
}

A few disclaimers:

  • I am really stocked by the complexity of Field API and it was pretty complicated to find good information about how to retrieve values and make queries.
  • I really enjoy doing SQL queries (see How to change the legnth of field settings) but I tried to only use the API instead of doing a simple query on the table field_data_field_unique. However it may not yet be perfect so please feel free to chime in.
  • The form_set_error() isn't highliting the field, I was unable to find the correct name.

Here are the documentation I used:

You need to implement this in a custom module, here is a sample code to achieve this:

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.');
    }
  }
}

A few disclaimers:

  • I am really stocked by the complexity of Field Field Collection API and it was pretty complicated to find good information about how to retrieve values and make queries.
  • I really enjoy doing SQL queries (see How to change the legnth of field settings) but I tried to only use the API instead of doing a simple query on the table field_data_field_unique. However it may not yet be perfect so please feel free to chime in.
  • The form_set_error() isn't highliting the field, I was unable to find the correct name.

Here are the documentation I used:

Edit: Following chx advice, to pass directly field_name to fieldCondition().

Source Link
tostinni
  • 9.1k
  • 33
  • 47

You need to implement this in a custom module, here is a sample code to achieve this:

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();
    
    // Get field information
    $field = field_info_field('field_unique');
    $query = new EntityFieldQuery;
    $query->entityCondition('entity_type', 'field_collection_item')
      ->fieldCondition($field, 'value', $value)
      ->count();
    $count = $query->execute();
    if ($count > 1) {
      form_set_error('field_unique', 'Unique field value is already used.');
    }
  }
}

A few disclaimers:

  • I am really stocked by the complexity of Field API and it was pretty complicated to find good information about how to retrieve values and make queries.
  • I really enjoy doing SQL queries (see How to change the legnth of field settings) but I tried to only use the API instead of doing a simple query on the table field_data_field_unique. However it may not yet be perfect so please feel free to chime in.
  • The form_set_error() isn't highliting the field, I was unable to find the correct name.

Here are the documentation I used: