slashworks / contao-simple-svg-icons-bundle
Simply use SVG icons from an SVG icon sprite file with Contao inserttags.
Package info
github.com/slashworks/contao-simple-svg-icons-bundle
Type:contao-bundle
pkg:composer/slashworks/contao-simple-svg-icons-bundle
Requires
- php: ^8.1
- ext-dom: *
- ext-libxml: *
- ext-simplexml: *
- contao/core-bundle: ^5.3
- contao/imagine-svg: ^1.0
- imagine/imagine: ^1.3
- symfony/dependency-injection: ^6.4 || ^7.0
- symfony/deprecation-contracts: ^3.0
- symfony/event-dispatcher: ^6.4 || ^7.0
- symfony/http-kernel: ^6.4 || ^7.0
- symfony/service-contracts: ^3.0
Requires (Dev)
- contao/easy-coding-standard: ^6.13 || ^7.0 || ^8.0
- contao/manager-plugin: ^2.3
- phpstan/phpstan: ^1.11 || ^2.0
- phpstan/phpstan-phpunit: ^1.4 || ^2.0
- phpstan/phpstan-symfony: ^1.4 || ^2.0
- phpunit/phpunit: ^10.5 || ^11.5 || ^12.0
Suggests
None
Provides
None
Conflicts
- contao/core: *
- contao/manager-plugin: <2.0 || >=3.0
Replaces
None
README
For Contao 5.3 and 5.7 LTS (PHP 8.1+; Contao 5.7 requires PHP 8.3+).
About
With this extension you can easily add SVG icons from an icon-sprite file to your website via Contao Inserttags.
It also allows to output inline SVG from an SVG file in the file system.
Installation
Install with composer require slashworks/contao-simple-svg-icons-bundle or via the Contao Manager.
After updating the database, select the SVG icon sprite file in the settings of your theme. Use the new field icon files to select the SVG icon sprite file. The icons you can use are taken from this file.
For an easy start, download the example-sprite.svg and place it inside the files folder of your contao installation.
An SVG icon sprite file is a collection of multiple SVG icons, defined within a <symbol>.
The menu icon for example looks like this:
<symbol viewBox="0 0 24 24" id="ic_menu_24px" title="Menu"> <path d="M3 18h18v-2H3v2zm0-5h18v-2H3v2zm0-7v2h18V6H3z"/> </symbol>
The important part for using this icon is the id of the symbol.
The optional title attribute (or a <title> child element) is used as accessible name of the icon. Icons without a title are treated as decorative and hidden from screen readers (aria-hidden="true").
Symbols nested in <defs> are supported as well.
For further information about creating your own SVG icon sprite file you can check out A Guide to Create and Use SVG Sprites.
Usage
SVG Sprite
When you want to output a symbol from an SVG sprite, the base format is
{{svg::ICON_ID}}.
The rendered output:
<svg class="svg-icon ICON_ID" viewBox="x y w h" aria-hidden="true" focusable="false"> <use href="/files/sprite.svg?v=FILE_HASH#ICON_ID"></use> </svg>
With a title:
<svg class="svg-icon ICON_ID" viewBox="x y w h" title="Menu" role="img" aria-label="Menu"> <title>Menu</title> <use href="/files/sprite.svg?v=FILE_HASH#ICON_ID"></use> </svg>
The sprite file must be publicly accessible (make the folder public in the file manager).
Inline SVG
You can also output an SVG file inline.
Instead of the symbol's id you have to provide the UUID of the SVG file in the file system:
{{svg::a8824458-a08e-11e9-9d96-81cb79fa7a74}}
The rendered output:
<svg class="svg-inline" viewBox="x y w h"> <path></path> … </svg>
Parameters
You can pass additional parameters to the insert tag:
class: {{svg::ICON_ID|UUID?class=my-css-class}}
This adds the given string as CSS class to the svg element and will be rendered as:
<svg class="svg-inline ICON_ID my-css-class">…</svg>
id: {{svg::ICON_ID|UUID?id=my-css-id}}
This adds the given string as CSS ID to the svg element and will be rendered as:
<svg class="svg-inline ICON_ID" id="my-css-id">…</svg>
width: {{svg::ICON_ID|UUID?width=120}}
For sprite icons the value is output as width attribute (numbers or px, em, rem, %).
For inline SVG, if the width property is set without a height property, the extension tries to get the aspect ratio from already existing width/height attributes or the viewBox attribute and sets the height accordingly. You can always overwrite this with CSS.
height: {{svg::ICON_ID|UUID?height=80}}
For sprite icons the value is output as height attribute.
For inline SVG, if the height property is set without a width property, the extension tries to get the aspect ratio from already existing width/height attributes or the viewBox attribute and sets the width accordingly. You can always overwrite this with CSS.
Invalid values (e. g. CSS classes or IDs containing quotes or brackets) are ignored.
You can use multiple parameters in any combination:
{{svg::UUID?width=120}}
{{svg::UUID?height=80&class=custom-icon}}
{{svg::ICON_ID|UUID?id=my-icon-id&class=custom-icon-class}}
Tips
The main purpose for svg icons is to use them in conjuction with text, e. g. menu burger, contact information, slider ui elements, …. As a starting point for the CSS styling of the svg icons you could use the following definitions:
.svg-icon, .svg-inline { fill: currentColor; height: auto; width: 1em; }
This will scale the icon according to the font-size of the parent element.
Additionally, the vertical positioning requires some further adjustments for fine tuning. You can try to use vertical-align, relative positioning with some top or bottom values, or make the parent a flex-container and use align-items to properly position the enclosed icon.
Example: Burger Menu
Lets walk through a burger menu example.
We want to use the icon_menu_24px from the example sprite, so our inserttag looks like this:
{{svg::icon_menu_24px}}
The HTML code with the inserttag:
<button> Menu {{svg::icon_menu_24px}} </button>
After setting the font-size for the button and the width for the svg, it will look like this:
button { font-size: 1em; } button .svg-inline { width: 1.5em; }
Finally we adjust the vertical alignment of the icon:
button { align-items: center; display: inline-flex; } button svg { margin-left: 5px; }
When using the recommended CSS from the tips section, the SVG icon will inherit color from the button.
button:hover { color: red; }
Of course you can define a different color for the SVG icon itself:
button:hover svg { color: green; }
Backend widget for icon selection
The extension contains a custom backend widget to make the icon selection easy and intuitive.
To use the widget, use the following code as reference. The General::getIcons() options callback returns the IDs of all symbols from the sprite files selected in the themes.
Example dca file, e. g. contao/dca/tl_content.php
<?php use \Slashworks\ContaoSimpleSvgIconsBundle\DataContainer\General; $GLOBALS['TL_DCA']['tl_content']['fields']['myIcon'] = array ( 'label' => &$GLOBALS['TL_LANG']['tl_content']['icon'], 'inputType' => 'svgiconselect', // This field uses the custom backend widget 'options_callback' => array(General::class, 'getIcons'), // Get all svg icons selected in the themes. 'reference' => &$GLOBALS['TL_LANG']['MSC']['icons'], // Use the symbols ID as key for a translation. 'eval' => array('includeBlankOption' => true), 'sql' => "varchar(64) NOT NULL default ''", );
Example language file, e. g. contao/languages/de/default.php
<?php $GLOBALS['TL_LANG']['MSC']['icons']['icon-arrow-right'] = 'Pfeil nach rechts'; $GLOBALS['TL_LANG']['MSC']['icons']['icon-arrow-left'] = 'Pfeil nach links';
Development
composer install composer unit-tests # PHPUnit composer phpstan # static analysis composer cs-fixer # coding standard (contao/easy-coding-standard)
Licensing
This contao module is licensed under the terms of the LGPLv3.
Credits
The icons used in the example sprite have been taken from Google Material Icons.