- View Offline
- Creating your first module
- Interacting with hooks
- Working with permissions and roles
- Controlling access
- Adding a menu item to an admin interface
- Using the Form API (FAPI) to quickly create a form
- Creating custom form validation
- Manually creating a custom content type
- Validating user input
- Importing content using feeds
- Creating a block
- Understanding best practices and coding standards
Skill Level Advanced
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.
Sign up for a Premium Membership to download courses for Internet-free viewing.
Watch offline with your iOS, Android, or desktop app.Start Your Free Trial
After signing up, download the course here or from the iOS/Android App.