Archive

Archive for the ‘Drupal’ Category

Drupal 7 Bootstrap : Load Drupal from an external PHP script

February 11, 2014 1 comment

This is how to load or bootstrap Drupal 7 into an external script.

We are doing a full Drupal bootstrap here. There are many phases which you can bootstrap, for eg bootstrap only the database layer. You can read about them on the Drupal site

// display all errors
error_reporting(E_ALL);
ini_set("display_errors", 1);

// drupal root and include
define('DRUPAL_ROOT', '/path/to/drupal');
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';

// you might need to define some of these if the script is not called from a webpage
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';             // required, else drupal will complain
$_SERVER['HTTP_HOST'] = 'www.mysitename.com:8888'; // optional but required for multi site configs
$_SERVER['REQUEST_METHOD'] = 'GET';                // optional
$_SERVER['SCRIPT_NAME'] = '/' . basename(__FILE__);// optional

// Load Drupal
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// Drupal is fully loaded and all Drupal functions are available - your code goes here
Categories: Drupal

Drupal 6 theming a fieldset

  • Drupal 6 fieldsets can be themed by supplying the #theme attribute for the fieldset
  • #theme = your custom fieldset theming function
  • you need to declare this theme function in the modules hook_theme function
  • drupal will automatically look for theme_your_fieldset_theme_function()
  • see the complete working example below
/**
 * hook_menu
 * @return array
 */
function test_menu() {

    $items = array();

    $items['test'] = array(
        'title' => 'Test',
        'page callback'	=> 'drupal_get_form',
        'page arguments' => array('test_form'),
        'access callback' => true,
        'type' => MENU_NORMAL_ITEM,
    );

    return $items;
}

/**
 * hook_theme
 * theme registry - register all theme functions here
 * "theme_" is added to all the functions
 *
 * @return array
 */
function test_theme() {
    return array(
        'test_form' => array( 'arguments' => array(),),
        'address_fieldset' => array( 'arguments' => array(),),
    );
}

/**
 * form with fieldset
 * @return array
 */
function test_form() {

    $form = array();

    // first name
    $form['first'] = array(
        '#type' => 'textfield'
    );

    // last name
    $form['last'] = array(
        '#type' => 'textfield'
    );

    // address fieldset
    $form['address'] = array(
        '#type' => 'fieldset',
        '#title' => 'Address',
        '#collapsible' => true,
        '#collapased' => false,
        '#theme' => 'address_fieldset' // declare this in the module theme registry
    );

    // city
    $form['address']['city'] = array(
        '#type' => 'textfield'
    );

    // zip
    $form['address']['zip'] = array(
        '#type' => 'textfield'
    );

    // submit
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Submit',
    );

    return $form;
}
/**
 * theme function for the form
 * @param $form
 * @return string
 */
function theme_test_form($form) {
     $output = "<table>
         <tr><td>First Name: </td>
             <td>".drupal_render($form['first'])."</td>
         </tr>
         <tr><td>Last Name: </td>
             <td>".drupal_render($form['last'])."</td>
         </tr>
     </table>";
     // render the fieldset
     $output .= drupal_render($form['address']);
     // render the rest of the form
     $output .= drupal_render($form);
     return $output;
}

/**
 * theme function for the address fieldset
 * @param $form
 * @return string
 */
function theme_address_fieldset($form) {
    $output = "<table>
        <tr><td>City: </td>
            <td>".drupal_render($form['city'])."</td>
            <td>Zip: </td>
            <td>".drupal_render($form['zip'])."</td>
        </tr>
    </table>";
    return $output;
}


Categories: Drupal

Drupal 6 Bootstrap : Load Drupal from an external PHP script

September 23, 2012 Leave a comment

You can load drupal into an external PHP script. The external PHP script looks like this

//change working directory to Drupal root directory
chdir('/var/www/drupal/');

//include bootstrap file
require_once 'includes/bootstrap.inc';

// bootstrap drupal
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); 

/* you can then call drupal functions etc like 
 * db_query()
 * noad_load()
 */ 

$global $user; 
/* remember if you want have user login working in your external script, 
 * you need to set ( uncomment )  $cookie_domain = 'your domain name'; in the drupal settings file
 * if on localhost $cookie_domain = 'localhost'; 
 * now log into your drupal instance from a browser 
 * open a tab in the same browser and call your external PHP script 
 * the user details must be populated in $user
 */ 
 echo "<pre>;
 var_dump($user);
 echo "</pre>";
Categories: Drupal

Drupal 7 installation and Clean URLs

January 6, 2011 7 comments

This is a short procedure to install Drupal 7 and enable clean URL’s . This is for setting a drupal site on Ubuntu .

Please note that,

  1. This is a specific type of installation and may not work in many cases – different installations have different requirements
  2. you can use your own custom database, user and folder names

I assume that

  1. You are an administrator on the system / server
  2. You have installed latest stable Apache Web server
  3. You have installed latest stable PHP
  4. You have installed latest stable MySQL database

