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

Redirect to data-dictionary list after form submission #4166

Merged
merged 5 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
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');
})

})
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
9 changes: 8 additions & 1 deletion modules/metastore/metastore.install
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,15 @@ 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.");
}

/**
* Enable data dictionary widget.
*/
function metastore_update_8010() {
\Drupal::service('module_installer')->install(['data_dictionary_widget']);
paul-m marked this conversation as resolved.
Show resolved Hide resolved
}