Drupal Module: ignite_drupal


Download Version 1.0 from Ownalbum.com:
http://www.ownalbum.com/files/download/fa2c84fad29772f621f60162d3be07fe....

WARNING: You must have a bit of CodeIgniter and MVC/OOP knowledge to be able to read the following

Ever since I was literally forced to work with drupal, I have always wondered about the following:

  1. What if I would be able to call CI controllers directly from my drupal functions/hooks, etc.
  2. What if I would be able to load anything I want into drupal and everything that's in the CI autoload automatically ?
  3. What if I would not change anything to the CI core so that CI may be uupdated at all times ?
  4. What if I would be able to call any Drupal API function inside my CI controllers ? Any Drupal function.
  5. What if I would be able to manage URI routing using both CI and Drupal.
  6. What if I would be able to setup a Drupal hook_menu() item, and that menu item's path would automagically call one of my CI controllers/method ?

Now all that, and more in this simple drupal module (see attachment)

How to use it ?

Step 1: Installing the drupal module.

  1. Unzip the ignite_drupal archive and copy the ignite_drupal folder in it to your drupal installation under sites/all/modules
  2. Go to your drupal admin area, click on modules and scroll down until you see the Ignite Drupal module
  3. Check the box beside that module
  4. Scroll down more and click the Save button

Step 2: Installing the CodeIgniter MVC

  1. Download the latest version of CodeIgniter from the CI website
  2. Unzip and copy the CodeIgniter you just downloaded to a folder of your preference inside your drupal installation. HINT: The ignite_drupal module will automatically look for the mvc folder. So should be: /path/to/drupal/mvc However you can put it in any folder you like. You'll just have to specify the folder name when you instantiate the CI front controller though.
    It is IMPERATIVE however to place it inside your drupal installation and not anywhere else. No deeper in the installation or outside of it.
  3. Now copy the contents of the folder: ci_core_patches in your mvc installation folder. It should write 3 patches to your application/core folder contents.
  4. To the CI-savy people: This just extends 3 Core components to make them ack-rite :-)

Step 3: Actually use it :-)
I'll just post some sample code from both drupal and one of my CodeIgniter controllers to see how it works:

CODEIGNITER PART
In one of my CI controllers I have the following code:

 if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class App extends CI_Controller {
        
    public function __construct(){
        parent::__construct();
    }


    public function index($uid = 69){
        // Let's not forget drupal API's user_load function. 
        $user_info = user_load($uid);
        print_r($user_info);
        
        /* Load and render some views if we wish
         * $this->data in my case is an array containing data to put in the views.
    $this->load->view('common/header', $this->data);
        $this->load->view('app', $this->data);
        $this->load->view('common/footer', $this->data);
        */
    }
    
}

/* End of file app.php */
/* Location: ./application/controllers/app.php */

DRUPAL PART
In one of my custom drupal modules I have the following code:

// "Implementation" of drupal's hook_menu.
function module_hook_menu() {

    $items = array();
    
    $items['class/method'] = array(
        'title' => t(""),
        'page arguments' => '',
        'access callback' => TRUE,
        'page callback' => 'call_class_and_method',
        'type' => MENU_CALLBACK
    );
    
    $items['codeigniter'] = array(
        'title' => t(""),
        'page arguments' => '',
        'access callback' => TRUE,
        'page callback' => 'codeigniter',
        'type' => MENU_CALLBACK
    );

    $items['blank'] = array(
        'title' => t(""),
        'page arguments' => '',
        'access callback' => TRUE,
        'page callback' => 'blank',
        'type' => MENU_CALLBACK
    );
    return $items;
}