Install Process

  1. First create a database for drupal in MySQL – call the database “drupal”
  2. Create a user called “drupal_admin” and grant him ALL privileges on the database “drupal” – note down user name and password
  3. Download Drupal 7 and unzip it
  4. Copy Drupal 7 folder to “/var/www”
  5. So path will be  “/var/www/drupal-7.0”
  6. For simplicity and easiness, create a symlink to the drupal-7.0 folder – call it “drupal”
  7. So now drupal -> drupal-7.0
  8. Navigate to drupal/sites/default
  9. Create a folder called “files” inside the “default” folder – please name the folder as “files” as this is drupal standard folder to store all uploaded files
  10. Give write access to the “files” folder to the user “www-data” ( i.e the web server user)
  11. Next copy the file “default.settings.php” to “settings.php” ( both these files must be in the same folder now “drupal/sites/default” )
  12. Give write permissions to “settings.php” for the user “www-data”
  13. Open the "settings.php" and change this line $update_free_access = FALSE; change it to $update_free_access = TRUE;
  14. Locate the $databases = array() line - we have to specify the drupal database here
  15. Change the above line to
$databases['default']['default'] = array(
'driver'=>'mysql',
'database'=>'drupal',
'username'=>'drupal_admin',
'password'=>'password',
'host'=>'localhost',
'prefix'=> '',
);

16. Open a  web browser and go to the URL http://localhost/drupal/install.php

17. Follow the steps till you finish the installation

18. Your drupal site must be up and running at http://localhost/drupal

19. Remove write permissions to “settings.php”  make it read-only

20. Open the "settings.php" and change this line $update_free_access = TRUE; change it to $update_free_access = FALSE; 
( basically undoing step 13)

Clean URL in drupal 7

21. On your drupal site navigate to Configuration -> Search and Meta data -> Clean URLs -> perform the Clean URL test ( there will be a button )

22. If your site is ready for clean URLs then you will see a check box which says “Enable clean URL” – check it and save and you are done

23. If this is not the case then do the following

24.  you have enable the apache rewrite module (http://drupal.org/node/15365)

25. your apache config file (/etc/apache2/apache2.conf) must have the following  2 lines in it – if not then add them – this tells apache to look for a .htaccess file for settings in the folder “/var/www/drupal”

     <Directory /var/www/drupal>
        AllowOverride All
   </Directory>
   AccessFileName .htaccess

26. Make sure you have a “.htaccess” file in the “drupal” folder – that might be hidden

27. Edit the “.htaccess”  file in the “drupal” folder and uncomment the line RewriteBase /drupal ( Remove the # before this line)

28. Restart apache

29. Now again go to Configuration -> Search and Meta data -> Clean URLs -> perform the Clean URL test

30 This will allow clean URLs

Categories: Drupal

Drupal tips and tricks

December 2, 2010 Leave a comment

Get the URI/URL of the current page

$current_uri = check_plain(request_uri());

 

If your URL is https://mysite.com/drupal/node/6 – $current_uri will be /drupal/node/6

Categories: Drupal

Drupal end session on closing the browser

December 2, 2010 Leave a comment

If you want to destroy a session when the browser closes , then check the settings.php file in the drupal installation the value to look is

ini_set('session.cookie_lifetime',  200000);

This is the default time ( 200000 seconds ) up till which Drupal will keep the session of a user active. Even if the user closes the browser without logging out, this session is kept active

This can be dangerous in some situations, for example if a single computer is used by different people and one person ( Person 1 ) just closes his browser ( without logging out )

If  the next person ( Person 2 ) tries to access the same site, then the session of the Person 1 is still active – this will allow Person 2 to see all data related to Person 1 without having to log in

To avoid this, we can make Drupal to destroy the session as soon as someone closes the browser by changing the setting to below

ini_set('session.cookie_lifetime',  0);

A value of ZERO means destroy the session immediately when the browser closes

This can also be annoying sometimes in single user environments – when the user has to give a password each time he closes and opens the browser – so use according to requirements 🙂

Categories: Drupal

Upgrading Drupal

September 28, 2010 Leave a comment

Follow the below Steps to upgrade drupal – this is up till drupal v 6.19

Most Important Steps

  1. Backup the Drupal Database
  2. Backup any external database that Drupal is referencing ( not required but a good practice )
  3. Backup the entire Drupal installation directory (mine is in /usr/share/drupal6)
  4. Backup the “sites” folder – very important
  5. For single sites, the config file is located in /sites/default/settings.php
  6. For multi-site each site folder has its own settings.php
  7. Disable all contributed modules – keep only Drupal core modules active
  8. Switch to a default theme (Blue Marine or Garland)
  9. Put the site in Maintenance Mode

I have my drupal installation like this

Drupal installation folder = /usr/shr/drupal6

This directory(/usr/shr/drupal6) has 2 symlinks (sites ->/etc/drupal/6/sites and profile-> /etc/drupal/6/profiles)  and other files like cron.php, index.php, robots.txt

Next Steps (Please note that your system paths may be different – this is just a guide)

  1. Log into your Drupal web site as a Super User ( Drupal uid = 1 )
  2. Download latest stable release of Drupal for the drupal site
  3. Unzip it to the Desktop
  4. Do the below operations as a Super User for the Operating System (or FTP or make sure the folders have the correct permissions)
  5. Delete all files in the Drupal installation folder
  6. Copy new Drupal installation files from the desktop to default Drupal installation folder (/usr/shr/drupal6)
  7. Now copy back the “sites” folder and “profile” folder to the default Drupal installation folder (/usr/shr/drupal6)
  8. If your robots.txt file was edited previously, copy the old one and over write the new one
  9. In your Drupal site, enable all required modules
  10. Next access the URL http://www.yoursite.com/update.php ( yoursite.com is the name of your website )
  11. It should tell you that the database will be updated – continue and finish the update
  12. Enable the themes , clear cache and check site for errors if any
Categories: Drupal