heimrichhannot / contao-likes-bundle
Put like/dislike buttons on anything in Contao CMS.
Installs: 17
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 0
Type:contao-bundle
Requires
- php: ^8.2
- contao/core-bundle: ^5.1
- doctrine/dbal: ^2.13 || ^3.0
- psr/log: ^1.0 || ^2.0 || ^3.0
- symfony/config: ^6.4 || ^7.1
- symfony/event-dispatcher-contracts: ^1.0 || ^2.0 || ^3.0
- symfony/filesystem: ^6.4 || ^7.1
- symfony/http-foundation: ^6.4 || ^7.1
- symfony/http-kernel: ^6.4 || ^7.1
- symfony/lock: ^6.4 || ^7.1
- symfony/string: ^6.4 || ^7.1
- twig/twig: ^3.0
Requires (Dev)
- contao/manager-plugin: ^2.0
- contao/test-case: ^4.0 || ^5.0
- heimrichhannot/contao-test-utilities-bundle: ^0.1
- php-coveralls/php-coveralls: ^2.0
- phpstan/phpstan: ^1.10
- phpstan/phpstan-symfony: ^1.2
- phpunit/phpunit: ^8.0 || ^9.0
- symfony/phpunit-bridge: ^5.4 || ^6.0
README
Slap a like button on anything in Contao.
This Contao CMS bundle allows you to add like and dislike buttons to any (twig) template, liking or disliking whatever you please to your heart's content.
Features
- Add like and dislike buttons to any template, attached to anything you want, e.g. models or your grandma's favorite knitting patterns
- Cookie-less/session-less association of likes and dislikes to individual user-agents using a tiny anonymous fingerprint
- Presumably GDPR-compliant, as no personal data is stored
- Extensive base template included
- Accessible with translatable title and aria-label attributes
- Rate Limiting to prevent abuse
- CSRF protected API endpoint
Installation
You can install this bundle using Composer:
composer require heimrichhannot/contao-likes-bundle
Then update your database and you're good to go.
Usage
To add a like button to your template, include a like & dislike button template and pass options as likes
,
preferably created by calling huh_likes()
with a namespace and a target (these can be literally any
strings you like to reference this likeable thing).
{{ include('@Contao/huh_likes/buttons.html.twig', { likes: huh_likes('knitting-pattern', 'honeycomb-lace') }) }}
On the Matter of liking Models
Most commonly, you will want to add a like button to something related to contao models, like a news article or a
calendar event. In this case, we recommend using the model's table name as namespace and the model's ID as
target. Or, even better, if you have access to the \Contao\Model
instance directly in your template, just pass it in.
Table and ID | Model Instance |
---|---|
huh_likes('tl_news', 42) |
huh_likes(model) |
The Dislike Button and other Options
In the provided default template, the dislike button is NOT shown by default. If you want to show it, pass more options
to the huh_likes()
function:
huh_likes(model, null, { likes_enabled: true, dislikes_enabled: true, })
Accessing Likes and Dislikes
By using the huh_likes()
function, you will get an object with the following getters available:
namespace
(string
): The namespace you providedtarget
(string
): The target you providedvoted
(int
): The number of votes the current user has cast on this target (typically 0, 1, or -1)likes
(int
): The total number of likesdislikes
(int
): The total number of dislikeslikesEnabled
(bool
): Whether likes are enableddislikesEnabled
(bool
): Whether dislikes are enabled
Customization
Within the default template @Contao/huh_likes/buttons.html.twig
,
you will find four blocks that you can override with your custom icons:
icon_like_neutral
icon_like_set
icon_dislike_neutral
icon_dislike_set
With these blocks, you can easily replace the default icons with your own SVGs or any other HTML content.