adds more methods to find core fields

This commit is contained in:
Leo Germani 2018-05-30 15:44:29 -03:00
parent 5d2a9f27ad
commit 02527eef49
4 changed files with 110 additions and 2 deletions

View File

@ -454,6 +454,73 @@ class Collection extends Entity {
return $Tainacan_Fields->fetch_by_collection( $this, [], 'OBJECT' );
}
/**
* Get the two core fields of the collection (title and description)
*
* @return array[\Tainacan\Entities\Field]
*/
function get_core_fields() {
$repo = \Tainacan\Repositories\Fields::get_instance();
return $repo->fetch_by_collection($this, [
'meta_query' => [
[
'key' => 'field_type',
'value' => ['Tainacan\Field_Types\Core_Title', 'Tainacan\Field_Types\Core_Description'],
'compare' => 'IN'
]
]
], 'OBJECT');
}
/**
* Get the Core Title Field for this collection
*
* @return \Tainacan\Entities\Field The Core Title Field
*/
function get_core_title_field() {
$repo = \Tainacan\Repositories\Fields::get_instance();
$results = $repo->fetch_by_collection($this, [
'meta_query' => [
[
'key' => 'field_type',
'value' => 'Tainacan\Field_Types\Core_Title',
]
],
'posts_per_page' => 1
], 'OBJECT');
if (is_array($results) && sizeof($results) == 1 && $results[0] instanceof \Tainacan\Entities\Field) {
return $results[0];
}
return false;
}
/**
* Get the Core Description Field for this collection
*
* @return \Tainacan\Entities\Field The Core Description Field
*/
function get_core_description_field() {
$repo = \Tainacan\Repositories\Fields::get_instance();
$results = $repo->fetch_by_collection($this, [
'meta_query' => [
[
'key' => 'field_type',
'value' => 'Tainacan\Field_Types\Core_Description',
]
],
'posts_per_page' => 1
], 'OBJECT');
if (is_array($results) && sizeof($results) == 1 && $results[0] instanceof \Tainacan\Entities\Field) {
return $results[0];
}
return false;
}
/**
* Set the collection name
*

View File

@ -451,7 +451,7 @@ class Item extends Entity {
*
* Each metadata is a label with the field name and the value.
*
* If an ID, a slug or a Tainacan\Entities\Field object is passed, it returns only one metadata, otherwise
* If an ID, a slug or a Tainacan\Entities\Field object is passed in the 'metadata' argument, it returns only one metadata, otherwise
* it returns all metadata
*
* @param array|string $args {

View File

@ -10,7 +10,7 @@ use \Tainacan\Repositories;
*
* Each metadata is a label with the field name and the value.
*
* If an ID, a slug or a Tainacan\Entities\Field object is passed, it returns only one metadata, otherwise
* If an ID, a slug or a Tainacan\Entities\Field object is passed in 'metadata' parameter, it returns only one metadata, otherwise
* it returns all metadata
*
* @param array|string $args {

View File

@ -170,5 +170,46 @@ class CoreFieldTypes extends TAINACAN_UnitTestCase {
$this->assertFalse($core_description->validate(), 'Core metadata should not validate because it can not allow it to have multiple');
}
function test_collection_getters() {
$Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'test',
),
true
);
$fieldDescription = $this->tainacan_entity_factory->create_entity(
'field',
array(
'name' => 'just to confuse',
'description' => 'description',
'collection' => $collection,
'field_type' => 'Tainacan\Field_Types\Text'
),
true
);
$core_fields = $collection->get_core_fields();
$this->assertEquals(2, sizeof($core_fields));
$this->assertNotEquals('Tainacan\Field_Types\Text', $core_fields[0]->get_field_type());
$this->assertNotEquals('Tainacan\Field_Types\Text', $core_fields[1]->get_field_type());
$title = $collection->get_core_title_field();
$this->assertEquals('Tainacan\Field_Types\Core_Title', $title->get_field_type());
$description = $collection->get_core_description_field();
$this->assertEquals('Tainacan\Field_Types\Core_Description', $description->get_field_type());
}
}