Drupal 8 + Google Perspective API

Rafael Nogueira
Grey Frogs
Published in
3 min readAug 8, 2017

If only we could have a Drupal module that automatically analyzes comments and makes sure people are not posting whatever they want… Wait, now we have it!

This text was originally written here: https://therefore.ca/blog/drupal-8-perspective-api/

Google has recently announced the new API called Perspective, which will analyze the content of any given text by using machine learning and return the — what they call — toxicity of it.

With the flexibility of Drupal 8 and the incredibly fast responses of the Google’s API, we decided it was a good opportunity to have them integrated, so we did it!

What do I need to know before downloading the module?

Before actually getting started, let’s review the status of the API.

  • By the time I’m writing this article, Perspective is not yet fully released, which means you’ll have to request an API access and get whitelisted here: https://www.perspectiveapi.com/#/
  • The API only supports English so far.

How does it work?

The API integration is very simple and straightforward. All we need to do is get an API key and submit a simple Json like this one:

The CURL call would look like:

Within less than 1 second, you should receive the response, which looks like this:

This response above is telling us that our comment is toxic. If you check the score value of the Json response, you’ll see a value pretty close to 1. The toxicity range goes from 0 (non-toxic) to 1 (toxic) so the closest you are to 1, the more toxic it is.

How do I get it running on Drupal?

All of the information above was just to explain the heart of the module, you won’t need to worry about any of this when installing and using it. So enough with this, let’s get straight into it.

1. DOWNLOAD AND INSTALL THE MODULE

If you have drush running, simply run drush en -y perspective. That will download and install the module.

If you don’t use drush, download it from https://www.drupal.org/project/perspective

2. ADD YOUR API KEY AND CONFIGURE THE MODULE

Navigate to /admin/config/content/perspective and you'll see 3 fields, like on the image below:

The Google API field is pretty self-explanatory. When you get whitelisted, you’ll have to enable the Perspective API on the Google Developer Console and you’ll get a key. Use this field to put it.

The Toxicity tolerance will be your filter. If the value is too high means somehow toxic comments are allowed. The closer it is to 0, the less tolerant it is.

The Error text will be the message displayed if the form does not pass the validation.

3. ADD THE PERSPECTIVE FIELD

As mentioned before, Drupal 8 is very flexible and we made use of it. The Perspective field can basically be added to any fieldable entity on Drupal. Just add a field like you add any other one and select Perspective on the dropdown, like below:

Here is an example of the Perspective field working in a comment entity:

Well, that wraps it up! I hope you enjoy the module and that it helps moderating content of your website.

--

--