drilix.com

Freedom, Community & Sustainability

Drupal Development Cheat Sheet

November 10, 2014 -- William
Last modified on August 2016
Duration: +- 15 minutes

This is a small recap of some useful command line tools that you can use while developing drupal websites. In this cheat sheet we have put together commands for:

We have also included recipes for:

Drush

Note: all commands below should be preceded with "drush" and eventually "sudo".

st Basic information about the drupal site (same as "status")
cc (cache-clear) Clear cache (add 'all' to clear all caches)
pml (pm-list) list downloaded modules and themes. You can use grep to filter the results like: drush pml | grep Enabled to know about enabled modules or drush pml | egrep '(alpha|beta)' to know about unstable modules.
dl <project name> downloads the given project from Drupal.org to local site. To select which version you want to install add the flag --select.
en <module> enables a previously downloaded module (same as pm-enable)
up updates drupal (minor or major) version plus enabled modules. Specify a module name to update just one module.
updb (or updatedb) runs update.php script
dis <module> disables the given module (same as pm-disable)
pm-uninstall <module> uninstalls the module
[command] -l [multisite] this executes a drush command only in one multisite. [command] can be any of the command in this table. [multisite] should be the name of the folder of your multisite inside the sites folder. (the flag is a lowercase "L")
@sites [command] this executes the command on all multisites
cron runs all registered cron hooks
ws (or watchdog-show) displays logs from watchdog (also watchdog-list for levels and watchdog-delete to clear)
rsync synchronizes drupal tree between servers
sql-dump > file.sql dumps content of the database in an sql file or piped to another command
sql-rsync synchronizes databases between servers (like mysqldump | mysql)
user-login Display a one time login link for the given user account (defaults to uid 1). Life saver if you forgot your pw.
vset maintenance_mode 1 -y put a site in maintenance mode from the command line
eval "[php code]" executes php code. This is useful in a variety of situations since you can execute whatever you want. Try 'node_access_rebuild();' to rebuild permissions or "variable_get('name', 'value');" to get the value of a variable.
fn-hook [hook_name] displays all modules that implements a given hook. If you have devel module installed you can also list many of the hooks on the page at devel/menu/item?q=path/to/page for menu hooks.

Some drush commands may require a lot of memory, increase /etc/php5/cli/php.ini 'memory_limit' in case of errors

Features with drush

fl shows features list with a handful of information
fc shows features components
fd <feature_name> shows differences between code and database overrides
fe <feature_name> component1 [component2...] exports feature components to code (module)
fu <feature_name> or fra (for all) update features <name> (recreate/gelerate) or recreate all, database => code
fr <feature_name> or fra (for all) reverts feature <name> or revert all. code => database

Use drush to create a new standard drupal site on local machine

Note : Go to your web server root first and replace values below that are between [ ].

1. Download drupal and go to it's folder  :


drush dl drupal --drupal-project-rename=[mysite] && cd [mysite]

2. Create the site with one command (needs mysql root credentials otherwise create the db first) :


sudo drush si standard --db-url=mysql://[username]:[password]@localhost/[dbname] --db-su=root --db-su-pw=[rootpassword] --account-mail="your@mail.com" --account-name="admin" --account-pass="password" --site-name="Your site name"

3. Change folder owner (will be the owner of who did the manipulations)


sudo chown -R www-data:www-data [dirname]

If you are installing drupal on a development environment you might want to set different permissions in order to allow both you and apache to edit files. Set the owner as your user and the group as apache: chown -R [your-user-name]:www-data [dirname].

GIT

git config --list Shows current configurations
git config --global user.name "Your Name Here" Configure git to identify you when you do commits
git config --global user.email "your_email@example.com" Configure git to show your e-mail address for others

git init (name) (--bare)

Creates a .git folder to initialize version control, you can optionally set a name to place the project inside a folder or set the bare option to start a bare repository
git clone /path/to/repository (or user@host:/path/to/repository) Creates a repository on local machine (or remote). Add --branch <branch> to clone from a specific branch

