Drupal version is specified in brackets if the code is version specific.

Performance

Increase your local machine’s memory

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

  <?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

Drush

Change any user’s password

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

Sync a database

Source on the left, destination on the right.

drush @site.dev 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/ @site.dev:%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

Tokens

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));

Entities

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.
        $variables['items'][$key]['attributes']->addClass('active');
      }
    }
  }
}

Source

Load a field’s value (Drupal 8)

Multiple values will return as an array.

$entity->get('field_name')->getValue();

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:

  <?php
    global $user;
    $user_wrapper = entity_metadata_wrapper('user', $user);
    $user_wrapper->field_name->value()

List an entity’s properties (Drupal 7)

  <?php
    $wrapper = entity_metadata_wrapper('node', $node);
    dsm($wrapper->getPropertyInfo());

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.

  <?php
    $entityform_type = entity_load_single('entityform_type', $machine_name);

Debugging

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.

Patching

For a patch made relative to the file:

patch -p1 < <filename.patch>

Kickstarter Coming January

I'm launching a board game called Delivery Duck. You're a duck, in a truck, delivering packages. It's a strategic family game for 2 to 6 players.

Check out the board game!