Drupal

What's new in Drupal World? Week #25 DrupalDevDays special

The big event of last week is of course the Drupal Dev days in Barcelona: not so many tweets about it though, but don't worry I've managed to find some interesting links!

New version for Commerce Kickstart

The CommerceGuys rolled out a version 2 of the Drupal Commerce installation profile, the new UI will surely amaze during client demos http://commerceguys.com/blog/sneak-preview-commerce-kickstart-v2

Barcelona DrupalDev days sessions

Here are the slides of the different sessions:

WSCCI and Web Services

A sprint in Paris took place last week on Web Services in Drupal 8, here's the summary by @Crell. This sums up how you are going to code with the future entities in D8 and what protocol is discussed for providing Web Services in Drupal http://groups.drupal.org/node/237443

Twig in D8 core

Twig just got committed to Drupal 8 core http://drupalcode.org/project/drupal.git/commit/07e217036136e0bc3369c276...

New UI for string translation committed

The issue is finally pushed to 8.x, congratz! http://drupal.org/node/1452188

Drupal 8 Plugin architecture

A draft of D8 plugin architecture is on the way by EclipseGc http://drupal.org/node/1637614

A Drupal energy drink spotted

drupal energy drink
So that you can code harder, better, faster, stronger...

What's new in Drush 5?

During the DrupalCon Denver this week, there was a presentation by Moshe Weitzman, Jonathan Hedstrom, Owen Barton and Mark Sonnabaum. With it the stable version 5 of Drush should have been released (I might be a little early for that).

Drush is your friend, if you are a developer, you have to have it installed, so I will assume that you have used Drush 4 before and I will present you the new features of version 5 in this article.

Configuration location

Configurations, through drushrc.php files, can now be placed at several locations:

  • Relative to the unix user: ~/.drush/drushrc.php
  • Relative to the Drupal site: sites/{default|example.com}/drushrc.php
  • Relative to the server: /etc/drush/drushrc.php

Shell aliases

You can now provide your own drush aliases in the configuration, for example:

1
2
3
4
$options['shell-aliases'] => array(
  'pull' => '!git pull',
  'pulldb' => '!git pull && drush updatedb',
);

So you can set drush commands along with system commands like git or rm.

Options handling

Unrecognized options now output an error, no more "Why this didn't work?", syntax errors can be avoided easily.

Drush help adopted a standard for option parameters:

  • --option=<value> for mandatory parameters
  • --option=[value] for optional parameters

Windows compatibility

Finally! Yes, they did a good work... Sponsored by Microsoft itself, an installer is now available, and (almost) no need of cygwin anymore.

Usage tracking

It is now possible to log used commands, they can be anonymized and sent to MongoLab (sponsor of Drush developers) to make Drush better.
This can also be used by sysadmins to keep track of what developers do.
This option is disabled by default.

Drush make

The famous packet manager integrates Drush core, is it now possible to create .make files out-of-the-box to install modules and themes faster and faster.
That's not all of it, multiple downloads are now handled with concurrency (4 processes by default), more speed!
Moreover, if you didn't know it, there is a Drush cache that prevents downloading tarballs if they have already been downloaded earlier.

Autocompletion

Yes, ladies and gentlemen! Site aliases, commands, shell aliases, options and modules autocompletion is now available. A context handling prevent the overflow of possibilities. For more information, the better is to test it or to watch the video.
To enable it you just have to copy-paste drush.complete.sh into /etc/bash_completion.d, or, if you don't have the permissions, you can include a little snippet in your ~/.bashrc

Some options are there so you can even do without the drush keyword at the beginning of the command, for example:
@mysite dl views

Built-in WebServer

Drush 5 comes with a light HTTP server to execute Drupal sites. You may "what is this for", well, if you want to test a Drupal site without wondering about Apache VHosts, this command is very useful. In addition to seeing access log, it also shows watchdog messages.
Thus, the command drush run-server :8889/ will open a page in your browser with the current Drupal site. This requires php-cgi and PHP 5.3.

There is more, you can now do a disposable Drupal with the command quick-drupal. It's perfect if you want to impress this Joomla "friend" you have in few seconds :
cd /tmp
drush quick-drupal

It will download Drupal 7, create a SQLite database (in a single file), and run standard install profile. You can then play with the multiple options:
drush qd "My store" --core=commerce_kickstart --profile=commerce_kickstart --yes --watchdog (enjoy watching your friend having a heart attack)

