This bundle provides an easy way to add a collection of media for each page type in Kunstmaan Bundles CMS.
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.
composer require arsthanea/page-media-set-bundle
After installation, add the PageMediaSetBundle to your kernel and update your db schema / create migration.
There are three main steps:
- Implement the
HasMediaSetInterfaceon 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:
There is a simple helper function in twig templates:
{% block header %}
{% set banner = page_media(page, "banner") %}
{% if banner %}<img src="{{ banner }}" alt="page banner">{% endif %}
{% endblock %}For example in your controller:
/** @var HasMediaSetInterface $page */
$mediaUrl = $this->get('page_media_set.page_media_set_service')->getPageMedia($page, "banner");Add to your config.yml or similar:
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.
You can configure the media set definitions using symfony config, instead of returning them using the getMediaSetDefinition method.
# 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.
Format names are taken from translations, from messages dictionary using page_media_set.format.%s keys. For instance:
# messages.yml
page_media_set:
format:
banner: Top page bannerIf 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.
# config.yml
page_media_set:
indexer: true