Whenever Drupal needs to provide an image in specific dimenions, FE needs to provide an image style.
Create a file in /.tools/es/responsive_image
and define the height and width of the image for each breakpoint. Afterwards run phab scaffold .tools/es/index.yml
to create the image styles. They are getting created in /config/sync
. Don't forget to commit them and let the BE devs know what the relevant file is.
To see the changes locally:
phab -cmbb drush cim
1
when you are prompted to):phab -cmbb drush 'image-flush --all'
phab -cmbb drush cr
If you get any php errors during running phab scaffold .tools/es/index.yml
you might need to update the scaffolder via composer update factorial-io/phab-entity-scaffolder
src/components/elements/image
// src/components/elements/image/schema.yaml
$schema: http://json-schema.org/draft-07/schema#
$id: /elements/image
additionalProperties: false
type: object
required:
- uri
- width
- height
- alt
properties:
uri:
type: string
format: uri-reference
width:
type: number
height:
type: number
alt:
type: string
sources:
type: array
items:
type: object
properties:
srcset:
type: string
media:
type: string
size:
type: string
enum:
- xs
- sm
- md
- lg
- xl
width:
type: number
height:
type: number
required:
- srcset
- media
- size
- width
- height
classes:
type: array
items:
type: string
is_lazy:
type: boolean
// src/components/elements/image/mocks.yaml
uri: https://via.placeholder.com/320x240
height: 240
width: 320
alt: Sample alt text
$variants:
- $name: src with lazy
is_lazy: true
- $name: srcset with same ratio
uri: https://via.placeholder.com/320x240
alt: alt text
sources:
- srcset: https://via.placeholder.com/2000x1504
media: '(min-width: 90em)'
size: xl
height: 1920
width: 2560
- srcset: https://via.placeholder.com/1440x1080
media: '(min-width: 64em) and (max-width: 89.9375em)'
size: lg
height: 1080
width: 1440
- srcset: https://via.placeholder.com/1024x768
media: '(min-width: 48em) and (max-width: 63.9375em)'
size: md
height: 768
width: 1024
- srcset: https://via.placeholder.com/768x576
media: '(min-width: 35.625em) and (max-width: 47.9375em)'
size: sm
height: 576
width: 768
- srcset: https://via.placeholder.com/570x427
media: '(max-width: 35.5625em)'
size: xs
height: 427
width: 570
- $name: srcset with different ratio
uri: https://via.placeholder.com/320x240
alt: alt text
sources:
- srcset: https://via.placeholder.com/2560x120
media: '(min-width: 90em)'
size: xl
height: 120
width: 2560
- srcset: https://via.placeholder.com/1440x1080
media: '(min-width: 64em) and (max-width: 89.9375em)'
size: lg
height: 1080
width: 1440
- srcset: https://via.placeholder.com/1024x768
media: '(min-width: 48em) and (max-width: 63.9375em)'
size: md
height: 768
width: 1024
- srcset: https://via.placeholder.com/576x768
media: '(min-width: 35.625em) and (max-width: 47.9375em)'
size: sm
height: 768
width: 576
- srcset: https://via.placeholder.com/427x570
media: '(max-width: 35.5625em)'
size: xs
height: 570
width: 427
- $name: srcset with lazy loading
is_lazy: true
uri: https://via.placeholder.com/320x240
alt: alt text
sources:
- srcset: https://via.placeholder.com/2560x1920
media: '(min-width: 90em)'
size: xl
height: 1920
width: 2560
- srcset: https://via.placeholder.com/1440x1080
media: '(min-width: 64em) and (max-width: 89.9375em)'
size: lg
height: 1080
width: 1440
- srcset: https://via.placeholder.com/1024x768
media: '(min-width: 48em) and (max-width: 63.9375em)'
size: md
height: 768
width: 1024
- srcset: https://via.placeholder.com/576x768
media: '(min-width: 35.625em) and (max-width: 47.9375em)'
size: sm
height: 768
width: 576
- srcset: https://via.placeholder.com/427x570
media: '(max-width: 35.5625em)'
size: xs
height: 570
width: 427
// src/components/elements/image/image.twig
{% if sources and sources is not empty %}
<picture>
{% for source in sources %}
<source
{% if source.type %}
type="{{ source.type }}"
{% endif %}
{% if source.media %}
media="{{ source.media }}"
{% endif %}
{% if source.width %}
width="{{ source.width }}"
{% endif %}
{% if source.height %}
height="{{ source.height }}"
{% endif %}
srcset="{{ source.srcset }}"
>
{% endfor %}
<img
src="{{ uri }}"
alt="{{ alt }}"
{% if alt == "" %}
aria-hidden="true"
{% endif %}
{% if width %}
width="{{ width }}"
{% endif %}
{% if height %}
height="{{ height }}"
{% endif %}
{% if is_lazy %}
loading="lazy"
decoding="async"
{% elseif is_priority %}
fetchpriority="high"
{% endif %}
>
</picture>
{% else %}
<img
src="{{ uri }}"
alt="{{ alt }}"
{% if alt == "" %}
aria-hidden="true"
{% endif %}
{% if width %}
width="{{ width }}"
{% endif %}
{% if height %}
height="{{ height }}"
{% endif %}
{% if is_lazy %}
loading="lazy"
decoding="async"
{% elseif is_priority %}
fetchpriority="high"
{% endif %}
>
{% endif %}
src with lazy mock data
uri: https://via.placeholder.com/320x240
height: 240
width: 320
alt: Sample alt text
is_lazy: true
srcset with same ratio mock data
uri: https://via.placeholder.com/320x240
height: 240
width: 320
alt: alt text
sources:
- srcset: https://via.placeholder.com/2000x1504
media: '(min-width: 90em)'
size: xl
height: 1920
width: 2560
- srcset: https://via.placeholder.com/1440x1080
media: '(min-width: 64em) and (max-width: 89.9375em)'
size: lg
height: 1080
width: 1440
- srcset: https://via.placeholder.com/1024x768
media: '(min-width: 48em) and (max-width: 63.9375em)'
size: md
height: 768
width: 1024
- srcset: https://via.placeholder.com/768x576
media: '(min-width: 35.625em) and (max-width: 47.9375em)'
size: sm
height: 576
width: 768
- srcset: https://via.placeholder.com/570x427
media: '(max-width: 35.5625em)'
size: xs
height: 427
width: 570
srcset with different ratio mock data
uri: https://via.placeholder.com/320x240
height: 240
width: 320
alt: alt text
sources:
- srcset: https://via.placeholder.com/2560x120
media: '(min-width: 90em)'
size: xl
height: 120
width: 2560
- srcset: https://via.placeholder.com/1440x1080
media: '(min-width: 64em) and (max-width: 89.9375em)'
size: lg
height: 1080
width: 1440
- srcset: https://via.placeholder.com/1024x768
media: '(min-width: 48em) and (max-width: 63.9375em)'
size: md
height: 768
width: 1024
- srcset: https://via.placeholder.com/576x768
media: '(min-width: 35.625em) and (max-width: 47.9375em)'
size: sm
height: 768
width: 576
- srcset: https://via.placeholder.com/427x570
media: '(max-width: 35.5625em)'
size: xs
height: 570
width: 427
srcset with lazy loading mock data
uri: https://via.placeholder.com/320x240
height: 240
width: 320
alt: alt text
is_lazy: true
sources:
- srcset: https://via.placeholder.com/2560x1920
media: '(min-width: 90em)'
size: xl
height: 1920
width: 2560
- srcset: https://via.placeholder.com/1440x1080
media: '(min-width: 64em) and (max-width: 89.9375em)'
size: lg
height: 1080
width: 1440
- srcset: https://via.placeholder.com/1024x768
media: '(min-width: 48em) and (max-width: 63.9375em)'
size: md
height: 768
width: 1024
- srcset: https://via.placeholder.com/576x768
media: '(min-width: 35.625em) and (max-width: 47.9375em)'
size: sm
height: 768
width: 576
- srcset: https://via.placeholder.com/427x570
media: '(max-width: 35.5625em)'
size: xs
height: 570
width: 427