From b9b88e373b56ea0586e7b71382bb0340789d5edf Mon Sep 17 00:00:00 2001 From: Mark Jordan Date: Tue, 30 Jun 2020 14:37:34 +0000 Subject: [PATCH 1/7] Work on #1491. --- islandora.libraries.yml | 7 +++++++ islandora.module | 31 +++++++++++++++++++++++++++++++ js/image_media_form_defaults.js | 6 ++++++ 3 files changed, 44 insertions(+) create mode 100644 islandora.libraries.yml create mode 100644 js/image_media_form_defaults.js diff --git a/islandora.libraries.yml b/islandora.libraries.yml new file mode 100644 index 000000000..9df91fa27 --- /dev/null +++ b/islandora.libraries.yml @@ -0,0 +1,7 @@ +image_media_form_defaults: + version: 1.x + js: + js/image_media_form_defaults.js: {} + dependencies: + - core/jquery + - core/jquery.once diff --git a/islandora.module b/islandora.module index 7af255e3d..be3536752 100644 --- a/islandora.module +++ b/islandora.module @@ -316,6 +316,24 @@ function islandora_preprocess_node(&$variables) { } } +/** + * Implements hook_form_alter(). + */ +function islandora_form_alter(&$form, FormStateInterface $form_state, $form_id) { + $media_add_forms = ['media_audio_add_form', 'media_document_add_form', + 'media_extracted_text_add_form', 'media_file_add_form', 'media_image_add_form', + 'media_fits_technical_metadata_add_form', 'media_video_add_form', + ]; + + if (in_array($form['#form_id'], $media_add_forms)) { + $params = \Drupal::request()->query->all(); + $media_of_nid = $params['edit']['field_media_of']['widget'][0]['target_id']; + $node = \Drupal::entityTypeManager()->getStorage('node')->load($media_of_nid); + $title = $node->getTitle(); + $form['name']['widget'][0]['value']['#default_value'] = $title; + } +} + /** * Implements hook_entity_form_display_alter(). */ @@ -468,3 +486,16 @@ function islandora_preprocess_views_view_table(&$variables) { } } } + +/** + * Implements hook_page_attachments(). + */ +function islandora_page_attachments(array &$attachments) { + $current_path = \Drupal::service('path.current')->getPath(); + $path_args = explode('/', ltrim($current_path, '/')); + // Need to populate the "Alt text" field using JavaScript since it is + // not part of the rendered form, it is added via Ajax on file upload. + if (count($path_args) >= 3 && $path_args[0] == 'media' && $path_args[1] == 'add' && $path_args[2] == 'image') { + $attachments['#attached']['library'][] = 'islandora/image_media_form_defaults'; + } +} diff --git a/js/image_media_form_defaults.js b/js/image_media_form_defaults.js new file mode 100644 index 000000000..180e1ff7f --- /dev/null +++ b/js/image_media_form_defaults.js @@ -0,0 +1,6 @@ +(function (Drupal, $) { + var ImageName = $('input[id=edit-name-0-value]').val(); + $(document).ajaxSuccess(function () { + $('input[data-drupal-selector=edit-field-media-image-0-alt]').val(ImageName); + }); +})(Drupal, jQuery); From 4fbf4f9c50472ff3028147dc60407572fdd53de0 Mon Sep 17 00:00:00 2001 From: Mark Jordan Date: Wed, 12 Aug 2020 03:59:19 +0000 Subject: [PATCH 2/7] Work in #1491, incorporating @jordandukart 's suggestion. --- islandora.libraries.yml | 7 ------- islandora.module | 35 +++++++++++++++++++++------------ js/image_media_form_defaults.js | 6 ------ 3 files changed, 22 insertions(+), 26 deletions(-) delete mode 100644 islandora.libraries.yml delete mode 100644 js/image_media_form_defaults.js diff --git a/islandora.libraries.yml b/islandora.libraries.yml deleted file mode 100644 index 9df91fa27..000000000 --- a/islandora.libraries.yml +++ /dev/null @@ -1,7 +0,0 @@ -image_media_form_defaults: - version: 1.x - js: - js/image_media_form_defaults.js: {} - dependencies: - - core/jquery - - core/jquery.once diff --git a/islandora.module b/islandora.module index be3536752..65db0630f 100644 --- a/islandora.module +++ b/islandora.module @@ -334,6 +334,28 @@ function islandora_form_alter(&$form, FormStateInterface $form_state, $form_id) } } +/** + * Implements hook_field_widget_WIDGET_TYPE_form_alter(). + */ +function islandora_field_widget_image_image_form_alter(&$element, $form_state, $context) { + $element['#process'][] = 'islandora_add_default_image_alt_text'; +} + +/** + * hook_field_widget_WIDGET_TYPE_form_alter() callback. + */ +function islandora_add_default_image_alt_text($element, $form_state, $form) { + if ($element['alt']['#access']) { + $params = \Drupal::request()->query->all(); + $media_of_nid = $params['edit']['field_media_of']['widget'][0]['target_id']; + $node = \Drupal::entityTypeManager()->getStorage('node')->load($media_of_nid); + $title = $node->getTitle(); + $element['alt']['#default_value'] = $title; + } + return $element; +} + + /** * Implements hook_entity_form_display_alter(). */ @@ -486,16 +508,3 @@ function islandora_preprocess_views_view_table(&$variables) { } } } - -/** - * Implements hook_page_attachments(). - */ -function islandora_page_attachments(array &$attachments) { - $current_path = \Drupal::service('path.current')->getPath(); - $path_args = explode('/', ltrim($current_path, '/')); - // Need to populate the "Alt text" field using JavaScript since it is - // not part of the rendered form, it is added via Ajax on file upload. - if (count($path_args) >= 3 && $path_args[0] == 'media' && $path_args[1] == 'add' && $path_args[2] == 'image') { - $attachments['#attached']['library'][] = 'islandora/image_media_form_defaults'; - } -} diff --git a/js/image_media_form_defaults.js b/js/image_media_form_defaults.js deleted file mode 100644 index 180e1ff7f..000000000 --- a/js/image_media_form_defaults.js +++ /dev/null @@ -1,6 +0,0 @@ -(function (Drupal, $) { - var ImageName = $('input[id=edit-name-0-value]').val(); - $(document).ajaxSuccess(function () { - $('input[data-drupal-selector=edit-field-media-image-0-alt]').val(ImageName); - }); -})(Drupal, jQuery); From f6983b2e3fa2290a6029e00b03dc3545498fcbac Mon Sep 17 00:00:00 2001 From: Mark Jordan Date: Wed, 12 Aug 2020 04:51:32 +0000 Subject: [PATCH 3/7] Fixed code style errors. --- islandora.module | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/islandora.module b/islandora.module index 65db0630f..9179c45b8 100644 --- a/islandora.module +++ b/islandora.module @@ -342,7 +342,7 @@ function islandora_field_widget_image_image_form_alter(&$element, $form_state, $ } /** - * hook_field_widget_WIDGET_TYPE_form_alter() callback. + * Callback for hook_field_widget_WIDGET_TYPE_form_alter(). */ function islandora_add_default_image_alt_text($element, $form_state, $form) { if ($element['alt']['#access']) { @@ -355,7 +355,6 @@ function islandora_add_default_image_alt_text($element, $form_state, $form) { return $element; } - /** * Implements hook_entity_form_display_alter(). */ From da8a095f2005d684de07262d1fe066fb1ac92907 Mon Sep 17 00:00:00 2001 From: Mark Jordan Date: Wed, 12 Aug 2020 05:56:16 +0000 Subject: [PATCH 4/7] Removed superfluous code. --- islandora.module | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/islandora.module b/islandora.module index 9179c45b8..87c17eb59 100644 --- a/islandora.module +++ b/islandora.module @@ -329,8 +329,7 @@ function islandora_form_alter(&$form, FormStateInterface $form_state, $form_id) $params = \Drupal::request()->query->all(); $media_of_nid = $params['edit']['field_media_of']['widget'][0]['target_id']; $node = \Drupal::entityTypeManager()->getStorage('node')->load($media_of_nid); - $title = $node->getTitle(); - $form['name']['widget'][0]['value']['#default_value'] = $title; + $form['name']['widget'][0]['value']['#default_value'] = $node->getTitle(); } } @@ -349,8 +348,7 @@ function islandora_add_default_image_alt_text($element, $form_state, $form) { $params = \Drupal::request()->query->all(); $media_of_nid = $params['edit']['field_media_of']['widget'][0]['target_id']; $node = \Drupal::entityTypeManager()->getStorage('node')->load($media_of_nid); - $title = $node->getTitle(); - $element['alt']['#default_value'] = $title; + $element['alt']['#default_value'] = $node->getTitle(); } return $element; } From b74d8a7f85e15169a3a0ddee5168cbbe019ce904 Mon Sep 17 00:00:00 2001 From: Mark Jordan Date: Wed, 12 Aug 2020 22:46:34 +0000 Subject: [PATCH 5/7] Fixed WSODs and warnings. --- islandora.module | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/islandora.module b/islandora.module index 87c17eb59..5250eb9f6 100644 --- a/islandora.module +++ b/islandora.module @@ -327,9 +327,13 @@ function islandora_form_alter(&$form, FormStateInterface $form_state, $form_id) if (in_array($form['#form_id'], $media_add_forms)) { $params = \Drupal::request()->query->all(); - $media_of_nid = $params['edit']['field_media_of']['widget'][0]['target_id']; - $node = \Drupal::entityTypeManager()->getStorage('node')->load($media_of_nid); - $form['name']['widget'][0]['value']['#default_value'] = $node->getTitle(); + if (count($params) > 0 && array_key_exists('edit', $params)) { + $media_of_nid = $params['edit']['field_media_of']['widget'][0]['target_id']; + $node = \Drupal::entityTypeManager()->getStorage('node')->load($media_of_nid); + if ($node) { + $form['name']['widget'][0]['value']['#default_value'] = $node->getTitle(); + } + } } } @@ -346,9 +350,13 @@ function islandora_field_widget_image_image_form_alter(&$element, $form_state, $ function islandora_add_default_image_alt_text($element, $form_state, $form) { if ($element['alt']['#access']) { $params = \Drupal::request()->query->all(); - $media_of_nid = $params['edit']['field_media_of']['widget'][0]['target_id']; - $node = \Drupal::entityTypeManager()->getStorage('node')->load($media_of_nid); - $element['alt']['#default_value'] = $node->getTitle(); + if (count($params) > 0 && array_key_exists('edit', $params)) { + $media_of_nid = $params['edit']['field_media_of']['widget'][0]['target_id']; + $node = \Drupal::entityTypeManager()->getStorage('node')->load($media_of_nid); + if ($node) { + $element['alt']['#default_value'] = $node->getTitle(); + } + } } return $element; } From b78c2cc7dab47db2d9e5e3af36e8ad3f62fbc7fa Mon Sep 17 00:00:00 2001 From: Mark Jordan Date: Thu, 13 Aug 2020 03:00:30 +0000 Subject: [PATCH 6/7] Empty commit to trigger a build. From 28b7b09c3ef1a6933c6f796d58c5a71d29cb163f Mon Sep 17 00:00:00 2001 From: Mark Jordan Date: Thu, 13 Aug 2020 14:18:35 +0000 Subject: [PATCH 7/7] Replace two logic checks with one. --- islandora.module | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/islandora.module b/islandora.module index 5250eb9f6..76f887e89 100644 --- a/islandora.module +++ b/islandora.module @@ -327,7 +327,7 @@ function islandora_form_alter(&$form, FormStateInterface $form_state, $form_id) if (in_array($form['#form_id'], $media_add_forms)) { $params = \Drupal::request()->query->all(); - if (count($params) > 0 && array_key_exists('edit', $params)) { + if (isset($params['edit'])) { $media_of_nid = $params['edit']['field_media_of']['widget'][0]['target_id']; $node = \Drupal::entityTypeManager()->getStorage('node')->load($media_of_nid); if ($node) { @@ -350,7 +350,7 @@ function islandora_field_widget_image_image_form_alter(&$element, $form_state, $ function islandora_add_default_image_alt_text($element, $form_state, $form) { if ($element['alt']['#access']) { $params = \Drupal::request()->query->all(); - if (count($params) > 0 && array_key_exists('edit', $params)) { + if (isset($params['edit'])) { $media_of_nid = $params['edit']['field_media_of']['widget'][0]['target_id']; $node = \Drupal::entityTypeManager()->getStorage('node')->load($media_of_nid); if ($node) {