Change any user’s password

Drush method:

drush @site.env upwd --password="<password>" "<user>"

SQL method:

UPDATE users SET pass = "$S$DZDqpoM1nUF8BAd/Bo6cHehxXA/uD.PLJql/PhIWmyhMvuJLaTEc" WHERE uid = 1;

This sets the user’s password to password… well you aren’t really concerned about security if you’re doing this now are you?


Get image url from an image field in views (Drupal 8)

Drupal unfortunately doesn’t provide the path to an image as a separate value. Have no fear, we can get the value out of the image using Twig. Inside your field click “Rewrite results” and paste:

  {% if field_image|trim is not empty%}
    {{ field_image|split('"')[1] }}
  {% endif %}


Stop Drupal caching everything

When developing on your local! Place in your settings.php:

// Cancel caching..
$conf['cache'] = FALSE;
$conf['block_cache'] = FALSE;
$conf['page_compression'] = FALSE;
$conf['cache_lifetime'] = 0;
$conf['page_cache_maximum_age'] = 0;
$conf['page_cache_invoke_hooks'] = TRUE;
$conf['preprocess_css'] = FALSE;
$conf['preprocess_js'] = FALSE;

Increase your local machine’s memory

If you experience memory issues on your local place in your settings.php:

  // Increase memory
  ini_set('memory_limit',             '500M');
  ini_set('max_execution_time',       '150');
  ini_set('max_input_vars',           '5000');
  ini_set('max_input_nesting_level',  '200');
  ini_set('xdebug.max_nesting_level', '500');

Optionally in your my.cnf: (MySQL)

innodb_buffer_pool_size = 1GB
query_cache_limit       = 1M
query_cache_size        = 16M


Sync a database

Source on the left, destination on the right.

drush sql-dump | drush @site.loc sql-cli

Rsync files using drush aliases

Source on the left, destination on the right.

drush -y -r . rsync --size-only -v --stats --progress --exclude=.DS_Store @site.loc:%files/


Change Drush version on your local

composer global require drush/drush:8.*

(Drush 8 works with Drupal 7)

Run registry rebuild even if the server doesn’t support it

drush @site.env php-eval "registry_rebuild();" --strict=0


There are three styles of placeholders:

!variable, to insert text with no filter:

$message = t("If you don't want to receive such e-mails, you can change your settings a!url.", array('!url' => l(t('My account'), "user/$account->uid")));

@variable. Run text through check_plain to escape HTML characters:

$title = t("@name's blog", array('@name' => $account->name));

%variable. Run text through check_plain and theme_placeholder() to emphasize text:

$message = t('%name-from sent %name-to an e-mail.', array('%name-from' => $user->name, '%name-to' => $account->name));


Active class for menu item <li> (Drupal 8)

Give li element of a list an .active class, not just its child a:

// Add ".active" class to list item.
function theme_preprocess_menu(&$variables, $hook) {
  if ($hook == 'menu') {
    $current_path = \Drupal::request()->getRequestUri();
    $items = $variables['items'];

    foreach ($items as $key => $item) {
      // Set active if path of menu item matches current path.
      if ($item['url']->toString() == $current_path) {
        // Add active link.

Load a field’s value (Drupal 8)

Multiple values will return as an array.


Load a user’s fields (Drupal 7)

Fields on the Drupal $user object aren’t loaded by default. Use the entity wrapper to access fields easily:

    global $user;
    $user_wrapper = entity_metadata_wrapper('user', $user);

List an entity’s properties (Drupal 7)

    $wrapper = entity_metadata_wrapper('node', $node);

Load an entityform type (Drupal 7)

Entityforms have no instances so loading them is a little different. You are working on the entityform type directly.

    $entityform_type = entity_load_single('entityform_type', $machine_name);


Devel Debug Log is much better than a one time dpm() or kint() as the debugged variables hang around on a separate admin listing, and it works well when debugging AJAX elements.


For a patch made relative to the file:

patch -p1 < <filename.patch>