Module Development

Movie Project: Validate a Form for Drupal 7

Check your version

This tutorial covers a topic in Drupal 7 which may or may not be the version you're using. We're keeping this tutorial online as a courtesy to users of Drupal 7, but we consider it archived.

Make SQL queries in Drupal, make HTTP requests to external websites, and use this data to validate a form.

Goal

Query an external API to see if a movie already exists, and use this check to validate a form.

Prerequisites

You will need the following knowledge and skills. If you need a review, check out the Additional resources section at the bottom of this page.

  1. Understand how to check for the existence of a property on a node.
  2. Create a validation hook for a form.
  3. Use an external API to access data to use for validation.

Tasks

Create a function to check for the existence of a matching node within Drupal using the Drupal database abstraction layer. Return a boolean for whether it exists or not. If it does, notify the user that the node already exists.

Create a function which makes an API request to search for a movie in The Movie DB API with a matching title. If it does not exist, notify the user the movie does not exist in the external database. If it does exist, return the data object.

Recap

  • All database transactions happen via an abstraction layer.
  • Making requests to external URLs is simple in Drupal 7.

Further your understanding

Additional resources

Creative Commons License

Drupal Training Resources by Damian Robinson are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Based on a work at http://www.damianrobinson.co.uk.

Hands-On Exercises: Movie Project