Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Create a custom module in Drupal 7 to create a list page of custom content type

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 922
    Comment on it

    Here I am giving a brief idea of creating a custom module for newbie.

    For taking an example, I am creating a list of contents of my custom content type.

    However for creating custom pages, we can use Views module and it is highly customizable. But I am creating this list using custom module for better understanding.

    So for creating a start, I am taking a content type "Downloadproduct" and I have to create list of all content of this type with a download link. For this I am assuming that you have created a "Downloadproduct" content type.

    Step 1: Create a folder "mydownload" under "/sites/all/modules" folder.

    Step 2: Create 3 blank files mydownload.info, mydownload.module, mydownload.tpl.php

    Step 3: Add following code into mydownload.info:

    ; $Id: mydownload.info some_more_info
    name = "My custome module module"
    description = "Basic custom and hook functions to add a custom list"
    package = "Mydownload"
    core = 7.x 
    

    Step 4: Open mydownload.module file and add following code in it:

    <?php
    // $Id: mydownload.module 
    /**
     * @file
     * Basic hook funcions related modification.
     *
     */
    function mydownload_menu() {
      $items = array();
        $items['mydownload'] = array(    //This will be url of your page
            'title' => t('Download products'),  //This will be title of your page
            'page callback' => 'mydownload_downloads',  //You have to create a function for your logic and data
            'access callback' => TRUE,
            'type' => MENU_LOCAL_TASK,
        );
        return $items;
    }
    
    /**
    * function for list all downloadable product
    **/            
    function mydownload_downloads() {
      drupal_set_title('Download Products');
        $query = new EntityFieldQuery();
        $query->entityCondition('entity_type', 'node')
                ->entityCondition('bundle', 'downloadproducts') //your content type
                ->propertyCondition('status', 1);
    
        $result = $query->execute();
        $nids = array_keys($result['node']);
        //echo "<pre>"; print_r($nids); die;
        $nodes = entity_load('node', $nids);
        //echo "<pre>"; print_r($nodes); die;
    
        return theme('downloads', array('nodes' => $nodes)); //From here you are calling mydownload_theme function 
    }
    
    
    /**
    * Implementation of hook_theme to assign view file
    **/    
    function mydownload_theme() {
      return array(
        'downloads' => array(
          'template' => 'mydownload',  //you have assigned html file here
          'arguments' => array('nodes'=>NULL) // Variable passed to html file. NULL is only placeholder
        ),
      );               
    }
    

    Step 4: Now your server side code is ready and you have to theme your result in HTML template file. We have done this using mydownload_theme() function and assigned mydownload.tpl.php file for it. Now open mydownload.tpl.php and put following code in it:

    <?php global $base_url;?>
    <?php $themePath = drupal_get_path('theme',$GLOBALS['theme']);?>
            <?php foreach ($nodes as $node) {?>   
                  // Add your html here
            <?php }?>
    

    Step 5: Now goto your module listing page and enable module "mydownload" and access url http://mysite.com/mydownload

    I hope this will help someone.

    NOTE: Please check your variable names and content type names as this is only a example code base and given only for logical referal.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: