Drush commands

Import translations:

drush locale:import --override=all fr modules/custom/base/translations/base.fr.po && drush cr

 Execute migrate:

drush migrate-import entity_invoice --update

 Execute cron job by name:

drush simple-cron incoming_session_archive --force

How can I change theme? For Drupal8+: 

Fix translations for Drupal 9

$connection = \Drupal::service('database');
$connection->update('locales_target')
  ->fields([
    'customized' => 0,
  ])
  ->execute();

$connection->delete('key_value')
  ->condition('collection', 'locale.translation_status')
  ->execute();
$connection->update('locale_file')
  ->fields([
    'timestamp' => 0,
    'last_checked' => 0,
  ])
  ->execute();
 

Database manipulations

Restore database:

drush sql:drop -y && pv ~/www/SITE_NAME/web/sites/default/files/backups/last.mysql | drush sql-cli --extra=-A

Restore zipped database:

drush sql:drop -y && unzip -p ~/www/_db_backups/SITE_NAME/last.mysql.zip | pv | drush sql-cli --extra=-A


Backup database:

drush sql:dump --structure-tables-list="cache,cache_*" > new.sql 

Twig debug

{{ dump() }}
{{ dump(variable_name) }}

List available variables (at top level):

{{ dump(_context|keys) }}

If you have Devel module, you can get an accordion display of the variables available to twig with:

{{ devel_dump() }}

Debugging with console.log
We can add the following to a twig template and view the logs in the browser:

Delete content from the site

To delete nodes:


$type = ['invoice'];
$nids = db_select('node', 'n')
           ->fields('n', array('nid'))
           ->condition('type', $type, 'IN')
           ->execute()->fetchCol();
if (!empty($nids)) {
  dsm($nids);
  node_delete_multiple($nids);
  drupal_set_message(t('Deleted %count nodes.', array('%count' => count($nids))));
}

To detele custom entity type:

String placeholders explanation

@variable: When the placeholder replacement value is:

       A string, the replaced value in the returned string will be sanitized using \Drupal\Component\Utility\Html::escape().
       A MarkupInterface object, the replaced value in the returned string will not be sanitized.
       A MarkupInterface object cast to a string, the replaced value in the returned string be forcibly sanitized using \Drupal\Component\Utility\Html::escape().