Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config option in metastore settings to stop forcing redirect #4381

Open
wants to merge 20 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/metastore/config/install/metastore.settings.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
redirect_to_datasets: true
csv_headers_mode: resource_headers
property_list:
'theme': 'theme'
Expand Down
3 changes: 3 additions & 0 deletions modules/metastore/config/schema/metastore.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ metastore.settings:
resource_perspective_display:
type: string
label: 'Resource download url display'
redirect_to_datasets:
type: boolean
label: 'Redirect to datasets view after form submit'
13 changes: 13 additions & 0 deletions modules/metastore/metastore.install
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,16 @@ function metastore_update_8009() {
modules/contrib/dkan/schema/collections/data-dictionary.json over you local
site version before attempting to read or write any data dictionaries.");
}

/**
* Set the default value for the redirect_to_datasets setting.
*/
function metastore_update_8010() {
$config = \Drupal::configFactory()->getEditable('metastore.settings');

if ($config->get('redirect_to_datasets') === NULL) {
$config->set('redirect_to_datasets', TRUE)->save();
}

drupal_flush_all_caches();
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,12 @@ function metastore_admin_form_alter(&$form, FormStateInterface $form_state, $for
* Submit handler to redirect after node save to dkan dataset content page.
*/
function metastore_admin_form_submit($form, FormStateInterface &$form_state) {
$form_state->setRedirect('view.dkan_dataset_content.page_1');
$redirect_to_datasets = \Drupal::config('metastore.settings')->get('redirect_to_datasets');

// If the setting is enabled, redirect to the dataset content page.
if ($redirect_to_datasets) {
$form_state->setRedirect('view.dkan_dataset_content.page_1');
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php

namespace Drupal\metastore_admin\Tests\Functional;

use Drupal\Tests\BrowserTestBase;

/**
* Test the redirect_to_datasets functionality.
*
* This test ensures that when the "Redirect to datasets view after form submit"
* setting is enabled, users are redirected to the dataset listing page after
* submitting a dataset form. When disabled, users remain on the dataset node page.
*
* @group dkan
* @group metastore
* @group metastore_admin
* @group functional
*/
class RedirectToDatasetsTest extends BrowserTestBase {

protected static $modules = [
'dkan',
'metastore',
'metastore_admin',
'node',
];

protected $defaultTheme = 'stark';

/**
* @todo Remove this when we drop support for Drupal 10.0.
*/
protected $strictConfigSchema = FALSE;

/**
* Tests dataset form submission when redirect_to_datasets is enabled.
*/
public function testRedirectEnabled() {
/** @var \Drupal\metastore\MetastoreService $metastore_service */
$metastore_service = $this->container->get('dkan.metastore.service');

$this->drupalLogin(
// @todo Figure out least possible admin permissions.
$this->drupalCreateUser([], NULL, TRUE)
);
$assert = $this->assertSession();

// 07_admin_dataset_json_form.spec.js : User can create and edit a dataset
// with the json form UI.
//
// Since we don't have JavaScript, we can't use select2 or select_or_other
// to add publisher or keyword entities. We create them here with arbitrary
// UUIDs so that we can post the names to the form.
$publisher_name = uniqid();
$metastore_service->post('publisher',
$metastore_service->getValidMetadataFactory()->get(
json_encode((object) [
'identifier' => '9deadc2f-50e0-512a-af7c-4323697d530d',
'data' => ['name' => $publisher_name],
]), 'publisher', ['method' => 'POST'])
);
// We need a keyword.
$keyword_data = uniqid();
$metastore_service->post('keyword',
$metastore_service->getValidMetadataFactory()->get(json_encode((object) [
'identifier' => '05b2e74a-eb23-585b-9c1c-4d023e21e8a5',
'data' => $keyword_data,
]), 'keyword', ['method' => 'POST'])
);

// Enable redirect option.
$this->config('metastore.settings')
->set('redirect_to_datasets', TRUE)
->save();

// Create new dataset, populate required fields.
$this->drupalGet('node/add/data');
$assert->statusCodeEquals(200);

$dataset_title = 'DKANTEST dataset title';
$this->submitForm([
'edit-field-json-metadata-0-value-title' => $dataset_title,
'edit-field-json-metadata-0-value-description' => 'DKANTEST dataset description.',
'edit-field-json-metadata-0-value-accesslevel' => 'public',
'edit-field-json-metadata-0-value-modified-date' => '2020-02-02',
'edit-field-json-metadata-0-value-publisher-publisher-name' => $publisher_name,
'edit-field-json-metadata-0-value-contactpoint-contactpoint-fn' => 'DKANTEST Contact Name',
'edit-field-json-metadata-0-value-contactpoint-contactpoint-hasemail' => 'dkantest@test.com',
'edit-field-json-metadata-0-value-keyword-keyword-0' => $keyword_data,
], 'Save');

// Assert that the redirect happened.
$assert->statusCodeEquals(200);
$assert->addressEquals('admin/dkan/datasets');
$assert->pageTextContains('Data ' . $dataset_title . ' has been created.');
}

/**
* Tests dataset form submission when redirect_to_datasets is disabled.
*/
public function testRedirectDisabled() {
/** @var \Drupal\metastore\MetastoreService $metastore_service */
$metastore_service = $this->container->get('dkan.metastore.service');

$this->drupalLogin(
// @todo Figure out least possible admin permissions.
$this->drupalCreateUser([], NULL, TRUE)
);
$assert = $this->assertSession();

// 07_admin_dataset_json_form.spec.js : User can create and edit a dataset
// with the json form UI.
//
// Since we don't have JavaScript, we can't use select2 or select_or_other
// to add publisher or keyword entities. We create them here with arbitrary
// UUIDs so that we can post the names to the form.
$publisher_name = uniqid();
$metastore_service->post('publisher',
$metastore_service->getValidMetadataFactory()->get(
json_encode((object) [
'identifier' => '9deadc2f-50e0-512a-af7c-4323697d530d',
'data' => ['name' => $publisher_name],
]), 'publisher', ['method' => 'POST'])
);
// We need a keyword.
$keyword_data = uniqid();
$metastore_service->post('keyword',
$metastore_service->getValidMetadataFactory()->get(json_encode((object) [
'identifier' => '05b2e74a-eb23-585b-9c1c-4d023e21e8a5',
'data' => $keyword_data,
]), 'keyword', ['method' => 'POST'])
);


// Disable redirect option.
$this->config('metastore.settings')
->set('redirect_to_datasets', FALSE)
->save();

$this->drupalGet('node/add/data');
$assert->statusCodeEquals(200);

$dataset_title_no_redirect = 'No Redirect Dataset';
$this->submitForm([
'edit-field-json-metadata-0-value-title' => $dataset_title_no_redirect,
'edit-field-json-metadata-0-value-description' => 'No Redirect Dataset Description.',
'edit-field-json-metadata-0-value-accesslevel' => 'public',
'edit-field-json-metadata-0-value-modified-date' => '2020-02-02',
'edit-field-json-metadata-0-value-publisher-publisher-name' => $publisher_name,
'edit-field-json-metadata-0-value-contactpoint-contactpoint-fn' => 'DKANTEST Contact Name',
'edit-field-json-metadata-0-value-contactpoint-contactpoint-hasemail' => 'dkantest@test.com',
'edit-field-json-metadata-0-value-keyword-keyword-0' => $keyword_data,
], 'Save');

// Assert that the user lands on the dataset node page (not redirected).
$assert->statusCodeEquals(200);
$assert->addressMatches('/node\/\d+$/');
$assert->pageTextContains('Data ' . $dataset_title_no_redirect . ' has been created.');
}

}
92 changes: 74 additions & 18 deletions modules/metastore/src/Form/DkanDataSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\metastore\Form;

use Drupal\Core\Config\Config;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteBuilderInterface;
Expand Down Expand Up @@ -74,49 +75,104 @@ public function getFormId() {
}

/**
* Inherited.
*
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('metastore.settings');

$form['description'] = [
'#markup' => $this->t(
'Configure the metastore settings.'
),
$form['description'] = $this->getDescriptionMarkup();
$form['redirect_to_datasets'] = $this->getRedirectCheckbox($config);
$form['html_allowed_properties'] = $this->getHtmlAllowedProperties($config);
$form['property_list'] = $this->getPropertyList($config);

return parent::buildForm($form, $form_state);
}

/**
* Provides a markup description for the Metastore settings form.
*
* @return array
* Render array containing the form description.
*/
private function getDescriptionMarkup() {
return [
'#markup' => $this->t('Configure the metastore settings.'),
];
}

/**
* Builds the checkbox form element for redirecting after form submission.
*
* @param \Drupal\Core\Config\Config $config
* The metastore settings configuration.
*
* @return array
* The form element array.
*/
private function getRedirectCheckbox(Config $config) {
return [
'#type' => 'checkbox',
'#title' => $this->t('Redirect to datasets view after form submit'),
'#default_value' => $config->get('redirect_to_datasets'),
'#description' => $this->t('Enable this option to automatically redirect
to the datasets view after submitting a dataset form.'),
];
}

$form['html_allowed_properties'] = [
/**
* Builds the checkboxes for dataset properties that allow HTML.
*
* @param \Drupal\Core\Config\Config $config
* The metastore settings configuration.
*
* @return array
* The form element array.
*/
private function getHtmlAllowedProperties(Config $config) {
return [
'#type' => 'checkboxes',
'#title' => $this->t('Dataset properties that allow HTML'),
'#description' => $this->t('Metadata properties that may contain HTML elements.'),
'#description' => $this->t('Metadata properties that may contain
HTML elements.'),
'#options' => $this->schemaHelper->retrieveStringSchemaProperties(),
'#default_value' => $config->get('html_allowed_properties') ?:
['dataset_description', 'distribution_description'],
'#default_value' => $config->get('html_allowed_properties')
?: [
'dataset_description',
'distribution_description',
],
];
}

$form['property_list'] = [
/**
* Builds the checkboxes for dataset properties stored as separate entities.
*
* @param \Drupal\Core\Config\Config $config
* The metastore settings configuration.
*
* @return array
* The form element array.
*/
private function getPropertyList(Config $config) {
return [
'#type' => 'checkboxes',
'#title' => $this->t('Dataset properties to be stored as separate entities; use caution'),
'#description' => $this->t('Select properties from the dataset schema to be available as individual objects.
Each property will be assigned a unique identifier in addition to its original schema value.'),
'#title' => $this->t('Dataset properties to be stored as separate
entities; use caution'),
'#description' => $this->t('Select properties from the dataset schema
to be available as individual objects. Each property will be assigned
a unique identifier in addition to its original schema value.'),
'#options' => $this->schemaHelper->retrieveSchemaProperties(),
'#default_value' => $config->get('property_list'),
];

return parent::buildForm($form, $form_state);
}

/**
* Inherited.
*
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);

$this->config('metastore.settings')
->set('redirect_to_datasets', $form_state->getValue('redirect_to_datasets'))
->set('property_list', $form_state->getValue('property_list'))
->set('html_allowed_properties', $form_state->getValue('html_allowed_properties'))
->save();
Expand Down