diff --git a/src/classes/entities/class-tainacan-field.php b/src/classes/entities/class-tainacan-field.php index 5650489cf..9aef33a14 100644 --- a/src/classes/entities/class-tainacan-field.php +++ b/src/classes/entities/class-tainacan-field.php @@ -370,7 +370,7 @@ class Field extends Entity { $fto = $this->get_field_type_object(); if (is_object($fto)) { - $is_valid = $fto->validate_options($this->get_field_type_options()); + $is_valid = $fto->validate_options($this); } if (true === $is_valid) diff --git a/src/classes/field-types/category/class-tainacan-category.php b/src/classes/field-types/category/class-tainacan-category.php index b32e818e0..9be693fa0 100644 --- a/src/classes/field-types/category/class-tainacan-category.php +++ b/src/classes/field-types/category/class-tainacan-category.php @@ -60,18 +60,49 @@ class Category extends Field_Type { + + +
+ + + + get_option('allow_new_terms')); ?> > + get_status(), apply_filters('tainacan-status-require-validation', ['publish','future','private'])) ) + return true; + + if (empty($this->get_option('taxonomy_id'))) + return ['taxonomy_id' => __('Please select a category', 'tainacan')]; + + global $Tainacan_Fields; + + $category_fields = $Tainacan_Fields->fetch([ + 'collection_id' => $field->get_collection_id(), + 'field_type' => 'Tainacan\\Field_Types\\Category' + ], 'OBJECT'); + + $category_fields = array_map(function ($field) { + $fto = $field->get_field_type_object(); + return $fto->get_option('taxonomy_id'); + }, $category_fields); + + if (in_array($this->get_option('taxonomy_id'), $category_fields)) { + return ['taxonomy_id' => __('You can not have 2 Category Fields using the same category in a collection', 'tainacan')]; + } + return true; - // TODO validate required and unique taxonomy + } /** - * Validate item based on field type categores options + * Validate item based on field type categories options * * @param TainacanEntitiesItem_Metadata_Entity $item_metadata * @return bool Valid or not @@ -88,6 +119,10 @@ class Category extends Field_Type { if (false === $this->get_option('allow_new_terms')) { $terms = $item_metadata->get_value(); + + if (false === $terms) + return true; + if (!is_array($terms)) $terms = array($terms); diff --git a/src/classes/field-types/field-type/class-tainacan-field-type.php b/src/classes/field-types/field-type/class-tainacan-field-type.php index 7de8bda39..b3eede15c 100644 --- a/src/classes/field-types/field-type/class-tainacan-field-type.php +++ b/src/classes/field-types/field-type/class-tainacan-field-type.php @@ -145,10 +145,10 @@ abstract class Field_Type { * * This method should be declared by each field type sub classes * - * @param Array $options Options to be saved as field_type_optins + * @param \Tainacan\Entities\Field $field The field object that is beeing validated * @return true|Array True if optinos are valid. If invalid, returns an array where keys are the field keys and values are error messages. */ - public function validate_options(Array $options) { + public function validate_options(\Tainacan\Entities\Field $field) { return true; } diff --git a/src/classes/field-types/relationship/class-tainacan-relationship.php b/src/classes/field-types/relationship/class-tainacan-relationship.php index 0675275c0..ef2a0982d 100644 --- a/src/classes/field-types/relationship/class-tainacan-relationship.php +++ b/src/classes/field-types/relationship/class-tainacan-relationship.php @@ -98,9 +98,9 @@ class Relationship extends Field_Type { get_option('collection_id')) && !is_numeric($this->get_option('collection_id'))) { return [ 'collection_id' => 'Collection ID invalid' ]; diff --git a/src/classes/repositories/class-tainacan-fields.php b/src/classes/repositories/class-tainacan-fields.php index fd74daaec..430433aa8 100644 --- a/src/classes/repositories/class-tainacan-fields.php +++ b/src/classes/repositories/class-tainacan-fields.php @@ -286,7 +286,9 @@ class Fields extends Repository { 'posts_per_page' => -1, 'post_status' => 'publish' ], $args); - + + $args = $this->parse_fetch_args($args); + $args['post_type'] = Entities\Field::get_post_type(); $wp_query = new \WP_Query($args); diff --git a/tests/test-category-field-types.php b/tests/test-category-field-types.php index d8a7332d3..19e83cb87 100644 --- a/tests/test-category-field-types.php +++ b/tests/test-category-field-types.php @@ -40,10 +40,11 @@ class CategoryFieldTypes extends TAINACAN_UnitTestCase { $field = $this->tainacan_entity_factory->create_entity( 'field', array( - 'name' => 'metadado', - 'description' => 'title', + 'name' => 'meta', + 'description' => 'description', 'collection' => $collection, 'field_type' => 'Tainacan\Field_Types\Category', + 'status' => 'publish', 'field_type_options' => [ 'taxonomy_id' => $tax->get_db_identifier(), 'allow_new_terms' => false @@ -55,10 +56,11 @@ class CategoryFieldTypes extends TAINACAN_UnitTestCase { $field2 = $this->tainacan_entity_factory->create_entity( 'field', array( - 'name' => 'metadado_desc', + 'name' => 'meta2', 'description' => 'description', 'collection' => $collection, - 'field_type' => 'Tainacan\Field_Types\Category' + 'field_type' => 'Tainacan\Field_Types\Category', + 'status' => 'draft', ), true ); @@ -111,6 +113,17 @@ class CategoryFieldTypes extends TAINACAN_UnitTestCase { $check_item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($checkItem, $field); $this->assertEquals('WP_Term', get_class($check_item_metadata->get_value())); + + // test 2 fields with same category + $field2->set_field_type_options([ + 'taxonomy_id' => $tax->get_db_identifier(), + ]); + $field2->set_status('publish'); + + $this->assertFalse($field2->validate(), 'Category Field should not validate when using a category in use by another field in the same collection'); + $errors = $field2->get_errors(); + $this->assertInternalType('array', $errors); + $this->assertArrayHasKey('taxonomy_id', $errors[0]); } } \ No newline at end of file