Make SQL queries in Drupal, make HTTP requests to external websites, and use this data to validate a form.
Objectives and Prerequisites
You will need the following knowledge and skills. If you need a review, check out the Resources section at the bottom of this page.
- Understand how to check for the existence of a property on a node.
- Create a validation hook for a form.
- 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 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.
Summary
- All database transactions happen via an abstraction layer.
- Making requests to external URLs is simple in Drupal 7.
Resources
- Video: Creating, Submitting and Validating a Simple Form (Drupalize.Me)
- Video: An Overview of the Database Layer in Drupal 7 (Drupalize.Me)
- Video: Querying the Database with db_select() (Drupalize.Me)
- API Documentation: Database abstraction layer (api.drupal.org)
- API Documentation: drupal_http_request (api.drupal.org)