Thursday, November 24, 2011

JW Player module now supports plugins

The JW Player module, which is currently in development for Drupal 7 has just got the feature of being able to support plugins for presets. With this feature, it is possible to plug in all the add ons availabe from Longtail video.

JW Player plugins

There is also a Drupal preset plugin created for Google Analytics Pro2 JW player available in my sandbox. Custom modules can implement plugins by using the newly introduced hook_jw_player_plugin_info() in the JW Player module. The format to use this hook as described in jw_player.api.php is:

/**
 * Implements hook_jw_player_plugin_info()
 *
 * @return array Associative array of plugins keyed by actual plugin id
 */
function hook_jw_player_plugin_info($preset) {
  // Create a plugin keyed by its actual plugin id
  $plugins['foo'] = array(
    'name' => t('Foobar'),
    'description' => t('A plugin to do foobar'),
    // Note: Each option should be in a valid FAPI format, as it is directly referenced in the preset settings form,
    // except the '#title' may be omitted for the name of the option to be taken as default
    'config options' => array(
      'accountid' => array(
        '#type' => 'textfield',
        '#required' => TRUE,
        '#size' => 15,
        '#default_value' => 'bar'
      ),
      'param2' => array(
        '#type' => 'select',
        '#options' => array('TRUE' => 'TRUE', 'FALSE' => 'FALSE'),
        '#default_value' => 'TRUE',
        '#description' => t('Enables the controls on an item when playing')),
    ),
  );
  return $plugins;
}

As described above, the plugin is an associative array keyed by its actual plugin name as required by the player. The data basically consists of a friendly name and description, followed by configurable options that the site-administrator/editor can set per preset, which would look something like:

JW Player preset settings
If the preset plugin is enabled, when viewing the source code of your page that contains a JW Player video, you should be able to see the plugin added to the player code; something like:


Wednesday, November 09, 2011

How to use hook_query_alter() to alter Views in Drupal 7

Sometimes you need to alter a Views query by using the regular SelectQueryInterface methods without having to use the Views data structure in hook_views_query_alter(). It may get tedious at times working out all the views relationships, and arguments separately and adding a simple 'groupby' may get tricky if you do not have aggregation set in the view. It could also be that your hook_views_query_alter() runs a little too early for the $query to be modified.

Luckily, when the Views query is built in views_plugin_query_default::query a unique tag is added to each view of the form 'view_<view_name>':

// Go ahead and build the query. 
// db_select doesn't support to specify the key, so use getConnection directly.
$query = Database::getConnection($target, $key) 
  ->select($this->base_table, $this->base_table, $options) 
  ->addTag('views')
  ->addTag('views_' . $this->view->name);

Note: views_plugin_query_default::execute adds access data to the query which should be used for building access rights to the query during execution.

So, in your hook_query_alter(), you could identify the view based on its tag and modify the query like any other SelectQuery. I found this easier than using hook_views_query_alter() many times.

Example to group by a specific field: 

function hook_query_alter(QueryAlterableInterface $query){
  if ($query->hasTag('view_my_custom_view')) {
    $query->groupBy('n.type');
  }
}

Tuesday, November 08, 2011

Themeing EVA views using custom templates [patch]

Entity Views Attachment lets you attach a view to a Drupal 7 entity. This means when viewing a node, you could display for example, a list of other nodes by the same author. See http://www.theweek.co.uk/columnist/michael-bywater

But due to the way the views plugin was written, it was not possible to use custom templates for EVA views. If you tried to, you would end up with a lot of 'underfined variable' notices for all the variables in your custom template.

E.g.
Notice: Undefined variable: title in include() (line 31 of /../sites/all/themes/../templates/eva-display-entity-view--gallery--entity-view-1.tpl.php).

There is a hard dependency on eva.theme.inc even when overriding eva-display-entity-view.tpl.php to a custom tpl. Due to this, template_preprocess_views_view() is not run.

The patch in http://drupal.org/node/1205008#comment-5130160 fixes the above limitation by removing the dependency from hook_views_plugins().

Sunday, November 06, 2011

Its all a dream they say

In my dream I conjure up characters, including myself. I am very real in the duration of my dream. I do things, I see other people. I feel emotions. Sometimes I also see myself from above doing different things, often connected to me past interactions with people. And then I wake up. All the characters and myself disappear away and I am back to my present self. It was all a dream, they say.

Where did all those characters go? The events in the dream seemed real, even though the colours were not as vibrant and vivid as the non-dream state. We have always pondered about what happens to the soul after death. Life is energy, so it has to transform from one form to another?

I believe the answer to the question of where one 'goes' after death is also similar to what happens to the characters of a dream on waking up.

Hinduism suggests the whole world is Maya (an illusion). Do illusions change form? What if we do not involve the perceptor?

Wednesday, November 02, 2011

Varnish simple purge

So whats a Drupal 7 developer to do if he needs to launch a highly dynamic anonymous site and needs to plan caching strategies? Use Varnish cache of course.

But if your site is as dynamic as mine, Varnish will display stale entries unless its TTL is set to something like 1 hour. For a dynamic news site then, surely you need automatic rules to purge pages from Varnish on new or updated content? For e.g. the homepage is changing constantly! The index pages will also change often.

There are currently two modules in the pipeline that should offer the ability to purge pages (cache ids) from Varnish, instead of the whole Varnish cache (oh God, no!):

  1. Cache Expiration, and
  2. Cache actions

While waiting for these more suited modules to have a stable, usable release on Drupal 7 production sites, I had to create a very simple module -Varnish Simple Purge - that allows site admins/editors to purge individual urls from Varnish.

We don't want to give them too many options, cos by experience we know they will use all :) Finally Varnish will more often be empty - not a good state to be in.

Hence with a Varnish TTL of 1hour and a simple purge mechanism to purge pages immediately if needed, my news site is working way faster than its non Drupal predecessor. Oh did I forget to mention that this site is a port from a bespoke CMS to Drupal?

Other cache clearing mechanisms we use are time based views cache (in-built in views). What we would really love is this port of Views Content cache.