git clone /path/to/repo /destination

Creates a new folder of the project from repo to destination

git add <filename> (or . Or * or –all) Adds a file or all files to the index
git commit -m “commit message” Commits the changes that have been added (or git ci ...)
git push origin <branch> Pushes the <branch> to the remote repository
git remote add origin <server> Creates connection “origin” to remote <server>
git remote rm origin Removes the remote connection to “origin”
git remote -v Displays list of remote hosts
git checkout -b featureA Creates a branch called featureA
git checkout master Returns to master branch
git diff <source branch> <target branch> Preview differences between branches before merging
git merge <branch> Merges <branch> to your active branch (when there are conflicts you must merge them manually then git add <filename>)
git branch -d featureA Deletes featureA branch
git pull Updates local repository to newest commit
git log Sees the ids and description of every commit
git tag 1.0 <commit id 10 char> Adds a version number to commit with given id
git reset --hard HEAD Replaces local working tree with content of HEAD
git fetch origin + git reset –hard origin/master Drop all local changes and commits and point your local master branch to server

See also: git - the simple guide at http://rogerdudler.github.io/git-guide/ then: a visual git reference at http://marklodato.github.io/visual-git-guide/index-en.html

Drupal version control routine on Linux

-Development Environment => Repository

sudo drush fl -> get the features list and status
sudo drush fu <feature_name> -> updates the code, generate feature
sudo git status -> see what changed in the code
sudo git add path/to/folder ->adds changes on this folder to index
sudo git commit -m "message" -> commits the added files
sudo git push origin main -> sends commit to main repository

-Repository => Development Environment

sudo fua -y -> put all changes to code (keep them)
sudo git pull origin main -> get code from main repo and merge
sudo drush updb -> updates the database with new code
sudo drush fl -> check all correctly setup

Patches

Apply a patch

Apply patch using GIT
wget http://url.tld/nameof.patch get the patch from, likely, drupal.org
git apply nameof.patch applies the patch (you should be on the module folder)
or Use the patch command
patch -p1 < path/file.patch apply a patch (go to the project forlder before aplying)
patch -p1 -R < path/file.patch revert a patch (if you don't like what the patch did)

Create a patch

git config --global -e check if username and email are correct to name the patch
git checkout -b [issue number]-[short-description] switch to a new branch and then make the changes
git commit -a -m "Issue #[number] by [username]: Fixed all instances of foo bug." commit the changes
git diff origin/[branchname] > [project_name]-[short-description]-[issue-number]-[comment-number].patch create the patch (see documentation for more details)

Advanced patch contributor guide https://www.drupal.org/node/1054616 , create patches documentation https://www.drupal.org/node/707484, video tutorial https://drupalize.me/videos/applying-and-creating-patches-git

PHP error reporting (to use on WSOD)

Add these lines of code on your sites/default/settings.php file:


<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

On multisite setups use


<?php
if ($_SERVER['HTTP_HOST']==='some.domain.name.here') {
  error_reporting(E_ALL);
  ini_set('display_errors', TRUE);
  ini_set('display_startup_errors', TRUE);
}
?>

This can optionally also be set on php.ini (on dev environment only !). Edit those two lines on php.ini on your development server :

error_reporting = E_ALL | E_STRICT <- you might leave this line at default if different

display_errors = On <- this is probably the most important line to edit

Command line tips

When managing files in a server you probably use the command sudo because in most cases you are not logged in as root nor as apache user. When you use sudo you become root and if you move a file it will then be assigned to root. Then you can use the chown -R command to change the ownership to apache. To prevent this you can use the sudo -u option:


sudo -u www-data cp default.settings.php settings.php

With this command the file will already be assigned to the user www-data.

Read also:
Debugging PHP errors
Apache configuration made simple - survival kit

Add new comment