Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

'Simple Like Button' released as a Drupal module | Why we used a custom AJAX form -and a custom entity.

Parent Feed: 

During the development of Lucius we built 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, outside of the OpenLucius Drupal install profile.

So this is the first release we 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 module_name.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 more 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

Check it out on Drupal.org. Go to download page

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web