category field type backend
This commit is contained in:
parent
e69f003de7
commit
fdd6203df2
|
@ -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)
|
||||
|
|
|
@ -60,18 +60,49 @@ class Category extends Field_Type {
|
|||
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</td>
|
||||
<td>
|
||||
<label><?php echo __('Allow creation of new terms','tainacan'); ?></label><br/>
|
||||
<small><?php echo __('If checked, users may create new terms for this category, otherwise they can only selected from existing terms.','tainacan'); ?></small>
|
||||
</td>
|
||||
<td>
|
||||
<input type="checkbox" name="allow_new_terms" <?php checked(true, $this->get_option('allow_new_terms')); ?> >
|
||||
<label>Allow</label>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function validate_options(Array $options) {
|
||||
public function validate_options(\Tainacan\Entities\Field $field) {
|
||||
|
||||
if ( !in_array($field->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);
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -98,9 +98,9 @@ class Relationship extends Field_Type {
|
|||
<?php
|
||||
}
|
||||
|
||||
public function validate_options(Array $options) {
|
||||
public function validate_options(\Tainacan\Entities\Field $field) {
|
||||
// TODO: This is just a sample validation to test validation workflow for field types. Must redo it
|
||||
if (isset($options['collection_id']) && !is_numeric($options['collection_id'])) {
|
||||
if (!empty($this->get_option('collection_id')) && !is_numeric($this->get_option('collection_id'))) {
|
||||
return [
|
||||
'collection_id' => 'Collection ID invalid'
|
||||
];
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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]);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue