Skip to content

Commit cf25bb0

Browse files
authored
Merge pull request #305 from torchbox/feature/work-page-img-alt
Add extra alt text options to the work page image
2 parents ce00cad + 50ff163 commit cf25bb0

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

tbx/project_styleguide/templates/patterns/pages/work/work_page.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<picture>
2020
<source media="(max-width: 598px)" srcset="{{ mobile_image.url }} 1x, {{ mobile_image_retina.url }} 2x" />
2121
<source media="(min-width: 599px)" srcset="{{ desktop_image.url }} 1x, {{ desktop_image_retina.url }} 2x" />
22-
<img src="{{ desktop_image.url }}" alt="{{ desktop_image.alt }}" />
22+
<img src="{{ desktop_image.url }}" alt="{% if not page.header_image_is_decorative %}{{ page.header_image_alt_text }}{% endif %}" />
2323
</picture>
2424

2525
{% if page.header_caption %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Generated by Django 4.2.13 on 2024-07-30 08:18
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("work", "0034_add_earth_colour_theme"),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name="workpage",
15+
name="header_alt_text",
16+
field=models.CharField(
17+
blank=True,
18+
help_text="By default the image title (shown above) is used as the alt text. Use this field to provide more specific alt text if required.",
19+
max_length=255,
20+
),
21+
),
22+
migrations.AddField(
23+
model_name="workpage",
24+
name="header_image_is_decorative",
25+
field=models.BooleanField(
26+
default=False,
27+
help_text="If checked, this will make the alt text empty.",
28+
),
29+
),
30+
]

tbx/work/models.py

+18
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,16 @@ class WorkPage(ColourThemeMixin, ContactMixin, SocialFields, NavigationFields, P
201201
on_delete=models.SET_NULL,
202202
related_name="+",
203203
)
204+
header_image_is_decorative = models.BooleanField(
205+
help_text="If checked, this will make the alt text empty.",
206+
default=False,
207+
)
208+
header_alt_text = models.CharField(
209+
blank=True,
210+
max_length=255,
211+
help_text="By default the image title (shown above) is used as the alt text. "
212+
"Use this field to provide more specific alt text if required.",
213+
)
204214

205215
header_caption = models.CharField("caption", max_length=255, blank=True)
206216
header_attribution = models.CharField("attribution", max_length=255, blank=True)
@@ -226,6 +236,8 @@ class WorkPage(ColourThemeMixin, ContactMixin, SocialFields, NavigationFields, P
226236
MultiFieldPanel(
227237
[
228238
FieldPanel("header_image"),
239+
FieldPanel("header_alt_text"),
240+
FieldPanel("header_image_is_decorative"),
229241
FieldPanel("header_caption"),
230242
FieldPanel("header_attribution"),
231243
],
@@ -277,6 +289,12 @@ def first_author(self):
277289
return author.author
278290
return None
279291

292+
@cached_property
293+
def header_image_alt_text(self):
294+
if header_alt_text := self.header_alt_text:
295+
return header_alt_text
296+
return self.header_image.title
297+
280298
@property
281299
def related_works(self):
282300
prefetch_listing_images = models.Prefetch(

0 commit comments

Comments
 (0)