Skip to content

Commit

Permalink
Add alt_text field to image content type
Browse files Browse the repository at this point in the history
  • Loading branch information
jackahl committed Sep 25, 2024
1 parent 4a1f217 commit e5004cf
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
2 changes: 2 additions & 0 deletions news/700.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add alt_text field to image content type. This allows users to manually set the value of alt tag, for usage in automated listings.
[@cekk, @jackahl]
3 changes: 2 additions & 1 deletion plone/app/contenttypes/browser/templates/image.pt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
<figure class="figure">
<a tal:define="
scale context/@@images;
img_tag python:scale.tag('image', scale='large', css_class='figure-img img-fluid');
alt_text context/alt_text|nothing;
img_tag python:scale.tag('image', scale='large', css_class='figure-img img-fluid', alt=alt_text);
"
tal:attributes="
href string:${context/@@plone_context_state/object_url}/image_view_fullscreen;
Expand Down
14 changes: 14 additions & 0 deletions plone/app/contenttypes/schema/image.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@
<required>False</required>
<title i18n:translate="">Description</title>
</field>
<field name="alt_text"
type="zope.schema.TextLine">
<description i18n:translate="label_alt_text_help">
Briefly describe the meaning of the image for people using assistive technology like screen readers.
This will be used when the image is viewed by itself or in automated contexts like listings.
Do not duplicate the Title or Description fields, since those might also be read by screen readers.
Alt text should describe what a sighted user sees when looking at the image.
This might include text the image contains, or even a description of an abstract pattern.
In case your description already sufficiently describes your image leave this field blank.
</description>
<required>False</required>
<title i18n:translate="label_alt_text">Alt Text</title>
</field>
<field marshal:primary="true"
name="image"
type="plone.namedfile.field.NamedBlobImage"
Expand All @@ -28,3 +41,4 @@
</field>
</schema>
</model>

41 changes: 41 additions & 0 deletions plone/app/contenttypes/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,16 @@ def setUp(self):
image.title = "My Image"
image.description = "This is my image."
image.image = dummy_image()

self.portal.invokeFactory('Image', 'image-with-alt')
image_alt = self.portal['image-with-alt']
image_alt.title = 'My Image 2'
image_alt.description = 'This is my second image.'
image_alt.alt_text = 'An alt text'

This comment has been minimized.

Copy link
@yurj

yurj Oct 1, 2024

Contributor

I think you've to add alt_image also to the image object too, not just image_alt. Contentlisting raise an error if the attribute does not exists, thus the template fails. You can set alt_text to None in the image object, for example.

image.image = dummy_image()

self.image = image
self.image_alt = image_alt
self.request.set("URL", image.absolute_url())
self.request.set("ACTUAL_URL", image.absolute_url())
alsoProvides(self.request, IPloneFormLayer)
Expand All @@ -85,6 +94,30 @@ def test_image_view(self):
self.assertTrue("My Image" in view())
self.assertTrue("This is my image." in view())

def test_image_view_alt(self):
view = self.image_alt.restrictedTraverse('@@view')

self.assertTrue(view())
self.assertEqual(view.request.response.status, 200)
self.assertTrue('My Image 2' in view())
self.assertTrue('This is my second image.' in view())
self.assertTrue('An alt text' in view())

def test_image_alt_in_listing_view(self):
self.image_alt.image = dummy_image(u'image.svg')
view = self.portal.restrictedTraverse('@@listing_view')
self.assertTrue('An alt text' in view())

def test_image_alt_in_summary_view(self):
self.image_alt.image = dummy_image(u'image.svg')
view = self.portal.restrictedTraverse('@@summary_view')
self.assertTrue('An alt text' in view())

def test_image_alt_in_album_view(self):
self.image_alt.image = dummy_image(u'image.svg')
view = self.portal.restrictedTraverse('@@album_view')
self.assertTrue('An alt text' in view())

# XXX: Not working. See ImageFunctionalTest test_image_view_fullscreen
# Problem seems to be that the image is not properly uploaded.
# def test_image_view_fullscreen(self):
Expand All @@ -105,6 +138,14 @@ def test_svg_image(self):
r'<img src="http://nohost/plone/image/@@images/[a-z0-9\-]*.svg" alt="My Image" title="My Image" height="[a-z0-9\-]*" width="[a-z0-9\-]*" />', # noqa: E501
)

def test_svg_image_alt(self):
self.image_alt.image = dummy_image(u'image.svg')
scale = self.image_alt.restrictedTraverse('@@images')
self.assertRegex(
scale.scale('image', scale='large').tag(),
r'<img src="http://nohost/plone/image-with-alt/@@images/[a-z0-9--]*.svg" alt="An alt text" height="768" width="768" />' # noqa: E501
)


class ImageFunctionalTest(unittest.TestCase):
layer = PLONE_APP_CONTENTTYPES_FUNCTIONAL_TESTING
Expand Down

0 comments on commit e5004cf

Please sign in to comment.