Learning Page Manager
CourseHappy New Year! The Module Development series is wrapping up by working our way through another major API in Drupal, the database layer. We have five videos to walk you through it.
- An overview of the database layer in Drupal 7 is an excerpt from our Overview of Drupal 7 roundtable discussion, led by Angie Byron, the Drupal 7 co-maintainer.
In this chapter we learn about changing the definition of a database table after it has already been installed. This example expands on the previous chapters by adding a new field to the table to collect the date a user last visited a node. We learn more about hook_update_N() as well as db_add_field().
In this chapter we learn other ways to interact with our database beyond the simple db_select() query from the earlier video. This example walks through writing a module that that saves and displays data about how many times a user has visited a specific page. We learn about db_select(), db_insert(), db_update(), db_merge(), db_delete() and introduce the concept of "get and set" helper functions.
In this chapter we learn how to get data out of the database and display it. The example walks through writing a module that creates a page that displays how many nodes are published and unpublished for each content type in our site. We use the db_select() function to accomplish this.
In this excerpt from the Overview of Drupal 7, Angie Byron, talks about the Database API in Drupal 7. This was a round table discussion with other Lullabots on the line who ask and answer questions, in addition to Angie's presentation. This lays a good foundation for how the database system works in Drupal and sets you up for the hands-on tutorials that follow.
Additional resources
In this chapter we learn how to create a new database table. This example walks through creating a module that defines the the properties of our new table and also programatically creating that table. We learn about hook_schema() and hook_update_N() in our module's install file.
Trainers note: This video starts out with a module that implements hook_node_view() and stores data about views in the $_SESSION variable. It also states that we built this earlier in the series. However, that's not true. We did talk about hook_node_view() in https://drupalize.me/videos/event-driven-hook-system, but we never actually wrote the $_SESSION handling part. So, if you're following along and want the same sample code we start with make sure you download the code attached to this node.
Release Day: More Module Development
Blog postFor this Wednesday, we're adding more videos to the Module Development for Drupal 7 series. We have three new videos for you:
- Altering forms with hook_form_alter introduces you to one of the most used hooks in Drupal development. It lets you change any form in Drupal and is a great tool in your kit.
This video goes through the process of changing some of the properties of a form with the hook_form_alter() function. This hook is a very powerful tool to be able to modify existing Drupal forms, created by core or contributed modules, and tailor them to your needs.
This video shows how to target a specific form with the hook_form_FORM_ID_alter and creating a customized validation function for a form.
Note: There is a typo in this video. (The code is correct in the downloadable example file attached to the previous video.)
In the demo_validate_password()
function, the following line shown in the video if (in_array($form['values']['pass'], $badpasswords)) {
should be if (in_array($form_state['values']['pass'], $badpasswords)) {
.
This video goes through the process of creating a configuration form in order to save settings to the variables table in the database, and how to integrate those variables into your module.
Correction
The $item
array in the function demo_menu()
should be named $items
, to match the return $items;
line. (Either that or return $items;
should be fixed to return $item;
.) Just make sure the array you are building matches the name of the variable you are returning.
Additional resources
It's that time of the week again! This week we are continuing two of our series: Module Development for Drupal 7 and Introduction to Drush.
For the Module Development series, we are wrapping up the discussion of the Render API with a look at integrating with the theme system. Then we move on to four videos about the Form API system and how to do some fun things with it:
This videos goes through the process of creating a form with Drupal Forms API that is single select list that has a validation, submission and redirect functionality.
NOTE:
Their is a typo in the code displayed in the video. The function
function form_fun_cake(&$form_state)
is missing the $form paramater and should instead be
function form_fun_cake($form, &$form_state)
Curious about when you're supposed to translate "title" and when you should leave it alone? Here's the answer: You don't need to translate the 'title' attribute of an array that defines a menu item because Drupal will take care of that automatically. In fact, you shouldn't translate those as they'll end up getting double translated if you do. This is because Drupal end's up using this string of text in a number of different ways some of which don't actually need to be translated and others that do like for example when it's the title of a page or the text of a link in the menu system. The #title (and #description) property for elements in Form API array however are your responsibility to translate.
This video shows how a module can integrate with Drupal's theme system by exposing a new themeable item to the theme registry with hook_theme(), and then creates a function in order the customize the output of that themeable item. The then video demonstrates how the theme layer can override the markup defined with theme functions at the module layer.
This presentation provides an overview of the different phases of the form submission and validation process that Drupal uses with the Forms API. This will give you a good foundation for implementing forms yourself in the following videos.
This chapter shows how to cluster different form elements into fieldsets as well as how to expand the Forms API renderable array a tree that preserves the structure and hierarchy of the form. We'll expand the Form Fun example module and talk about the #tree property. This video uses krumo() and dsm() functions. You will need to download, install, and enable the devel module to use these functions. These functions allow you to see what variables are available to you. To accomplish the same task without using the devel module, you can add the following snippet to your module: drupal_set_message('' . print_r($vars, true) .'');
Note: There is a typo in the code used in this video. The function form_fun_tree()
is missing a parameter, and should be as follows function form_fun_tree($form, &$form_state)
.
This video shows the basic syntax for creating form dependencies with the states system. For a more information about the states system, then be sure to check out JavaScript form dependencies with the States System.
Note: There is a typo in the code used in this video. The function form_fun_states_form()
is missing a parameter, and should be: function form_fun_states_form($form, &$form_state)
.
This video walks through how to use the new hook_page_alter()
in Drupal 7 by transforming an unordered list into an ordered list. Because all of the content, region & blocks are stored within a renderable array before being output to the page, then this new hook allows modules and themes to make changes to page before it is fully rendered.
This chapter walks through how you can conditionally add either JavaScript or CSS to a content element as well as how to cache the content with Drupal's caching system. Additional notes: The cache will clear the first time cron.php is run AFTER whatever time you've specified as the cache expiration date in your code. Which you could easily calculate to be 30 seconds in the future and then store that timestamp in the database. However, you would also need to make sure that cron was running frequently enough to clear the cache every 30 seconds.
Additional resources
Render Arrays overview (Drupal.org)
This video walks through the basic use of Drupal 7's Render API for outputting content, and gives some hands-on experience in navigating and working with renderable arrays. We expand the Menu Magic module by creating several different kinds of page elements to show how renderable arrays work.