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.

How to force downloading a file with Drupal 7

New quick post to share a feature.

To force to download a file, as usual with Drupal, there's a module for that. In this case there are even two: Download file and File force. But you may be reticent to install a module for such a basic feature! So here is the solution in a few lines of code:

Here is a snippet based on File API. It is more secure than the second one but it will not work on files not handled by Drupal (whatever file download method you're using) : thus I recommend it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
/**
 * Implementation of hook_menu()
 */
function mymodule_menu() {
  $items['download/%file'] = array(
    'page callback' => 'mymodule_download_file',
    'access arguments' => array('administer site configuration'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}
 
/**
 * Page callback for forcing a file to download
 */
function mymodule_download_file($file) {
  if($file) {
    file_transfer($file->uri, array('Content-disposition' => 'attachment; filename='.$file->filename));
  }
  else {
    return drupal_access_denied();
  }
}

You can download the file with a link like http://mysite.com/download/123 where 123 is the file id (fid) of the file being downloaded.

This second snippet may present a security issue! It is recommended to use it as a last resort when files are not handled by Drupal. To increase security, this feature will be restricted to administrators, and filtered on file extensions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
/**
 * Implementation of hook_menu()
 */
function mymodule_menu() {
  $items['download'] = array(
    'page callback' => 'mymodule_download_file',
    'access arguments' => array('administer site configuration'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}
 
/**
 * Page callback for forcing a file to download
 */
function mymodule_download_file() {
  if (!isset($_GET['file']) || !file_exists($_GET['file'])) {
    return drupal_not_found();
  }
  $filepath = $_GET['file'];
  $realpath = realpath($path);
  $filename = basename($filepath);
  $extension = pathinfo($filepath, PATHINFO_EXTENSION);
  // Check extension and restrict to files in DRUPAL_ROOT
  if(in_array($extension, array('jpg', 'png', 'gif', 'mp4')) && substr($path, 0, strlen(DRUPAL_ROOT)) === DRUPAL_ROOT) {
    drupal_add_http_header('Content-disposition', 'attachment; filename=' . $filename);
    readfile($filepath);
  }
  else {
    return drupal_access_denied();
  }
}

Then you can create your file link as in http://mysite.com/download?file=sites/default/files/myimage.png

It's only working for the public file system handling but it is the most famous one.

Install Drupal book on Coda

If like me, you are on a Mac and develop under Coda (software that I recommend by the way), here is a little tip that will surely help you. As you may have noticed, Coda has an integrated documentation of PHP language, but this one is offline. OK, it's cool, but we, Drupal developers would prefer to have API under the hook... wink

Wish exauced ! laugh

How to ? How to :

  • Open Coda if not already > Click on Books in display modes toobar
  • Click on the bottom left "+"
  • The fill in the form 

Book title: Drupal 6 API
Book URL: http://drupalcontrib.org/
Use for Mode: PHP-HTML
Search URL: http://drupalcontrib.org/api/search/6/*

and for the Cover image I found this one pretty:

Now where it gets interesting, it's that you can use a hotkey to open directly your book on a function: Command key icon + double click. Furthermore, you can search on external sites which provide documentation for third-party modules, like drupalcontrib.

This tip is possible for others languages and frameworks, like jQuery :

URL: http://docs.jquery.com/Main_Page
Search URL: http://docs.jquery.com/Special:Search?ns0=1&search=*&go=

Add yours in comments ! 

Source

Bartik : a new face for drupal

Drupal 7 alpha 6 is now out, and the issue queue has never been so thin (under 40 issues cheeky), and it shows some surprises, as the new default theme : Bartik

It will replace the theme used after installation... So bad for this old Garland after four years of good services! blush His successor, named after one of the first programmers, worked on the ENIAC, Jean Bartik. Its goal is to bring a fresh and new image to the next release of Drupal. Its design is modern, clean and flexible and is well appreciated with the new administration theme : Seven.

 

A new feature that Garland did not have is that it supports full right-to-left wiriting (RTL), it means that blocks will be also moved when RTL is active. Other themes were also in competition to be part of the new Drupal core : Corolla (demo) and Busy (demo), but Bartik's team must have made a better work smiley.

This new theme is part of refresh process of the community websites, a new look is on the way: a theme named Blue Cheeze. New versions can now be seen for some of them like localize.drupal.org and association.drupal.org, others like api.drupal.org and the main website are in test phase. Note that the main website will show a new dashboard, customizable, replacing links to followed issues queues and other blocks in the sidebar.

What do you think of this? In my opinion, I hope that all these little news will bring a new image of Drupal and grow the community bigger!

Hidden Devel functions

What more frustrating than not knowing what is going on when a drupal page is loading? angry There are often redirection pages containing errors we can't see.
When developing a module or building a theme, it would be useful to know what content there is in some variables.

So what does drupal offer for this? A drupal_set_message('<pre>'.print_r($variable, true).'</pre>');? Isn't this a bit too long? indecision

Well, the Devel module, specifically designed to make the development of module and drupal administration easier, bring up some awesome functions:

1
dsm($input, $name = NULL);

alias of drupal_set_message, put the input variable in a dropdown style with the Krumo library
$input:
your variable or your text
$name: (optional) prefix of the message (e.g. : dsm('my content', 'my variable'); will display "my variable => my content")

1
dpm($input, $name = NULL);

alias of dsm()

1
dvm($input, $name = NULL);

same as dsm() but using var_dump() (e.g. : dvm('my content'); will display "string(10) "my content")

1
dpr($input, $return = FALSE, $name = NULL);

display the print_r() of your argument at the beginning of the page or store it  in a variable (useful if the message section is not displayed in any region of the theme)
$return: if TRUE, print_r() of the input will be returned

1
kpr($input, $return = FALSE, $name = NULL);

same as dpr() but will display with Krumo library 
 

1
dvr($input, $return = FALSE, $name = NULL);

same as dpr() but using var_dump()

And others not concerning variable display :

1
dargs($always = TRUE);

display arguments passed to the current function
$always: if FALSE, will only display once

1
db_queryd($query);

display the complete SQL query and the error in the browser (useful to know what arguments are passed to the query)

1
dfb($arg, ...);

display arguments in the Firebug console if FirePHP is installed

All these useful functions are hardly documented, so if you find others: share it ! wink

Blog install

Welcome on this new blog !

I'll talk about all, but mostly about development and my virtual life because yes, there's a life after life ! smiley

Maybe you know it, but I'm specialized in Drupal. For a year and a half now I use it. So I'll use this blog to share my experience, my tips and my snippets.

Let's begin by the beginning : this blog. Yeah, it is Drupal-powered cool ! Drupal 6 to be precise, and here is the modules I've installed to run it :

Or more simply with drush :

drush dl ajax admin_menu addthis ckeditor comment_notify pathauto token geshifilter captcha google_analytics jgrowl fivestar votingapi

drush en ajax admin_menu addthis ckeditor comment_notify pathauto token geshifilter captcha google_analytics jgrowl fivestar votingapi

I also enabled some core modules :

  • Blog
  • Comment
  • Contact
  • Content Translation
  • Database logging
  • Poll
  • Taxonomy
  • Search
  • Upload

I choosed BlogBuzz by Astin for the theming part, it offers several regions and 3 color sets.

Tags: 
Subscribe to RSS - Drupal