Drupal Tricks

Performance

If you experience memory issues on you 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');

In your my.cnf

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

Or go the old fashioned SQL way.

Enable multiple modules

drush @site.env en devel devel_debug_log stage_file_proxy -y

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 @maf.loc:%files/ @maf.dev:%files

Change drush version on your local

composer global require drush/drush:8.*

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

Using entities on the standard Drupal $user object is a great way to gain access to fields:

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

Listing an entity’s properties:

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

Loading an entityform type:

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

See more:
https://www.drupal.org/documentation/entity-metadata-wrappers
http://www.pixelite.co.nz/article/how-use-entity-metadata-wrappers-drupal-7/

Scripts

Generate password hash (Drupal 7)

cd <drupal-root>
./scripts/password-hash.sh "password"

Debugging

ddebug_backtrace();

Patching

patch -p1 < <filename.patch>

Kickstarter Coming September

I'm launching a board game. Put your email here to be notified on the launch!