function unique_field_match_value in Unique field 7
Same name and namespace in other branches
- 5 unique_field.module \unique_field_match_value()
- 6 unique_field.module \unique_field_match_value()
Find nodes with a matching field value within a given scope.
1 call to unique_field_match_value()
- unique_field_node_validate in ./
unique_field.module - Implements hook_node_validate().
File
- ./
unique_field.module, line 250 - Provides content validation requirement that a node's author, language, taxonomy terms, or other fields are unique.
Code
function unique_field_match_value($field, $values, $scope, $ntype = NULL, $nlanguage = NULL) {
// Initialize EntityFieldQuery
$entity_type = 'node';
$efq = new EntityFieldQuery();
$efq
->entityCondition('entity_type', $entity_type);
// Add query conditions depending on the type of field
if ($field === UNIQUE_FIELD_FIELDS_TITLE) {
// Query condition for bundle title property
$efq
->propertyCondition('title', $values);
}
elseif ($field === UNIQUE_FIELD_FIELDS_AUTHOR) {
// Query condition for bundle author property
$efq
->propertyCondition('uid', $values);
}
elseif ($field === UNIQUE_FIELD_FIELDS_LANGUAGE) {
// Query condition for bundle language property
$efq
->propertyCondition('language', $values);
}
else {
// Query condition for other fields
$field_info = field_info_field($field);
// Check all items/values
foreach ($values as $value) {
// Check all columns
foreach ($value as $key => $val) {
// Skip values that are not stored in the database
if (!isset($field_info['columns'][$key]) || !is_array($field_info['columns'][$key])) {
continue;
}
// Skip if the value is an empty string or is not a scalar
if (!strlen($val) || !is_scalar($val)) {
continue;
}
$efq
->fieldCondition($field, $key, $val);
}
}
}
// Add query conditions for the scope setting
if ($scope === UNIQUE_FIELD_SCOPE_TYPE && !empty($ntype)) {
// Query condition for the bundle (content type) scope
$efq
->entityCondition('bundle', $ntype);
}
elseif ($scope === UNIQUE_FIELD_SCOPE_LANGUAGE && !empty($nlanguage)) {
// Query condition for the language scope
$efq
->propertyCondition('language', $nlanguage);
}
// Execute query and collect results
$result = $efq
->execute();
$nids = array();
if (isset($result[$entity_type]) && is_array($result[$entity_type])) {
foreach ($result[$entity_type] as $item) {
if (!empty($item->nid)) {
$nids[] = $item->nid;
}
}
}
return array_unique($nids);
}