Drush - Short Tips

· konordo
Quick notes on Drush, while on a Drupal installation folder: To create an archive of the current Drupal site: drush archive-dump --destination=/backups/mysite.tar To restore the files and databases for all sites in the archive. drush archive-restore /backups/mysite.tar To create a mysql dump of the current Drupal database: drush sql-dump --result-file= /mydb.sql To drop all tables of the current Drupal database: drush sql-drop To import a new db file in the current Drupal database:

Drupal 7: How to check the available variables in a template (tpl) file

· konordo
There is a very handy php function that allows us to quickly check all the available variables in a tpl file (e.g. html.tpl.php). Assuming that the devel module is installed, the following line should do the trick: Alternatively, a print_r should work just fine: More info: http://php.net/manual/en/function.get-defined-vars.php

Drupal 7: Reset User 1 Password quick and easy using Drush

· konordo

There are many solutions out there to reset User 1's password in a Drupal 7 website. A really convenient way is to use Drush, to generate a one time login URL - no mails involved. The output would be directly on the console, and it is simple to just copy and paste it in a browser. 

The drush command is the following:

 

drush php-eval 'echo user_pass_reset_url(user_load(1));

 

and once logged in, the password can be easily changed in the user edit screen. 

Drupal 7: Hide a field depending on the value of a checkbox

· konordo
I recently had the requirement to hide a horizontal tab in node display depending on the value of a checkbox i.e. Show tab X. For the horizontal tabs I was using the excellent Fieldgroup module. In order to achieve the above, I created a little custom module and used the newly introduced in drupal 7 hook, hook_node_view_alter.

Drupal: Send file programmatically

· konordo
Here is a handy, general function for Drupal 6 that takes as an argument a file id and returns the file for downloading. <?php function MYMODULE_send_file($fid) { $file = db_fetch_object(db_query("SELECT * FROM {files} WHERE fid = %d", $fid)); if ($file) { $my_path = $_SERVER['DOCUMENT_ROOT'] .'/'.$file->filepath; header("Pragma: public"); header("Expires: 0"); header('Cache-Control: no-store, no-cache, must-revalidate'); header('Cache-Control: pre-check=0, post-check=0, max-age=0', false); header('Last-Modified: '.gmdate('D, d M Y H:i:s') .?>

MySQL Order by before Group by

· konordo
In a custom module, I needed to get the latest node that each user published. It is fairly easy to goup by users, but it needs a little something to actually accomplish the sorting. The following query worked for me, we need to do the sorting in a nested select: In case the following error comes up:

Error: 1248 - SQLSTATE: 42000 ER_DERIVED_MUST_HAVE_ALIAS

Create an ubercart order programmatically and update address and products.

· konordo
One of my latests tasks was to allow a link that would take a user id and a node id as an argument, and automatically create a new order that would have some fields pre populated and ready for further editing. Scope is to use the user id as the client that does the order and to automatically select the product depending on the taxonomy term of the provided node. In my case I offer a series of links through a custom view template, that uses data from each row results to built the link. Here is the code for my module, hopefully it will be useful for someone: