Friday, 30 September 2011

Configuring drush on Mac OS X Lion

I downloaded the drush from the drupal website at http://drupal.org/project/drush. I downloaded the 7.x-4.5 version -  I was using mac os x and if you are also using mac os or any other flavour of linux - you should download the tar.gz file. 

Drush works best on linux/mac os x - it works on windows as well but has its limitations, although things are improving but you will need to install additional software that allows you to use many drush functions. If you are interested to look at how to install drush on windows then have a look at drush.ws. I did install on my old windows pc as well, got most of it working.

Coming back to the topic, we are installing on mac os x - so here goes.

The accompanying readme.txt file is quite good in showing what is required, and it really is straight forward. Albeit there are a few differences and may be you would like to customise your installation a little bit - but this is what i did.

The first step was to download and extract the drush-7.x-4.5.tar.gz file to a folder out of web root as recommended.

Then make 'drush' executable, I installed drush in my home folder hence my command to make drush executable was

chmod u+x ~/drush/drush

a quick jump to ~/drush folder and enter drush at the $ prompt, as you have made drush executable your should see drush options, a good way to learn options is to use drush help.

if all is fine, we can create a symlink to /usr/bin so that drush is always in the path. The command to do this of course is 

ln -s ~/drush/drush /usr/bin/drush

you may require sudo access so prepend sudo to the above command.

Thats all, i tried drush and all worked swell! I was able to download drupal, install modules, enable/disable modules and everything. I hit a block when i was trying site-install option.Drush has a powerful option that lets you install a new site from the command prompt itself, and it is extremely fast.

The usual syntax for site-install is as follows

drush si standard --db-url=mysql://root:xxx@localhost/dru7 --site-name="Drupal Example" --site-mail=xyz@xyz.com

I got an error which was 

sh: mysql command not found.

Thankfully it was an easy fix, it was because though mysql was installed and working, mysql was not working from the path where drush was installed. so symlinking to the rescue i did the following

ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql

Thats it - all working fine now and i am enjoying working with drush.

Saturday, 17 September 2011

How do you expose your legacy database tables to views in drupal 6 Part 2


Continuing from Part 1 of the post where we defined custom data table to make drupal aware of our legacy data, we also want to define custom filters that can be used by the views system to define conditional views.

in the hook_views_data() or our example code in part one, the candidate_views_data() function you can see that a field was defined as below:

    $data['candidate']['candidate_gender'] = array(
        'title' => t('Gender'),
        'field' => array(
          'click sortable' => TRUE,
        ),
        'filter' => array(
            'handler' => 'candidate_handler_filter_gender',
        ),
    );

notice the filter key in the $data array, we want custom data to appear here, for example 'Male' and 'Female', so we have defined a custom handler by the name 'candidate_handler_filter_gender'.

to make drupal aware of your handler, you need to define them in hook_views_handlers() the definition will look something like this.

function candidate_views_handlers(){
    return array(
      'info' => array(
          'path' => drupal_get_path('module','candidate'). '/views',

      ),
      'handlers' => array(
          'candidate_handler_filter_gender' => array(
              'parent' => 'views_handler_filter_string',
          ),
      ),
    );
}

having defined this handler, now you need to place a new file called candidate_handler_filter_gender.inc in the earlier defined views sub folder. and the code that goes in this file is:

class candidate_handler_filter_gender extends views_handler_filter_string{
    function value_form(&$form, &$form_state) {

    $form['value'] = array(
    '#type' => 'select',
    '#title' => t('Gender'),
    '#options' => getGender(),
    );

    return $form;
  }

}

getGender() is a function that just returns a keyed array for 'M'=>'Male', 'F'=>'Female'.

Thats all, now your views interface should hook into your custom module and be aware of the tables, and custom handlers available. 

Remember to clear caches if something does not appear.

How do you expose your legacy database tables to views in drupal 6 Part 1

This is a 2 part post, that shows how I exposed a legacy table to views in drupal and used the incredible power of views to create many combinations static and conditional views. This was very helpful for my client.

I had a requirement that warranted exposing existing database table to views, so that views can contain data from this table.  To enable this to happen, the following steps need to be taken.

In a custom module you need to first define the views_api hook as following:

function candidate_views_api(){
    $view = array();
    $view = array('api' => 2,
                  'path' => drupal_get_path('module','candidate'). '/views');
    return $view;
}

candidate module was a custom module which I already had, so it made sense to add this hook here. 

The important bit here is telling the drupal sub-system where the views file will reside. 

Then create a file with your <modulename>.views.inc in my case it was candidate.views.inc, this file should have two hooks, one hook_views_data() that exposes your table to drupal and then one which can define custom handlers for you data.

function candidate_views_data(){
    $data = array();
    $data['candidate']['table']['group'] = t('Candidate');

    $data['candidate']['table']['base'] = array(
        'field' => 'candidate_ref',
        'title' => t('Candidate'),
        'help' => t("Candidate content."),
        'weight' => -10,
    );

    $data['candidate']['candidate_ref'] = array(
        'title' => t('Candidate Ref'),
        'field' => array(
          'click sortable' => TRUE,
        ),
        'filter' => array(
            'handler' => 'views_handler_filter_string',
        ),
    );    
  
    $data['candidate']['candidate_first_name'] = array(
        'title' => t('First Name'),
        'field' => array(
          'click sortable' => TRUE,
        ),
    );

    $data['candidate']['candidate_gender'] = array(
        'title' => t('Gender'),
        'field' => array(
          'click sortable' => TRUE,
        ),
        'filter' => array(
            'handler' => 'candidate_handler_filter_gender',
        ),
    );

    return $data;
}

I have removed all the other fields from the above function for brevity of the post, and it is enough to explain the point.

some explanation about the code above.

$data['candidate']['table']['base']

tells drupal what is the base table, base tables, because you can also join tables using a unique field in each table, for eg. you may want to join the candidate table with the node table.

then definition of all your fields is just like any other drupal array, important things of note is the field which you can decide it to be sortable, and the filter. in case of views when you want to define views based on filters and you want these to appear in conditions options while defining views through the admin screen.

Part 2 will show how to define custom handlers for your views.

Add behat in Symfony 4

To add behat in Symfony 4 i have generally followed this link https://blog.rafalmuszynski.pl/how-to-configure-behat-with-symfony-4/ How...