Skip to content

Commit 22918db

Browse files
authored
Add config option in metastore settings to stop forcing redirect (#4381)
1 parent e273496 commit 22918db

File tree

6 files changed

+213
-19
lines changed

6 files changed

+213
-19
lines changed

modules/metastore/config/install/metastore.settings.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
redirect_to_datasets: true
12
csv_headers_mode: resource_headers
23
property_list:
34
'theme': 'theme'

modules/metastore/config/schema/metastore.schema.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@ metastore.settings:
2626
resource_perspective_display:
2727
type: string
2828
label: 'Resource download url display'
29+
redirect_to_datasets:
30+
type: boolean
31+
label: 'Redirect to datasets view after form submit'

modules/metastore/metastore.install

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,16 @@ function metastore_update_8009() {
150150
modules/contrib/dkan/schema/collections/data-dictionary.json over you local
151151
site version before attempting to read or write any data dictionaries.");
152152
}
153+
154+
/**
155+
* Set the default value for the redirect_to_datasets setting.
156+
*/
157+
function metastore_update_8010() {
158+
$config = \Drupal::configFactory()->getEditable('metastore.settings');
159+
160+
if ($config->get('redirect_to_datasets') === NULL) {
161+
$config->set('redirect_to_datasets', TRUE)->save();
162+
}
163+
164+
drupal_flush_all_caches();
165+
}

modules/metastore/modules/metastore_admin/metastore_admin.module

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,12 @@ function metastore_admin_form_alter(&$form, FormStateInterface $form_state, $for
155155
* Submit handler to redirect after node save to dkan dataset content page.
156156
*/
157157
function metastore_admin_form_submit($form, FormStateInterface &$form_state) {
158-
$form_state->setRedirect('view.dkan_dataset_content.page_1');
158+
$redirect_to_datasets = \Drupal::config('metastore.settings')->get('redirect_to_datasets');
159+
160+
// If the setting is enabled, redirect to the dataset content page.
161+
if ($redirect_to_datasets) {
162+
$form_state->setRedirect('view.dkan_dataset_content.page_1');
163+
}
159164
}
160165

161166
/*
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
namespace Drupal\metastore_admin\Tests\Functional;
4+
5+
use Drupal\Tests\BrowserTestBase;
6+
7+
/**
8+
* Test the redirect_to_datasets functionality.
9+
*
10+
* This test ensures that when the "Redirect to datasets view after form submit"
11+
* setting is enabled, users are redirected to the dataset listing page after
12+
* submitting a dataset form. When disabled, users remain on the dataset node page.
13+
*
14+
* @group dkan
15+
* @group metastore
16+
* @group metastore_admin
17+
* @group functional
18+
*/
19+
class RedirectToDatasetsTest extends BrowserTestBase {
20+
21+
protected static $modules = [
22+
'dkan',
23+
'metastore',
24+
'metastore_admin',
25+
'node',
26+
];
27+
28+
protected $defaultTheme = 'stark';
29+
30+
/**
31+
* @todo Remove this when we drop support for Drupal 10.0.
32+
*/
33+
protected $strictConfigSchema = FALSE;
34+
35+
/**
36+
* Tests dataset form submission when redirect_to_datasets is enabled.
37+
*/
38+
public function testDatasetRedirect() {
39+
/** @var \Drupal\metastore\MetastoreService $metastore_service */
40+
$metastore_service = $this->container->get('dkan.metastore.service');
41+
42+
$this->drupalLogin(
43+
// @todo Figure out least possible admin permissions.
44+
$this->drupalCreateUser([], NULL, TRUE)
45+
);
46+
$assert = $this->assertSession();
47+
48+
// 07_admin_dataset_json_form.spec.js : User can create and edit a dataset
49+
// with the json form UI.
50+
//
51+
// Since we don't have JavaScript, we can't use select2 or select_or_other
52+
// to add publisher or keyword entities. We create them here with arbitrary
53+
// UUIDs so that we can post the names to the form.
54+
$publisher_name = uniqid();
55+
$metastore_service->post('publisher',
56+
$metastore_service->getValidMetadataFactory()->get(
57+
json_encode((object) [
58+
'identifier' => '9deadc2f-50e0-512a-af7c-4323697d530d',
59+
'data' => ['name' => $publisher_name],
60+
]), 'publisher', ['method' => 'POST'])
61+
);
62+
// We need a keyword.
63+
$keyword_data = uniqid();
64+
$metastore_service->post('keyword',
65+
$metastore_service->getValidMetadataFactory()->get(json_encode((object) [
66+
'identifier' => '05b2e74a-eb23-585b-9c1c-4d023e21e8a5',
67+
'data' => $keyword_data,
68+
]), 'keyword', ['method' => 'POST'])
69+
);
70+
71+
// Enable redirect option.
72+
$this->config('metastore.settings')
73+
->set('redirect_to_datasets', TRUE)
74+
->save();
75+
76+
// Create new dataset, populate required fields.
77+
$this->drupalGet('node/add/data');
78+
$assert->statusCodeEquals(200);
79+
80+
$dataset_title = 'DKANTEST dataset title';
81+
$this->submitForm([
82+
'edit-field-json-metadata-0-value-title' => $dataset_title,
83+
'edit-field-json-metadata-0-value-description' => 'DKANTEST dataset description.',
84+
'edit-field-json-metadata-0-value-accesslevel' => 'public',
85+
'edit-field-json-metadata-0-value-modified-date' => '2020-02-02',
86+
'edit-field-json-metadata-0-value-publisher-publisher-name' => $publisher_name,
87+
'edit-field-json-metadata-0-value-contactpoint-contactpoint-fn' => 'DKANTEST Contact Name',
88+
'edit-field-json-metadata-0-value-contactpoint-contactpoint-hasemail' => '[email protected]',
89+
'edit-field-json-metadata-0-value-keyword-keyword-0' => $keyword_data,
90+
], 'Save');
91+
92+
// Assert that the redirect happened.
93+
$assert->statusCodeEquals(200);
94+
$assert->addressEquals('admin/dkan/datasets');
95+
$assert->pageTextContains('Data ' . $dataset_title . ' has been created.');
96+
97+
// Disable redirect option.
98+
$this->config('metastore.settings')
99+
->set('redirect_to_datasets', FALSE)
100+
->save();
101+
102+
$this->drupalGet('admin/content');
103+
$assert->statusCodeEquals(200);
104+
$this->clickLink($dataset_title);
105+
$this->drupalGet($this->getSession()->getCurrentUrl() . '/edit');
106+
107+
$this->submitForm([
108+
'edit-field-json-metadata-0-value-description' => 'Updated Dataset Description.',
109+
], 'Save');
110+
111+
// Assert that the user lands on the dataset node page (not redirected).
112+
$assert->statusCodeEquals(200);
113+
$assert->addressMatches('/node\/\d+$/');
114+
$assert->pageTextContains('Data ' . $dataset_title . ' has been updated.');
115+
}
116+
117+
}

modules/metastore/src/Form/DkanDataSettingsForm.php

Lines changed: 73 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Drupal\metastore\Form;
44

5+
use Drupal\Core\Config\Config;
56
use Drupal\Core\Form\ConfigFormBase;
67
use Drupal\Core\Form\FormStateInterface;
78
use Drupal\Core\Routing\RouteBuilderInterface;
@@ -74,49 +75,103 @@ public function getFormId() {
7475
}
7576

7677
/**
77-
* Inherited.
78-
*
7978
* {@inheritdoc}
8079
*/
8180
public function buildForm(array $form, FormStateInterface $form_state) {
8281
$config = $this->config('metastore.settings');
8382

84-
$form['description'] = [
85-
'#markup' => $this->t(
86-
'Configure the metastore settings.'
87-
),
83+
$form['description'] = $this->getDescriptionMarkup();
84+
$form['redirect_to_datasets'] = $this->getRedirectCheckbox($config);
85+
$form['html_allowed_properties'] = $this->getHtmlAllowedProperties($config);
86+
$form['property_list'] = $this->getPropertyList($config);
87+
88+
return parent::buildForm($form, $form_state);
89+
}
90+
91+
/**
92+
* Provides a markup description for the Metastore settings form.
93+
*
94+
* @return array
95+
* Render array containing the form description.
96+
*/
97+
private function getDescriptionMarkup() {
98+
return [
99+
'#markup' => $this->t('Configure the metastore settings.'),
100+
];
101+
}
102+
103+
/**
104+
* Builds the checkbox form element for redirecting after form submission.
105+
*
106+
* @param \Drupal\Core\Config\Config $config
107+
* The metastore settings configuration.
108+
*
109+
* @return array
110+
* The form element array.
111+
*/
112+
private function getRedirectCheckbox(Config $config) {
113+
return [
114+
'#type' => 'checkbox',
115+
'#title' => $this->t('Redirect to datasets view after form submit'),
116+
'#default_value' => $config->get('redirect_to_datasets'),
117+
'#description' => $this->t("Disable this option if you want to use Drupal's default or your own custom redirect after submitting a metadata form."),
88118
];
119+
}
89120

90-
$form['html_allowed_properties'] = [
121+
/**
122+
* Builds the checkboxes for dataset properties that allow HTML.
123+
*
124+
* @param \Drupal\Core\Config\Config $config
125+
* The metastore settings configuration.
126+
*
127+
* @return array
128+
* The form element array.
129+
*/
130+
private function getHtmlAllowedProperties(Config $config) {
131+
return [
91132
'#type' => 'checkboxes',
92133
'#title' => $this->t('Dataset properties that allow HTML'),
93-
'#description' => $this->t('Metadata properties that may contain HTML elements.'),
134+
'#description' => $this->t('Metadata properties that may contain
135+
HTML elements.'),
94136
'#options' => $this->schemaHelper->retrieveStringSchemaProperties(),
95-
'#default_value' => $config->get('html_allowed_properties') ?:
96-
['dataset_description', 'distribution_description'],
137+
'#default_value' => $config->get('html_allowed_properties')
138+
?: [
139+
'dataset_description',
140+
'distribution_description',
141+
],
97142
];
143+
}
98144

99-
$form['property_list'] = [
145+
/**
146+
* Builds the checkboxes for dataset properties stored as separate entities.
147+
*
148+
* @param \Drupal\Core\Config\Config $config
149+
* The metastore settings configuration.
150+
*
151+
* @return array
152+
* The form element array.
153+
*/
154+
private function getPropertyList(Config $config) {
155+
return [
100156
'#type' => 'checkboxes',
101-
'#title' => $this->t('Dataset properties to be stored as separate entities; use caution'),
102-
'#description' => $this->t('Select properties from the dataset schema to be available as individual objects.
103-
Each property will be assigned a unique identifier in addition to its original schema value.'),
157+
'#title' => $this->t('Dataset properties to be stored as separate
158+
entities; use caution'),
159+
'#description' => $this->t('Select properties from the dataset schema
160+
to be available as individual objects. Each property will be assigned
161+
a unique identifier in addition to its original schema value.'),
104162
'#options' => $this->schemaHelper->retrieveSchemaProperties(),
105163
'#default_value' => $config->get('property_list'),
106164
];
107-
108-
return parent::buildForm($form, $form_state);
109165
}
110166

111167
/**
112-
* Inherited.
113-
*
114168
* {@inheritdoc}
115169
*/
116170
public function submitForm(array &$form, FormStateInterface $form_state) {
117171
parent::submitForm($form, $form_state);
118172

119173
$this->config('metastore.settings')
174+
->set('redirect_to_datasets', $form_state->getValue('redirect_to_datasets'))
120175
->set('property_list', $form_state->getValue('property_list'))
121176
->set('html_allowed_properties', $form_state->getValue('html_allowed_properties'))
122177
->save();

0 commit comments

Comments
 (0)