function call_class_and_method(){
    // Start CodeIgniter
    $dci = new ignite_drupal();
    
    // This attempts to load the CI controller "class" and method "method"
    $CI = $dci->load_controller();
}
function codeigniter(){
    // Make Drupal logic here
    //........


    // Start CodeIgniter
    $dci = new ignite_drupal();
    
    // This loads the specified controller and method.
    $CI = $dci->load_controller("my_controller", "that_controllers_method");
}
function blank(){
    // Start CodeIgniter
    $dci = new ignite_drupal();

    /*
     * Assuming you have created a controller called blank with a method called index
     * you may now use all CI functions right here to achieve your ultimate logic goals
     * this way you get help from both Drupal and CI. Play with this anyway you want.
     */
    $CI = $dci->load_controller("blank", "index");
    
    // ignite_drupal module will automatically load all libraries and all the stuff inside autoload.php    

    // However we may also load our custom or core libraries manually.
    //$CI->load->library("my_library");
    
    // Use the CI libraries and everything else we want.
    
    // User it for sessions
    $CI->session->set_userdata("uid", "69");
    $user_id = $CI->session->userdata("uid");
    echo "<h1>The user id from the cookie session is: " . $user_id . "</h1>";
    
    // Use it for models ! :D
    $CI->load->model("model_name");
    $my_stuff = $CI->model_name->get_stuff_by_name($stuff_name);

    // Use it for anything else you want ! It accepts all CI's components.
    // ..............

}

A few things to note:

  1. Autoload is also in place. You may notice that the $CI you just instantiated already contains all things you autoloaded in your CodeIgniter config/autoload.php file
    Loading anything: libraries, models, config files, helpers.
    They all work out of the box.
    In your drupal function just go like:
    function menu_item_callback_or_any_other_drupal_function(){
        $dci = new ignite_drupal();
        $CI = load_controller("class", "method");
        
        /*
         * $CI has already autoloaded the stuff in autload.php however 
         * However this is how we load more stuff from CI inside Drupal
         */
        $CI->load->library("my_custom_CI_library");
        $CI->load->model("users_model");
        
        //  Demonstrate the use of both Drupal and CI stuff
        $user_info = user_load($nid);
        
        $CI->users_model->get_user_by_username($user_info->name);
    
        // Right ? Get the picture ?
    }
    
  2. Observe that you may send data form Drupal to CodeIgniter either by URI segments or you may just set the ignite_drupal property: $this->drupal_data like this:
    function menu_item_callback_or_any_other_drupal_function(){
        $dci = new ignite_drupal();
        $dci ->drupal_data = array("param_name" => "param_value");
        $CI = load_controller("class", "method");
    }
    

    This way, in your CI controller you may do:

    ...
    /* 
     * $segment_1 to $segment_n are the segments of URI you pass the controller. See CodeIgniter documentation on URI segments and URI routing
     * For instance, if you do not pass any uri segments, this method would only take $drupal_data as parameter.
     * If you pass one segment you would have $segment_1 and $drupal_data as parameters.
     * For more info you may look at ignite_druplal.module and modify it to suite your better needs.
     */
    public function controller_method($segment_1, $segment_2, $drupal_data){
        // This outputs the array sent via $drupal_data
        print_r($drupal_data);
    }
    ...
    
  3. Observe that you may send data from CI to Drupal by setting properties to $this inside your controller like... well... this:
    ...
    public function controller_method($segment_1, $segment_2, $drupal_data){
        $this->data_for_drupal = "something";
    }
    ...
    

    and then, in your drupal ignited function you go like:

    function menu_item_callback_or_any_other_drupal_function(){
        $dci = new ignite_drupal();
        $CI = load_controller("class", "method");
        $data_from_ci = $CI->data_from_drupal;
        print_r($data_from_ci);
    }
    

Anyway I hope this little documentation is clear enough.
I will soon add this somewhere in google code as opensource.
Until then, here's the download link for version 1.0:
http://www.ownalbum.com/files/download/fa2c84fad29772f621f60162d3be07fe....

For questions please ask: phpcip@gmail.com

P.S.: This has been tested under Drupal 7 but should really work with any Drupal version since it doesn't depend on any other Drupal module.

Comments

rizktony81’s picture

Hey,

the link you provided to download the ignite drupal module is not working.
can you please fix this?

Thanks,
Tony

vsinghfullestop’s picture

please check module download link that is not work provide correct link.