Skip to content

Commit

Permalink
Improve interface to choose referenceable properties (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
thierrydallacroce authored and fmizzell committed Jun 10, 2019
1 parent be785f0 commit fb2ae8c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 40 deletions.
13 changes: 1 addition & 12 deletions modules/custom/dkan_api/src/Routing/RouteProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
25 changes: 21 additions & 4 deletions modules/custom/dkan_data/config/install/dkan_data.settings.yml
Original file line number Diff line number Diff line change
@@ -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
34 changes: 30 additions & 4 deletions modules/custom/dkan_data/src/Form/DkanDataSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down
13 changes: 1 addition & 12 deletions modules/custom/dkan_data/src/ValueReferencer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

}
18 changes: 10 additions & 8 deletions modules/custom/dkan_data/tests/src/Unit/ValueReferencerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit fb2ae8c

Please sign in to comment.