Drupal 8 Tips and Tricks

Drupal Version

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.


A quick overview of writing Drupal 8 modules (and migrating Drupal 7 modules to Drupal 8) can be found on the Ivan Allen College's Web Developer website.


The current development version of the CAS module does work, but there is no documentation yet.  The most important piece of the puzzle is that after you install and enable the module, the default login path is now '/caslogin', not '/cas' as in the Drupal 6 and 7 versions.  (You'd think a change like that would be documented somewhere ...)  Also, even though the configuration page has options for Gateway and Forced Login settings, these are not fully functional yet, so you'll most likely have to go to /caslogin to log into your site every time, though you can make an alias to /caslogin (e.g. '/gtlogin')


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

If you're wanting to add features to CKEditor in Drupal 8, don't do it the hard way.  Check out our compilation of CKEditor tips and tricks first!


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.


Additional Resources