When you don't need it anymore, rm -r the directory, au revoir.

At this point, I must confess I clapped alone at home...

Queue API

It seems that the new Queue API handling in Drupal 7 is a bit too hidden and only runnable within the cron, Drush solves this problem with two commands:

  • drush queue-list lists all queues registered by modules
  • drush queue-run runs these queues

Multisites

For people working with multisites, it can be frustrating to execute a command in the wrong site (say sites/default), Drush 5 provides the commands drush site-set and drush site-reset to make this environment persistent.

So lots of new features, and what features! A little tip: to enjoy fresh features from Drush, clone the repository directly. These new features come along with some bugs and that would the reason why the stable version is still not here, but anyway: Kudos to them all!

"We'll see you on the command line" — Moshe Weitzman

The presentation video is available here.

Proof of Concept: Twitter aggregator for Drupal with Drupal

Happy new year! Oops, already Fabruary...

As I have some time for my last semester in Dublin, I can test some little things I wanted to do.

Concept

I have built a little Drupal tweets aggregator, fetching only those that have links embedded (this Twitter search). It stores all these tweets, but as the nodes are not very easy to manipulate for statistics, I decided to make my own module et not use the Aggregator module from Drupal core. Four SQL tables and let's go: storing tweets, links et their authors in a hook_cron() :

  • aggreg_tweets(id, author, text, date)
  • aggreg_authors(id, name)
  • aggreg_links(id, link)
  • aggreg_links_tweets(link, tweet)

Statistics

Ok, that's good for now, but we have to do something with these tweets... So let's make some hook_menu entries for statistics pages:

  • One for time-related to see when drupalers are tweeting more, so when to watch
  • One for trendings tweets to see what is happening and what is relevant right now

For that I would have been bothered by the nodes: doing some GROUP BY and ROLLUP queries on so many JOIN , no thanks!
Some highcharts.js library to render nice charts, little tables and an ajax query to fetch which tweets are related to a link (to add some context) later and here it is: a nice Twitter aggregator

Twitter bootstrap

This fresh new theme (therefore in dev version) have some bugs due to Drupal integration and licensing issues but there are solved in few changes. It provides some CSS classes to render nice tables and some JS plugins to create modals (I also added bootbox.js for easier handling, thanks to @FGRibreau). It results in a simple & nice interface perfect for proofs of concept or small projects like this one.

Drupal, a real framework

This recipe is prepared in about 350 lines of code thanks to Drupal hooks and methods. Among them I used:

  • hook_menu() & hook_cron()
  • db_merge()
  • variable_get() and variable_set(), just essential
  • l(), truncate_utf8(), theme_table() for rendering
  • curl for HTTP requests

Now I will be able to tweet relevant links about Drupal, so don't hesitate to follow me on Twitter !

Drupal Live templates for PHPStorm

Djebbz recently draw my attention on IRC. He has put up a GitHub repository with a way to bulk export Live templates from the Drupal code.

Explanation

It's a perl script that parses the Hooks API files (mainly contained within the Examples module and in *.api.php files in contrib modules).

Usage

First, close PHPStorm as it overrides files while closing.
Basic hooks are already available in the user.xml file in the repository, on Mac OSX all you have to do is:
cd ~/Library/Preferences/WebIDE10/templates
curl -O -# https://raw.github.com/DjebbZ/Drupal-PHPStorm-Live-Templates/master/user.xml

Then start PHPStorm and in your code type h_menu<TAB> to insert a hook_menu.
Note that typing hook_menu<TAB> will insert the full hook documentation.

PHPStorm Live template example

More

The script is available in the repository if you wish to add more hooks from other contrib modules. To execute it:
cd /path/to/drupal
find . -name \*.php | xargs grep -l '^function hook_' | xargs /path/to/parse_drupal_api.pl > ~/Library/Preferences/config/templates/user.xml

Enjoy!

Order view on string length

Let's keep on with useful snippets. Here is, in 4 lines of code, how to order view results on title string length.

1
2
3
4
5
6
7
8
9
10
11
<?php
 
/**
 * Implements hook_query_TAG_alter().
 *
 * @param SelectQuery $query
 */
function glossaire_query_views_export_alter(SelectQuery $query) {
  $query->addExpression('LENGTH(node.title)', 'title_length');
  $query->orderBy('title_length', 'DESC');
}

This is used in the custom module of the french glossary. The code was built from the documentation available on drupal.org.