Skip to content

Commit

Permalink
Add alt_text field for banner tile
Browse files Browse the repository at this point in the history
  • Loading branch information
hvelarde committed Mar 5, 2018
1 parent 01050f3 commit d416841
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Expand Up @@ -6,7 +6,8 @@ There's a frood who really knows where his towel is.
1.6b6 (unreleased)
^^^^^^^^^^^^^^^^^^

- Nothing changed yet.
- Provide alternative text for image fields in tiles (closes `#628 <https://github.com/collective/collective.cover/issues/628>`_).
[hvelarde]


1.6b5 (2017-11-21)
Expand Down
20 changes: 16 additions & 4 deletions src/collective/cover/tests/test_banner_tile.py
Expand Up @@ -3,6 +3,7 @@
from collective.cover.tests.base import TestTileMixin
from collective.cover.tiles.banner import BannerTile
from collective.cover.tiles.banner import IBannerTile
from lxml import etree
from mock import Mock

import unittest
Expand Down Expand Up @@ -96,10 +97,21 @@ def test_render_empty(self):
def test_render_with_image(self):
obj = self.portal['my-image']
self.tile.populate_with_object(obj)
rendered = self.tile()
# the image is there and the alt attribute is set
self.assertIn('<img ', rendered)
self.assertIn('alt="This image was created for testing purposes"', rendered)
html = etree.HTML(self.tile())
img = html.find('*//img')
self.assertIsNotNone(img)
self.assertIn('alt', img.attrib)
self.assertEqual(
img.attrib['alt'], 'This image was created for testing purposes')

# set alternate text
alt_text = u'Murciélago hindú'
self.tile.data['alt_text'] = alt_text
html = etree.HTML(self.tile())
img = html.find('*//img')
self.assertIsNotNone(img)
self.assertIn('alt', img.attrib)
self.assertEqual(img.attrib['alt'], alt_text)

def test_render_with_link(self):
obj = self.portal['my-link']
Expand Down
25 changes: 21 additions & 4 deletions src/collective/cover/tiles/banner.py
Expand Up @@ -3,7 +3,9 @@
from collective.cover import _
from collective.cover.tiles.base import IPersistentCoverTile
from collective.cover.tiles.base import PersistentCoverTile
from collective.cover.tiles.configuration_view import IDefaultConfigureForm
from collective.cover.utils import get_types_use_view_action_in_listings
from plone.autoform import directives as form
from plone.namedfile import field
from plone.tiles.interfaces import ITileDataManager
from plone.uuid.interfaces import IUUID
Expand All @@ -25,12 +27,24 @@ class IBannerTile(IPersistentCoverTile):
required=False,
)

remote_url = schema.TextLine(
# form.no_omit('alt_text')
form.omitted(IDefaultConfigureForm, 'alt_text')
alt_text = schema.TextLine(
title=_(
u'label_alt_text',
default=u'Alternative Text'),
description=_(
u'help_alt_text',
default=u'Provides a textual alternative to non-text content in web pages.'), # noqa E501
required=False,
)

remote_url = schema.TextLine( # FIXME: this must be schema.URI()
title=_(u'URL'),
required=False,
)

uuid = schema.TextLine(
uuid = schema.TextLine( # FIXME: this must be schema.ASCIILine()
title=_(u'UUID'),
required=False,
readonly=True,
Expand Down Expand Up @@ -82,6 +96,8 @@ def populate_with_object(self, obj):
'description': description,
'uuid': IUUID(obj),
'image': image,
# FIXME: https://github.com/collective/collective.cover/issues/778
'alt_text': description or title,
'remote_url': remote_url,
})

Expand Down Expand Up @@ -110,5 +126,6 @@ def htmltag(self):

@property
def alt(self):
"""Return the alt attribute for the image."""
return self.data.get('description') or self.data.get('title')
"""Return alternative text dealing with form init issues."""
alt_text = self.data['alt_text']
return alt_text if alt_text is not None else u''

0 comments on commit d416841

Please sign in to comment.