diff --git a/islandora.libraries.yml b/islandora.libraries.yml deleted file mode 100644 index 9df91fa2..00000000 --- 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 be353675..65db0630 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 180e1ff7..00000000 --- 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);