Skip to content

Commit

Permalink
Redirect to data-dictionary list after form submission (#4166)
Browse files Browse the repository at this point in the history
  • Loading branch information
janette authored Apr 25, 2024
1 parent 3b34bde commit 77572ae
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cypress/integration/09_admin_links.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ context('Administration pages', () => {
})
cy.contains('h1', 'DKAN Metastore (Data Dictionaries)');
cy.get('.button').contains('+ Add new data dictionary').click( { force:true })
cy.get('summary').contains('Project Open Data Data-Dictionary');
cy.get('fieldset').contains('Data Dictionary Fields');
})

})
9 changes: 8 additions & 1 deletion dkan.install
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@
function dkan_update_9001() {
$modules = ['path', 'config'];
\Drupal::service('module_installer')->install($modules);
}
}

/**
* Enable data dictionary widget.
*/
function dkan_update_9002() {
\Drupal::service('module_installer')->install(['data_dictionary_widget']);
}
32 changes: 28 additions & 4 deletions modules/data_dictionary_widget/data_dictionary_widget.module
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
*/

use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\data_dictionary_widget\Fields\FieldOperations;
use Drupal\node\NodeInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
* Implements hook_theme().
Expand Down Expand Up @@ -65,20 +68,31 @@ function data_dictionary_widget__data_dictionary_data_type_checker($context) {
* Implements hook_form_alter().
*
* Setting form validation for unique identifier.
*
*
* Modifying current_fields array structure to prevent errors in element render array.
*
*
* Attaching module library to render theme changes.
*/
function data_dictionary_widget_form_alter(&$form, &$form_state, $form_id) {
$formObject = $form_state->getFormObject();

if ($formObject instanceof \Drupal\Core\Entity\EntityFormInterface) {
$entity = $formObject->getEntity();
$data_type = $entity->get('field_data_type')->value;
if (isset($form["field_json_metadata"]["widget"][0]["dictionary_fields"])) {
if ($entity->getEntityTypeId() === 'node' && in_array($entity->bundle(), ['data'])) {
$form['#attached']['library'][] = 'data_dictionary_widget/dataDictionaryWidget';
}
}

// If we are saving a data dictionary alter the submit.
foreach (array_keys($form['actions']) as $action) {
if ( isset($form['actions'][$action]['#type'])
&& $form['actions'][$action]['#type'] === 'submit'
&& $data_type == 'data-dictionary') {
$form['actions']['submit']['#submit'][] = 'data_dictionary_widget_form_submit';
}
}
}

if ($form_id == 'node_data_edit_form' || $form_id == 'node_data_form') {
Expand All @@ -90,7 +104,7 @@ function data_dictionary_widget_form_alter(&$form, &$form_state, $form_id) {
foreach ($current_fields as $key => $value) {
$keys = array_keys($value);
$formatted_current_fields[$key] = [];

foreach ($keys as $attr) {
$formatted_current_fields[$key][$attr] = [
'#value' => $value[$attr]
Expand All @@ -110,6 +124,16 @@ function data_dictionary_widget_form_alter(&$form, &$form_state, $form_id) {
}
}

/**
* Redirect to the data dictionary view after saving a data dictionary.
*/
function data_dictionary_widget_form_submit($form, FormStateInterface $form_state) {
$url = Url::fromRoute('metastore.data_dictionary');
$response = new RedirectResponse($url->toString());
$response->send();
return;
}

/**
* Checking if identifier is already used.
*/
Expand All @@ -120,7 +144,7 @@ function data_dictionary_widget_validate_unique_identifier($form, &$form_state)

if (isset($form["field_json_metadata"]["widget"][0]["identifier"]["#value"])) {
$submitted_identifier = $form["field_json_metadata"]["widget"][0]["identifier"]["#value"];

foreach ($existing_data_dictionary_nodes as $node) {
if ($current_nid !== $node["nid"] && strtolower($submitted_identifier) === strtolower($node["identifier"])) {
$form_state->setError($form["field_json_metadata"]["widget"][0]["identifier"], 'The identifier you entered is taken. Please choose another one.');
Expand Down
2 changes: 1 addition & 1 deletion modules/metastore/metastore.install
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function metastore_update_8009() {
$dict->save();
$count++;
}
return t("Updated $count dictionaries. If you have overridden DKAN's core schemas,
return t("Updated $count dictionaries. If you have overridden DKAN's core schemas,
you must update your site's data dictionary schema after this update. Copy
modules/contrib/dkan/schema/collections/data-dictionary.json over you local
site version before attempting to read or write any data dictionaries.");
Expand Down

0 comments on commit 77572ae

Please sign in to comment.