Mr Treublaan 1-3
1097 DP Amsterdam
The Netherlands

'Simple Like Button' released as Drupal module | A technical explanation on the custom AJAX form -and custom entity implementation.

31 Jul, 2020 Drupal

Home Blog 'Simple Like Button' rele...

screenshot like button

During the development of Lucius I build quite a lot of features, that are embedded in the product (an open source Drupal distribution). But we figured some of these might come in handy for other Drupal site owners to use, outside of the OpenLucius Drupal install profile.

So this is the first release I did: Simple Like Button, a very simple like button Drupal module that works in two steps:

  1. Install module
  2. Place block
  3. Done!

Features

  • Instant feedback for end-user: count likes +1 or -1
  • Adds names of users that liked the content
  • It's compatible with all (custom) entities and bundles
  • Compatible with all 'authenticated' roles, not with 'anonymous'.
  • (non feature) Not compatible with Views at the moment.

Built with custom Drupal AJAX form

It's built with a custom AJAX Form, so we don't have to worry about building a custom AJAX route, a CSRF token and custom Javascripts to handle the user interactions.

While CSRF access checking looks like it can be easily integrated via xyz.routes.yml, ajax form was still more easy to implement.

Downside is that all forms can be cached, in high content site can lead up to unnecessary caching data.

And if you disagree on this approach, please let me know in the comments.

So anyway, if you are looking for a compact example of implementing an AJAX Form, check out the code of this module or the example in this blog.

Built with a Drupal custom entity

We used a Drupal entity for easy management of data:

  • Defining the data model and database table, easy installable.
  • CRUD: the Create, Read, Update and Delete queries are just easy if you work with Drupal entities.

So if you are looking for a compact example of implementing a custom entity, check out the code of this module.

And if you disagree on using a custom Drupal entity here, please let me know in the comments below.

Download the module

Please check out the project page on Drupal.org

Written by Joris Snoek | Jul 31, 2020
Let's Talk

Please tell me all about your project!
Mail , or send a message:

Got some more time?

Related content
09 May, 2023 Drupal

Save time, frustration, and potential content loss.


07 Jun, 2021 Drupal

Programmatically in multiple Views


02 Jun, 2021 Drupal