Page Media Set
This bundle provides an easy way to add a collection of media for each page type in Kunstmaan Bundles CMS., (*1)
The main goal is to have a common set of media attachments across many page types without the hassle of upgrading
the db schema every time. For instance, think of adding a top page banner with the same dimensions to every page
without the need to add the relations themselves to the entities., (*2)
Installation:
composer require arsthanea/page-media-set-bundle
, (*3)
After installation, add the PageMediaSetBundle
to your kernel and update your db schema / create migration., (*4)
Usage
There are three main steps:, (*5)
- Implement the
HasMediaSetInterface
on your entities. This tells the bundle what types of media will be used for each page.
- Configure your media types in symfony configuration (see below)
- You now have an additional âMedia Setâ tab when editing the page where you can set the media
After setting them there are two ways of accessing them:, (*6)
Twig function
There is a simple helper function in twig templates:, (*7)
{% block header %}
{% set banner = page_media(page, "banner") %}
{% if banner %}<img src="{{ banner }}" alt="page banner">{% endif %}
{% endblock %}
Service in the container
For example in your controller:, (*8)
/** @var HasMediaSetInterface $page */
$mediaUrl = $this->get('page_media_set.page_media_set_service')->getPageMedia($page, "banner");
Configuration
Add to your config.yml
or similar:, (*9)
page_media_set:
formats:
banner:
min_width: 1920
min_height: 420
max_width: 1920
max_height: 460
teaser: ~
You need to configure all your media types, but the constraints are optional., (*10)
You can configure the media set definitions using symfony config, instead of returning them using the getMediaSetDefinition
method., (*11)
# app/config/config.yml
page_media_set:
types:
'Acme\Foo\Bar\BazEntity': [ 'foo', 'bar' ]
In this case Acme\Foo\Bar\BazEntity::getMediaSetDefinition()
wonât be called, foo
and bar
formats will be used., (*12)
Format names are taken from translations, from messages
dictionary using page_media_set.format.%s
keys. For instance:, (*13)
# messages.yml
page_media_set:
format:
banner: Top page banner
Indexer
If youâre using the search bundle, you may enable indexing page thumbnails. The first defined media will be stored in
the elasticsearch document under 'photo' key and then you can use it directly on the search results page., (*14)
# config.yml
page_media_set:
indexer: true