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.

Last updated June 7, 2017

Use a form submit hook to pass information down the form API and create a node programmatically.

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.

  1. Create a form submit hook, and act upon values passed to it.
  2. Create a node programmatically.

Tasks

Write a submit hook to accept the values of the form.

Use the validation hook to pass the details of the film to the submit hook.

Write a function to create a node programmatically if the movie does not already have a node on the site. Use the title, overview, and release date from the data object you retrieved from The Movie DB API in the previous lesson.

For bonus points, on an existing node, you can check if the data from the external API is the same as is in the Drupal database and update the node programmatically if there are differences.

Summary

  • Parameters in the form API, e.g. hook_form_validate and hook_form_submit are passed by reference.
  • Nodes can be created programmatically and the representation of nodes when we view them is very similar in structure to when we save them.

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.