7.x

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.

Error 500 on Drupal 7 upgrade

While upgrading this blog to Drupal 7 following the UPGRADE.txt, I encountered an error 500 on the update.php?op=info page.

Assuming it was an error caused by the .htaccess file I renamed it: no changes. Eventually I dived into the update.php code only to find on the line 340:

ini_set('display_errors', FALSE);

This just hides everything from output on error and provide an error 500 page. So unless you log your php errors in a file, you may not see what is going on. I don't know why Dries chose to do this (he seems to act like Steve Jobs these days wink ) but this is not funny and cost me an hour to figure it out...

Just comment this line or put TRUE instead of FALSE and there you go, you know exactly what is the issue. For me it was the token module that was defining duplicate functions with the same name, this is easily resolve with a "drush sqlc"and:

UPDATE system SET status = 0 WHERE name = 'token' AND type = 'module';

This can also be done with PhpMyAdmin.

Good luck upgrading from Drupal 6 to Drupal 7!

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!

Drupal Gardens

It has been months since I received my invitation to Drupal Gardens' beta. This service provided by Acquia, offers a Drupal 7 hosting.

 

Drupal as a service

Foremost, this service is a complete hosting plaform, with the ability to manage multiple sites, backuo, restore and export any of your sites.  Each site will be given an address like mysite.drupalgardens.com, as Wordpress already does to offer free blogging service.
This way we can call it "Drupal as a service".

Theme builder

Acqui focused its  development of its preinstalled distribution to be the most intuitive. Users can change their site's design in seconds, they can load and edit theme with live demonstration then save or cancel modifications. Almost all modifications will showed directly in the browser as this is handeld by AJaX.

A litte administration area manage this feature:

7 default themes are given, and foreach the ability to change color palette (links, titles, lists, ...), logo and favicon, structure (number and width of columns), font, borders and margins, background. And if it is not enough customizable , you can even put you own CSS code!

When all fit your needs, save and/or apply your new personal theme.

Administration

This one is managed by a menu on top looking like the admin module. Preferences are displayed and modified by AJaX popups giving and fluidity and ergonomy to the admin part, thanks to Drupal 7.

Conclusion

Acquia provides a solution viable to the manual installation of Drupal, defivitely useful for little blogs or quick sites (for an event for example). At the end of the beta we should see options to stay free, with ads and some limitations, non-free options will be likely to appear offering more disk space and ressources.
 

Click here to sign up.

Subscribe to RSS - 7.x