Drupal 8 Guides, Tips and Tricks

Drupal Version

Drupal 8 Pages of Note

Where applicable, Drupal 8 knowledge is being added throughout the rest of the Developers Handbook.  The following highlights some useful pages found elsewhere in the handbook.


Drupal 8 Guides


Drupal 8 Tips and Tricks

This section is for tips and tricks specific to Drupal 8, which is quite different from it's predecessors, Drupal 7 and 6.  Topics posted here will eventually be migrated into the main sections of this handbook after some mainstream adoption of Drupal 8 on campus has occurred.

Drupal 7 began forcing mySQL to act like a traditional SQL server so that all SQL code used in Drupal will be highly compatible with other database systems like PostgreSQL or SQLite.  This means that mySQL statements that syntactically work in other contexts may produce errors under Drupal 7 or 8.  Drupal 8 adds the 'ONLY_FULL_GROUP_BY' restriction, which can potentially cause custom mySQL code that worked in Drupal 7 to fail with errors in Drupal 8.

Here's the exact settings being used in Drupal 8: 

  • SET sql_mode = 'ANSI, STRICT_TRANS_TABLES, STRICT_ALL_TABLES, NO_ZERO_IN_DATE, NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO, NO_AUTO_CREATE_USER, ONLY_FULL_GROUP_BY'

The mySQL Reference Guide has full details on these SQL modes and their effects.  (See especially the section on STRICT mode and ONLY_FULL_GROUP_BY mode.)


Here's how to whitelist HTML tags in your module output that are normally stripped out by the XSS filter:

    $tagList = array('input');
  
    return array(
      '#markup' => $buffer,
      '#allowed_tags' => array_merge(\Drupal\Component\Utility\Xss::getAdminTagList(), $tagList),
    );

And here's how to allow data URLs in your module output for displaying images with data that you've already pulled in from a data source (code updated 2017-12-11 to a better format that avoids whitescreening in certain use cases):

  $allowedProtocols = \Drupal\Component\Utility\URLHelper::getAllowedProtocols();
  $allowedProtocols[] = 'data';
  \Drupal\Component\Utility\URLHelper::setAllowedProtocols($allowedProtocols);

Here's a quick reference guide to working with the new configuration management system.  You'll need this if you want to build a module that adds its own configuration to Drupal during its installation process:

Simple Configuration API | Drupal.org


Add additional tips and tricks here.