From 7c0e7b5a1a5c0946c0bd47288461527b141fb4ca Mon Sep 17 00:00:00 2001 From: henry Date: Mon, 27 Mar 2023 20:49:02 +1100 Subject: [PATCH] SDPAP-7602: Add a URL Redirect editing improvements. --- js/redirect.js | 21 +++++++++++ tide_site.libraries.yml | 8 +++++ tide_site.module | 80 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 js/redirect.js diff --git a/js/redirect.js b/js/redirect.js new file mode 100644 index 00000000..73c43e98 --- /dev/null +++ b/js/redirect.js @@ -0,0 +1,21 @@ +/** +* @file +* Provides site redirect functionality. +*/ + +(function ($, Drupal) { + Drupal.behaviors.human_interval = { + attach: function (context, settings) { + let base_url = drupalSettings.base_url; + let site_id = drupalSettings.site_id; + // Edit a form. + if (site_id !== null) { + $('.field--name-redirect-source span.field-prefix').text(base_url + 'site-' + site_id + '/'); + } + // Select change event. + $("#edit-redirect-site").change(function(){ + $('.field--name-redirect-source span.field-prefix').text(base_url + 'site-' + this.value + '/'); + }); + } + } +})(jQuery, Drupal); diff --git a/tide_site.libraries.yml b/tide_site.libraries.yml index cce46c25..776544c8 100644 --- a/tide_site.libraries.yml +++ b/tide_site.libraries.yml @@ -2,3 +2,11 @@ tide_site_layout: css: theme: css/tide_site_layout.css: {} +tide_site_redirect: + js: + js/redirect.js: {} + dependencies: + - core/jquery + - core/jquery.once + - core/drupal + - core/drupalSettings diff --git a/tide_site.module b/tide_site.module index 38a62c99..1f59c9e9 100644 --- a/tide_site.module +++ b/tide_site.module @@ -24,6 +24,7 @@ use Drupal\views\ViewExecutable; use Drupal\path_alias\Entity\PathAlias; use Drupal\path_alias\PathAliasInterface; use Drupal\tide_site\TideSitePathAliasListBuilder; +use Drupal\taxonomy\TermInterface; /** * Implements hook_entity_bundle_create(). @@ -588,6 +589,14 @@ function tide_site_form_alter(array &$form, FormStateInterface $form_state, $for } } } + // Add site prefix to redirect URL. + if ('redirect_redirect_form' === $form_id) { + _site_redirect_selection($form); + } + // Edit site prefix to redirect URL. + if ('redirect_redirect_edit_form' === $form_id) { + _site_redirect_selection($form, TRUE); + } } /** @@ -936,3 +945,74 @@ function tide_site_tide_help(string $route_name, RouteMatchInterface $route_matc return '

' . t('Learn how to create and edit navigation menus.', ['@sdp-handbook' => 'https://www.singledigitalpresence.vic.gov.au/create-and-edit-menus']) . '

'; } } + +/** + * Submition handler for redirect form. + */ +function _tide_site_set_site_redirect(&$form, FormStateInterface $form_state) { + $from = $form_state->getValue('redirect_source'); + $site = $form_state->getValue('redirect_site'); + if (($site !== 0) && isset($from[0]['path'])) { + $form_state->setValue('redirect_source', [['path' => 'site-' . $site . '/' . $from[0]['path']]]); + } +} + +/** + * Add site selection field to redirect form. + */ +function _site_redirect_selection(&$form, $is_edit_form = FALSE) { + $taxonomy_term = 'sites'; + // Get all primary sites ony, do not load sub-sites. + $term_tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree( + $taxonomy_term, + 0, + 1, + TRUE + ); + // Build an options list. + $options = [0 => 'None']; + foreach ($term_tree as $term) { + if ($term instanceof TermInterface) { + $options[$term->id()] = $term->getName(); + } + } + // Get the site ID from the redirect source while edit a redirect entity. + if ($is_edit_form) { + $source = $form['redirect_source']['widget'][0]['path']['#default_value']; + $site_position = strpos($source, 'site-'); + $id_position = strpos($source, '/'); + if (($site_position !== FALSE) && ($id_position !== FALSE)) { + $site_id = substr($source, 5, $id_position - 5); + $form['redirect_source']['widget'][0]['path']['#default_value'] = substr($source, $id_position + 1); + } + } + $form['redirect_site'] = [ + '#type' => 'select', + '#title' => 'Select site', + '#options' => $options, + '#default_value' => $site_id ?? 0, + '#weight' => -10, + '#attached' => [ + 'library' => [ + 'tide_site/tide_site_redirect', + ], + 'drupalSettings' => [ + 'base_url' => $form['redirect_source']['widget'][0]['path']['#field_prefix'], + 'site_id' => $site_id ?? NULL, + ] + ] + ]; + + if (array_key_exists('actions', $form)) { + foreach (array_keys($form['actions']) as $action) { + if ( + $action !== 'delete' + && isset($form['actions'][$action]['#type']) + && $form['actions'][$action]['#type'] === 'submit' + && isset($form['actions'][$action]['#submit']) + ) { + array_unshift($form['actions'][$action]['#submit'], '_tide_site_set_site_redirect'); + } + } + } +}