Monday, April 30, 2012

Feature Driven Development : Distros

Moving the database settings from one one instance to other, has always been a menace. Not just in Drupal but web-development in general. Thanks to features module we today have solved this problem significantly if not completely.

A feature is a collection of Drupal entities which taken together satisfy a certain use-case. And what features module does is enables the capture and management of features in Drupal. A features module takes different site building components from modules(with exportables) and bundles them together in a single feature module. Examples of modules with exportables are views, cck, contexts and panels. So in simple terms features bundles together the varoius components from modules like views, cck, contexts and panels.

To get more clarity lets take an example. Let us assume that we are exporting a “Photo Gallery” feature through features module. The “Photo Gallery” feature will typically consist of two content types images and gallery, a view for latest galleries, a view for images in the gallery and a view for the featured gallery. Three to four image-cache presets. Few admin settings. So it would be a herculean task to export all these features individually. With features when you select one items it autosuggest all the other realte entities that should be exported. Using contexts simplifies this process further more.

With the advent of features module, a new trend has started in the Drupal Distro space and its called Features Driven Development. The idea here is to keep the naming convention generic enough so that all you need to create a new Distro is export your features through feature module and create a simple profile with the modules exported by feature enabled by default. Thanks to features module every Drupal developer can now also be a Distro contributor, if he can generalise the requirements. A whole new set of Distros or built base don this concept.

Wednesday, January 11, 2012

Drush : Set password, watchdog entries and run sql-query

The more you learn about Drush, the more you will love it. Generally most of the developers use Drush only for download modules, install modules and clear cache. But there is more to Drush than that.

1. You have a Drupal instance and you don't remember the password for the admin.

drush user-password admin  --password="IamNotadmin$123"

converting the password to MD5 and storing it in DB drush will take care :)

2. Recently I had an instance of Drupal to which I was not able to login. I wanted to check out the watchdog entries, but as I was not able to login I had to check it from db. I thought drush may help me out and it did.


drush watchdog-show  

You can also use watchdog-list which will give you more control. This throws up a set of options like cc does and you can select what you want
drush watchdog-list 

3. If you are drupal developer and you are using, it's a safe bet to assume that you are using sql-cli. But the pain with that is that you need to remove the braces from the table names to actuall run the query.


drush sql-query "SELECT u.name, u.status from {users} u where uid = 1"



And you can see the result of the query ;) Saves a  lot of time. Doesn't it?

Tuesday, January 3, 2012

Drupal 6 : PHP snipper to get a list of all the content types with respective fields.

Here is small snippet of php code that will help you get a list of all Content types in an instance along with the fields within the content type. It came handy to me when I wanted to give a list of conent types with fields to one of my clients for remapping them to the new structure that they wanted to build.

  
  $content_types = node_get_types();
  $output = '';
  foreach($content_types as $content_type => $content_type_object) {
    $output .= "" . $content_type_object->name . "";
    $output .= '
';
    $fields = _content_type_info();
    $fields = $fields['content types'][$content_type]['fields'];
    $ouput = '';
    foreach ($fields as $field_name=>$field) {
      $output .= $field['widget']['label'];
      $output .= '
';
    }
  }

Wednesday, December 28, 2011

Using drush to get a list of enable modules

If you just wanted to make a list of all the modules which are installed in your instance. Don't waste time, be smart write a command using drush.

  1. List all modules and themes : drush pm-list
  2. List only modules : drush pm-list --type=Module
  3. List all modules except core modules : drush pm-list --type=Module --no core
  4. List all enabled modules except those from the core : drush pm-list --type=Module --no core -- status=enabled
  5. List all enabled modules except those from the core which belong to the CCK package : drush pm-list --type=Module --no core --package=CCK status=enabled
If you want more info about the pm-list
  1. drush help pm-list


Tuesday, October 4, 2011

Some frequently used commands in Ubuntu Linux for SQL and DRupal

MYSQL : 
To create a database, to create a user and then to give all the permissions on that database to the user.



create database my_database_name_to_be_created; 
create USER 'username_to_be_created'@'localhost' IDENTIFIED BY 'password';
GRANT ALL ON  my_database_name_to_be_created.* TO 'username_to_be_created'@'localhost' ;
FLUSH ALL PRIVILEGES;



Linux/ Mysql : 
To take a dump of the data base.

mysqldump -u root -p databased_to_be_dumped> dump_file_name.sql;


To create a new database
mysql -u root -p name_of_the_database_to_which_dump_is_loaded < dump_file_name.sql

Linux : 

find . -maxdepth 1 -exec mv {} .. \;

Monday, July 4, 2011

How to remove few formatting options from CK editor.

  • Download and install CK editor.
  • Make sure that you have given the permissions.
  • Associate the roles with the CK editor profiles.
  • Now edit the appropriate profile. For example if you want to edit the CK editor for the authenticated user then you will have to edit the Advanced profile editor.


  • Click open the "Advanced Options" fieldset.





And copy paste the following code so that it looks like the image on the right.
format_tags = 'p;pre;h4;h5;h6'
removeFormatTags = 'b;big;code;del;dfn;em;font;i;ins;kbd;q;samp;small;span;strike;strong;sub;sup;tt;u;var;h1;h2;h3' 




How to make titles optional and generate title from body text

Head to automatic nodetitles , download and install the module.

Goto /admin/content/node-type/entry under the "Automatic title generation" field set select the second or third option and paste the code below.

//the least number of words in a scentence when trying to extract a complete scentence
$w_sentence= 1;
//if the scentence obtained contains more words than this it will be truncated to this many words and "..."
$w_wordnum = 10;
//if the number of characters after all truncation is above this number it will be truncated to this many characters and "...",  This is to protect agains extremely long words
$w_maxchar = 50;

// this is the name of the cck node token you want to use for example"
$bodyfield = '[field_sms_text-formatted]';


function summarize($paragraph, $limit)
    // cuts the 
       {
           $tok = strtok($paragraph, " ");
           while($tok)
           {
               $text .= " $tok";
               $words++;
               if(($words >= $limit) && ((substr($tok, -1) == "!")||(substr($tok, -1) == ".")))
                   break;
               $tok = strtok(" ");
           }
           
           return ltrim($text);
       }

function shorten_string($string, $wordsreturned)
     /*  Returns the first $wordsreturned out of $string.  If string
    contains more words than $wordsreturned, the entire string
    is returned.
    */
    {
    $retval = $string;    //    Just in case of a problem
    $array = explode(" ", $string);
    if (count($array)<=$wordsreturned)
    /*  Already short enough, return the whole thing
        */
        {
        $retval = $string;
        }
    else
    /*  Need to chop of some words
        */
        {
        array_splice($array, $wordsreturned);
        $retval = implode(" ", $array)."...";
        }
    return $retval;
    }     

// if somone managed to put in html we have to clean it out
$bodyfield = strip_tags($bodyfield);    

// multiple spaces in my editor were correctly converted to hardspaces but it confuses drupal so we need to remove them
$bodyfield = str_replace(' ', " ", $bodyfield);
$bodyfield = trim($bodyfield);


$bodyfield = summarize($bodyfield, $w_sentence);
$bodyfield = shorten_string($bodyfield, $w_wordnum);

// to protect agains extremely long words we also have a character based rule
if(strlen($bodyfield) > $w_maxchar )
    {
    $bodyfield = substr($bodyfield,0,$w_maxchar)."...";
    }
    
print check_plain($bodyfield);