diff --git a/src/api/endpoints/class-tainacan-rest-fields-controller.php b/src/api/endpoints/class-tainacan-rest-fields-controller.php index ac35638f0..ed5266abe 100644 --- a/src/api/endpoints/class-tainacan-rest-fields-controller.php +++ b/src/api/endpoints/class-tainacan-rest-fields-controller.php @@ -281,6 +281,7 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller { $form = ob_get_clean(); $item_arr['edit_form'] = $form; $item_arr['field_type_object'] = $item->get_field_type_object(); + $item_arr['disabled'] = $item->get_disabled_for_collection(); } return $item_arr; @@ -299,6 +300,10 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller { $collection_id = $request['collection_id']; $args = $this->prepare_filters( $request ); + + if ($request['context'] === 'edit') { + $args['include_disabled'] = true; + } $collection = new Entities\Collection( $collection_id ); diff --git a/src/classes/entities/class-tainacan-field.php b/src/classes/entities/class-tainacan-field.php index 5245f0561..37a5638bb 100644 --- a/src/classes/entities/class-tainacan-field.php +++ b/src/classes/entities/class-tainacan-field.php @@ -11,7 +11,9 @@ class Field extends Entity { // Collection getter and setter declared here use \Tainacan\Traits\Entity_Collection_Relation; - + + public $disabled_for_collection = false; + protected static $post_type = 'tainacan-field'; /** * {@inheritDoc} @@ -323,6 +325,21 @@ class Field extends Entity { function set_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 diff --git a/src/classes/repositories/class-tainacan-fields.php b/src/classes/repositories/class-tainacan-fields.php index 20acea042..8ab0a1ecb 100644 --- a/src/classes/repositories/class-tainacan-fields.php +++ b/src/classes/repositories/class-tainacan-fields.php @@ -333,7 +333,7 @@ class Fields extends Repository { return $this->order_result( $this->fetch( $args, $output ), $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 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 */ - 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(); if($order) { $order = ( is_array($order) ) ? $order : unserialize($order); @@ -363,9 +363,11 @@ class Fields extends Repository { if( $index !== false ) { // skipping fields disabled if the arg is set - if( $disabled_fields && !$order[$index]['enable'] ){ - continue; - } + if( !$include_disabled && !$order[$index]['enable'] ) { + continue; + } elseif ($include_disabled && !$order[$index]['enable']) { + $item->set_disabled_for_collection(true); + } $result_ordinate[$index] = $item; } else { diff --git a/tests/test-fields.php b/tests/test-fields.php index bb7691122..3509d7c25 100644 --- a/tests/test-fields.php +++ b/tests/test-fields.php @@ -259,10 +259,14 @@ class Fields extends TAINACAN_UnitTestCase { $update_collection = $Tainacan_Collections->update( $collection ); $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' ); - $this->assertEquals( 'field2', $fields_ordinate_enabled[0]->get_name() ); + $fields_ordinate_enabled = $Tainacan_Fields->fetch_by_collection( $update_collection, [ 'include_disabled' => true ], 'OBJECT' ); + $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() {