diff --git a/src/classes/entities/class-tainacan-item-metadata-entity.php b/src/classes/entities/class-tainacan-item-metadata-entity.php index 13927969f..0130b6fa0 100644 --- a/src/classes/entities/class-tainacan-item-metadata-entity.php +++ b/src/classes/entities/class-tainacan-item-metadata-entity.php @@ -486,10 +486,35 @@ class Item_Metadata_Entity extends Entity { // if its required, at least one must be filled $one_filled = false; - $valid = true; - foreach($value as $val) { - if (!empty($val)) - $one_filled = true; + $valid = true; + $dupe_array = array(); + foreach($value as $val) { + if (!empty($val)) + $one_filled = true; + + if ($this->is_collection_key()) { + if (++$dupe_array[$val] > 1) { + $this->add_error( 'key_exists', sprintf( __('%s is a collection key and there is another item with the same value', 'tainacan'), $metadatum->get_name() ) ); + return false; + } + + $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); + $test = $Tainacan_Items->fetch([ + 'meta_query' => [ + [ + 'key' => $this->metadatum->get_id(), + 'value' => $val + ] + ], + 'post__not_in' => [$item->get_id()] + ], $item->get_collection()); + + if ($test->have_posts()) { + // translators: %s = metadatum name. ex: Register ID is a collection key and there is another item with the same value + $this->add_error( 'key_exists', sprintf( __('%s is a collection key and there is another item with the same value', 'tainacan'), $metadatum->get_name() ) ); + return false; + } + } } if ($this->is_required() && !$one_filled) { @@ -505,7 +530,7 @@ class Item_Metadata_Entity extends Entity { } $this->set_as_valid(); - return true; + return true; } else { // translators: %s = metadatum name. ex: Title is invalid $this->add_error( 'invalid', sprintf( __('%s is invalid', 'tainacan'), $metadatum->get_name() ) ); @@ -540,7 +565,7 @@ class Item_Metadata_Entity extends Entity { } $this->set_as_valid(); - return true; + return true; } } } \ No newline at end of file