From b9b88e373b56ea0586e7b71382bb0340789d5edf Mon Sep 17 00:00:00 2001 From: Mark Jordan Date: Tue, 30 Jun 2020 14:37:34 +0000 Subject: [PATCH] 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 00000000..9df91fa2 --- /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 7af255e3..be353675 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 00000000..180e1ff7 --- /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);