Wednesday, September 9, 2015

Programmatically Creating a new Content Type in Drupal7

To create a module, in the sites/all/modules directory of your Drupal installation create a new directory named mymodule.
 Create following three files in mymodule folder:
·– will contain the information about the module so that Drupal will recognize it and show it in the list.
·         mymodule.install – will have details about things you are going to install or uninstall for the module.
·         mymodule.module – will contain code to implement the Drupal hooks used by the module.
Once you have made the directory, open and add the following code to it:
name = mymodule
description = A new custom content type creation by mujuonly
package = mymodule Custom node Type
version = "7.x-1.12"
core = 7.x
files[] = mymodule.install 
files[] = mymodule.module 
The above simply defines some parameters for our module (mymodule) so Drupal can use it and display the information about  mymodule. Once this is done we should see mymodule in the module list as shown below.

Installing and Uninstalling Content Type
Once we have the basic files for the module in place, we can start writing code for installing the custom node type. The first hook we need to implement is hook_node_info. Using this hook, a module can define one or more node types in Drupal. The hook implementation returns an array defining the new node type which the module is going to add.
To implement this hook, add the following code to mymodule.module
 * Implements hook_node_info()
function mymodule_node_info() {
    return array(
        'news' => array(
            'name' => t('News'),
            'base' => 'news',
            'description' => t('You can add  News here'),
            'has_title' => TRUE,
            'title_label' => t('News title')
The implementation returns an array defining a new node type news  along with some of its properties, such as its name, description, title, and base.
As we have defined that this node type has a title, we need to show the title text field when the form for this node is displayed to add content. To do this, we will have to implement the hook hook_form.
The hook_form is used to show the form for create/edit nodes. The hook is implemented in mymodule.module as follows:
 * Implement hook_form()
function mymodule_form($node, $form_state) {
    return node_content_form($node, $form_state);
We simply use the Drupal API which gives an implementation of hook_form and adds a title field provided the node definition has the has_title attribute set .Once we have got this done, we need to implement the hook_install hook to add the body field to the new node type.
Add the implementation to mymodule.install is as follows:
 * Implements hook_install().
function mymodule_install() {
    $types = node_type_get_types();|
We first save all of the new node types created by different modules by calling the Drupal API node_types_rebuild() function. Then we get all of the node types and call node_add_body_field() on our type to add the body field.
Once we have done this we can enable mymodule which will install our new node type. Then we should be able to see our new type when we click on add content as follows:

If we add a new news, it will also be seen on the Drupal front page.
All modules in Drupal should clean up any data they create in the database or any types they have added when it is uninstalled by the user. To support this, we have to implement the hook_uninstall in ou mymodule.install file as follows:
 * Implements hook_uninstall().
function mymodule_uninstall() {
    $customtype = 'news';
    $sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
    $result = db_query($sql, array(':type' => $customtype));
    $nodeids = array();
    foreach ($result as $row) {
        $nodeids[] = $row->nid;
We first find out all of the node IDs which are nodes of our installed content type. Once we have collected the IDs, we use the API function node_delete_multiple() to delete multiple nodes. Then we use the node_type_delete() function to delete our node type.

Now if we uninstall our module, all of the nodes of our type and our type itself should be deleted.

No comments:

Post a Comment