Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
Extend your Drupal 7 sites with custom modules, which allow you to create everything from admin interfaces to forms. Author Jon Peck describes how modules extend your base Drupal installation, then walks through how to write your own module with a practical example featuring geo-positioned alternative energy centers. The course also describes how to control access to site features, create new content types, build forms, understand data persistence, embrace coding standards, and much more.
Previously, I demonstrated how to add persistent variables to module, then how to remove them cleanly using hook install and hook uninstall. When creating and destroying a content type the same hooks are used, which is logical. Open the IDE and navigate the windfarms. install file and navigate to hook_install following the message to the user. I'm going to define the content type itself using an associative array. Start with the variable definition named content type, Content type definition, content_type = array. The order of the keys is unimportant, but I'll use a logical progression.
First, the type of content. This is a machine name. All lower case with spaces instead of underscores. In this case, I will use windfarm as the name of the content type. Next, a human readable name for the content type. I will use the T variable for the Translation function, since hook installed for a module can be called during site installation. Name is Wind Farm, following the name a brief description.
A Wind Farm, including location. The next key is for the title label, title_label. This is shown on the edit page. Usually it's just title. This is used as the heading of the page. In the case of a wind farm it doesn't have a title, it has a name. Based on the data set that I'll import later, I'll use a more generic label of facility name to cover the different types of wind farms.
A content type has a base which refers to the source of hooks that are used for various events such as loading and saving. Drupal provides a number of these by default with nodes. To use them, I must explicitly declare that I'm going to use node content as the base, base, node_content. Finally, there is a flag that needs to be set that indicates with this custom type was added via the add content type interface.
This isn't actually true, but it gives site administrators at additional flexibility if they like to manipulate the content type, custom TRUE. There are a large number of settings for a content type, but most of them can be left on their defaults. Drupal offers a function that will go through the settings and only set the ones that are missing and I'll use that to complete the content type definition for a node. Set remaining definitions with defaults.
Node_type = node_type_set_defaults which takes the parameter of the content type. Now that the base structure is complete, save the content type using the function node_type_save which takes the array structure that I defined as the parameter. Save the content type, node_type_save, node_type. Later on, I will fully define how to remove the content type and everything associated with it.
For now, just save the windfarm.install file. At this time the base content type has been added with only a title. If I were to uninstall the module then reinstall the content type for wind farm would be created, but it wouldn't be very useful in that it would only have one editable field, the facility name. I'll cover adding custom fields in the next segment.
Find answers to the most frequently asked questions about Drupal 7 Custom Module Development.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.