include disabled fields info in api response

This commit is contained in:
Leo Germani 2018-03-12 10:44:20 -03:00
parent 772890f193
commit 495cd9b64b
4 changed files with 38 additions and 10 deletions

View File

@ -281,6 +281,7 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
$form = ob_get_clean(); $form = ob_get_clean();
$item_arr['edit_form'] = $form; $item_arr['edit_form'] = $form;
$item_arr['field_type_object'] = $item->get_field_type_object(); $item_arr['field_type_object'] = $item->get_field_type_object();
$item_arr['disabled'] = $item->get_disabled_for_collection();
} }
return $item_arr; return $item_arr;
@ -299,6 +300,10 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
$collection_id = $request['collection_id']; $collection_id = $request['collection_id'];
$args = $this->prepare_filters( $request ); $args = $this->prepare_filters( $request );
if ($request['context'] === 'edit') {
$args['include_disabled'] = true;
}
$collection = new Entities\Collection( $collection_id ); $collection = new Entities\Collection( $collection_id );

View File

@ -11,7 +11,9 @@ class Field extends Entity {
// Collection getter and setter declared here // Collection getter and setter declared here
use \Tainacan\Traits\Entity_Collection_Relation; use \Tainacan\Traits\Entity_Collection_Relation;
public $disabled_for_collection = false;
protected static $post_type = 'tainacan-field'; protected static $post_type = 'tainacan-field';
/** /**
* {@inheritDoc} * {@inheritDoc}
@ -323,6 +325,21 @@ class Field extends Entity {
function set_field_type_options( $value ){ function set_field_type_options( $value ){
$this->set_mapped_property('field_type_options', $value); $this->set_mapped_property('field_type_options', $value);
} }
/**
* Transient property used to store the status of the field for a particular collection
*
* Used by the API to tell front end when a field is disabled
*
*/
public function get_disabled_for_collection() {
return $this->disabled_for_collection;
}
public function set_disabled_for_collection($value) {
$this->disabled_for_collection = $value;
}
// helpers // helpers

View File

@ -333,7 +333,7 @@ class Fields extends Repository {
return $this->order_result( return $this->order_result(
$this->fetch( $args, $output ), $this->fetch( $args, $output ),
$collection, $collection,
isset( $args['disabled_fields'] ) ? $args['disabled_fields'] : false isset( $args['include_disabled'] ) ? $args['include_disabled'] : false
); );
} }
@ -344,10 +344,10 @@ class Fields extends Repository {
* *
* @param $result Response from method fetch * @param $result Response from method fetch
* @param Entities\Collection $collection * @param Entities\Collection $collection
* @param bool $disabled_fields Disabled fields wont appear on list collection fields * @param bool $include_disabled Wether to include disabled fields in the results or not
* @return array or WP_Query ordinate * @return array or WP_Query ordinate
*/ */
public function order_result( $result, Entities\Collection $collection, $disabled_fields = false ){ public function order_result( $result, Entities\Collection $collection, $include_disabled = false ){
$order = $collection->get_fields_order(); $order = $collection->get_fields_order();
if($order) { if($order) {
$order = ( is_array($order) ) ? $order : unserialize($order); $order = ( is_array($order) ) ? $order : unserialize($order);
@ -363,9 +363,11 @@ class Fields extends Repository {
if( $index !== false ) { if( $index !== false ) {
// skipping fields disabled if the arg is set // skipping fields disabled if the arg is set
if( $disabled_fields && !$order[$index]['enable'] ){ if( !$include_disabled && !$order[$index]['enable'] ) {
continue; continue;
} } elseif ($include_disabled && !$order[$index]['enable']) {
$item->set_disabled_for_collection(true);
}
$result_ordinate[$index] = $item; $result_ordinate[$index] = $item;
} else { } else {

View File

@ -259,10 +259,14 @@ class Fields extends TAINACAN_UnitTestCase {
$update_collection = $Tainacan_Collections->update( $collection ); $update_collection = $Tainacan_Collections->update( $collection );
$fields_ordinate = $Tainacan_Fields->fetch_by_collection( $update_collection, [], 'OBJECT' ); $fields_ordinate = $Tainacan_Fields->fetch_by_collection( $update_collection, [], 'OBJECT' );
$this->assertEquals( 'field3', $fields_ordinate[0]->get_name() ); $this->assertEquals( 'field2', $fields_ordinate[0]->get_name() );
$fields_ordinate_enabled = $Tainacan_Fields->fetch_by_collection( $update_collection, [ 'disabled_fields' => true ], 'OBJECT' ); $fields_ordinate_enabled = $Tainacan_Fields->fetch_by_collection( $update_collection, [ 'include_disabled' => true ], 'OBJECT' );
$this->assertEquals( 'field2', $fields_ordinate_enabled[0]->get_name() ); $this->assertEquals( 'field3', $fields_ordinate_enabled[0]->get_name() );
$this->assertTrue($fields_ordinate_enabled[0]->get_disabled_for_collection());
$this->assertFalse($fields_ordinate_enabled[1]->get_disabled_for_collection());
$this->assertFalse($fields_ordinate_enabled[2]->get_disabled_for_collection());
} }
function test_unique_slugs() { function test_unique_slugs() {