From fb2ae8c4116c2f5b6cc3acdc7ca1a4284728327a Mon Sep 17 00:00:00 2001 From: thierrydallacroce Date: Mon, 10 Jun 2019 15:34:04 -0700 Subject: [PATCH] Improve interface to choose referenceable properties (#139) --- .../dkan_api/src/Routing/RouteProvider.php | 13 +------ .../config/install/dkan_data.settings.yml | 25 +++++++++++--- .../src/Form/DkanDataSettingsForm.php | 34 ++++++++++++++++--- .../custom/dkan_data/src/ValueReferencer.php | 13 +------ .../tests/src/Unit/ValueReferencerTest.php | 18 +++++----- 5 files changed, 63 insertions(+), 40 deletions(-) diff --git a/modules/custom/dkan_api/src/Routing/RouteProvider.php b/modules/custom/dkan_api/src/Routing/RouteProvider.php index f13436ea94..db8513a2b1 100644 --- a/modules/custom/dkan_api/src/Routing/RouteProvider.php +++ b/modules/custom/dkan_api/src/Routing/RouteProvider.php @@ -19,18 +19,7 @@ class RouteProvider { */ public function getPropertyList() { $list = \Drupal::config('dkan_data.settings')->get('property_list'); - if ($list) { - // Trim and split list on newlines whether Windows, MacOS or Linux. - return preg_split( - '/\s*\r\n\s*|\s*\r\s*|\s*\n\s*/', - trim($list), - -1, - PREG_SPLIT_NO_EMPTY - ); - } - else { - return []; - } + return array_values(array_filter($list)); } /** diff --git a/modules/custom/dkan_data/config/install/dkan_data.settings.yml b/modules/custom/dkan_data/config/install/dkan_data.settings.yml index 230817fad9..587f8c8a98 100644 --- a/modules/custom/dkan_data/config/install/dkan_data.settings.yml +++ b/modules/custom/dkan_data/config/install/dkan_data.settings.yml @@ -1,4 +1,21 @@ -property_list: | - theme - keyword - organization +property_list: + 'theme': 'theme' + 'keyword': 'keyword' + 'publisher' : 'publisher' + 'contactPoint': 0 + '@type': 0 + 'title': 0 + 'identifier': 0 + 'description': 0 + 'accessLevel': 0 + 'accrualPeriodicity': 0 + 'describedBy': 0 + 'describedByType': 0 + 'distribution': 0 + 'issued': 0 + 'license': 0 + 'modified': 0 + 'references': 0 + 'spatial': 0 + 'temporal': 0 + 'isPartOf': 0 diff --git a/modules/custom/dkan_data/src/Form/DkanDataSettingsForm.php b/modules/custom/dkan_data/src/Form/DkanDataSettingsForm.php index a6d46e94fd..bd167188e6 100644 --- a/modules/custom/dkan_data/src/Form/DkanDataSettingsForm.php +++ b/modules/custom/dkan_data/src/Form/DkanDataSettingsForm.php @@ -34,15 +34,41 @@ public function getFormId() { */ public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config('dkan_data.settings'); + $options = $this->retrieveSchemaProperties(); + $default_values = $config->get('property_list'); $form['property_list'] = [ - '#type' => 'textarea', - '#title' => $this->t('List of dataset properties to be referenced'), - '#description' => $this->t('Separate properties by a new line.'), - '#default_value' => $config->get('property_list'), + '#type' => 'checkboxes', + '#title' => $this->t('List of dataset properties with referencing and API endpoint'), + '#options' => $options, + '#default_value' => $default_values, ]; return parent::buildForm($form, $form_state); } + /** + * @return array + * List of schema properties' title and description. + */ + public function retrieveSchemaProperties() : array { + // Create a json object from our schema. + $schemaRetriever = \Drupal::service('dkan_schema.schema_retriever'); + $schema = $schemaRetriever->retrieve('dataset'); + $schema_object = json_decode($schema); + + // Build a list of the schema properties' title and description. + $property_list = []; + foreach ($schema_object->properties as $property_id => $property_object) { + if (isset($property_object->title)) { + $property_list[$property_id] = "{$property_object->title} ({$property_id})"; + } + else { + $property_list[$property_id] = ucfirst($property_id); + } + } + + return $property_list; + } + /** * {@inheritdoc} */ diff --git a/modules/custom/dkan_data/src/ValueReferencer.php b/modules/custom/dkan_data/src/ValueReferencer.php index 1f9d6bb2d1..c4781c28be 100644 --- a/modules/custom/dkan_data/src/ValueReferencer.php +++ b/modules/custom/dkan_data/src/ValueReferencer.php @@ -388,18 +388,7 @@ protected function emptyPropertyOfSameType($data) { */ protected function getPropertyList() : array { $list = $this->configService->get('dkan_data.settings')->get('property_list'); - if ($list) { - // Trim and split list on newlines whether Windows, MacOS or Linux. - return preg_split( - '/\s*\r\n\s*|\s*\r\s*|\s*\n\s*/', - trim($list), - -1, - PREG_SPLIT_NO_EMPTY - ); - } - else { - return []; - } + return array_values(array_filter($list)); } } diff --git a/modules/custom/dkan_data/tests/src/Unit/ValueReferencerTest.php b/modules/custom/dkan_data/tests/src/Unit/ValueReferencerTest.php index cfd805686e..678039ead1 100644 --- a/modules/custom/dkan_data/tests/src/Unit/ValueReferencerTest.php +++ b/modules/custom/dkan_data/tests/src/Unit/ValueReferencerTest.php @@ -382,15 +382,17 @@ public function testGetPropertyList() { $this->writeProtectedProperty($mock, 'configService', $mockConfigService); - $list = "theme - some property - yet another property - - "; + $list = [ + "contactPoint" => "contactPoint", + "theme" => "theme", + "keyword" => "keyword", + "other properties" => 0, + "not interested in" => 0, + ]; $expected = [ - 'theme', - 'some property', - 'yet another property', + "contactPoint", + "theme", + "keyword", ]; // Expect.