diff --git a/client/src/components/ChooserWidget/ImageChooserWidget.js b/client/src/components/ChooserWidget/ImageChooserWidget.js index af3342a4cf..a9a9a27626 100644 --- a/client/src/components/ChooserWidget/ImageChooserWidget.js +++ b/client/src/components/ChooserWidget/ImageChooserWidget.js @@ -34,6 +34,9 @@ export class ImageChooser extends Chooser { width: this.previewImage.getAttribute('width'), height: this.previewImage.getAttribute('height'), }; + state.default_alt_text = this.previewImage.getAttribute( + 'data-default-alt-text', + ); } return state; } @@ -42,6 +45,10 @@ export class ImageChooser extends Chooser { super.renderState(newState); this.previewImage.setAttribute('src', newState.preview.url); this.previewImage.setAttribute('width', newState.preview.width); + this.previewImage.setAttribute( + 'data-default-alt-text', + newState.default_alt_text, + ); } } diff --git a/wagtail/images/templates/wagtailimages/widgets/image_chooser.html b/wagtail/images/templates/wagtailimages/widgets/image_chooser.html index 81c27a2912..2f4d926f21 100644 --- a/wagtail/images/templates/wagtailimages/widgets/image_chooser.html +++ b/wagtail/images/templates/wagtailimages/widgets/image_chooser.html @@ -3,5 +3,5 @@ {% block chosen_icon %} {# Empty alt because the chosen item’s title is already displayed next to the image. #} - + {% endblock chosen_icon %} diff --git a/wagtail/images/tests/test_admin_views.py b/wagtail/images/tests/test_admin_views.py index 0cf5d0f150..4a3cddfca7 100644 --- a/wagtail/images/tests/test_admin_views.py +++ b/wagtail/images/tests/test_admin_views.py @@ -1876,6 +1876,7 @@ class TestImageChooserChosenView(WagtailTestUtils, TestCase): self.image = Image.objects.create( title="Test image", file=get_test_image_file(), + description="Test description", ) def get(self, params={}): @@ -1890,6 +1891,12 @@ class TestImageChooserChosenView(WagtailTestUtils, TestCase): response_json = json.loads(response.content.decode()) self.assertEqual(response_json["step"], "chosen") self.assertEqual(response_json["result"]["title"], "Test image") + self.assertEqual( + set(response_json["result"]["preview"].keys()), {"url", "width", "height"} + ) + self.assertEqual( + response_json["result"]["default_alt_text"], "Test description" + ) def test_with_multiple_flag(self): # if 'multiple' is passed as a URL param, the result should be returned as a single-item list @@ -1900,6 +1907,13 @@ class TestImageChooserChosenView(WagtailTestUtils, TestCase): self.assertEqual(response_json["step"], "chosen") self.assertEqual(len(response_json["result"]), 1) self.assertEqual(response_json["result"][0]["title"], "Test image") + self.assertEqual( + set(response_json["result"][0]["preview"].keys()), + {"url", "width", "height"}, + ) + self.assertEqual( + response_json["result"][0]["default_alt_text"], "Test description" + ) class TestImageChooserChosenMultipleView(WagtailTestUtils, TestCase): @@ -1910,15 +1924,18 @@ class TestImageChooserChosenMultipleView(WagtailTestUtils, TestCase): self.image1 = Image.objects.create( title="Test image", file=get_test_image_file(), + description="Test description", ) self.image2 = Image.objects.create( title="Another test image", file=get_test_image_file(), + description="Another test description", ) self.image3 = Image.objects.create( title="Unchosen test image", file=get_test_image_file(), + description="Unchosen test description", ) def get(self, params={}): @@ -1940,6 +1957,8 @@ class TestImageChooserChosenMultipleView(WagtailTestUtils, TestCase): self.assertEqual(len(response_json["result"]), 2) titles = {item["title"] for item in response_json["result"]} self.assertEqual(titles, {"Test image", "Another test image"}) + alt_texts = {item["default_alt_text"] for item in response_json["result"]} + self.assertEqual(alt_texts, {"Test description", "Another test description"}) class TestImageChooserSelectFormatView(WagtailTestUtils, TestCase): diff --git a/wagtail/images/views/chooser.py b/wagtail/images/views/chooser.py index 1caa34a84b..9dbed02f59 100644 --- a/wagtail/images/views/chooser.py +++ b/wagtail/images/views/chooser.py @@ -43,6 +43,7 @@ class ImageChosenResponseMixin(ChosenResponseMixin): "width": preview_image.width, "height": preview_image.height, } + response_data["default_alt_text"] = image.default_alt_text return response_data diff --git a/wagtail/images/widgets.py b/wagtail/images/widgets.py index b1954ad678..1fd4818da4 100644 --- a/wagtail/images/widgets.py +++ b/wagtail/images/widgets.py @@ -31,11 +31,13 @@ class AdminImageChooser(BaseChooser): "width": preview_image.width, "height": preview_image.height, } + data["default_alt_text"] = instance.default_alt_text return data def get_context(self, name, value_data, attrs): context = super().get_context(name, value_data, attrs) context["preview"] = value_data.get("preview", {}) + context["default_alt_text"] = value_data.get("default_alt_text", "") return context @property