Drupal 8 Gotchas

Yaml spaces

Drupal configuration file coding standards documentation states:

Use two spaces to indent in config files. In YAML, the white space has semantic meaning to represent nested structures.

Now and again I get the error yaml_parse(): parsing error encountered during parsing because sometimes I don't properly indent yaml files.


To change logo add something like the following to the theme.settings.yml in config/install folder:

logo:
  path: 'themes/custom/theme_name/images/logo.png'
  use_default: false

This doesn't actually do anything, you need to reinstall the theme. I found it's better to go to the theme settings page in the UI, in the Override Global Settings section, uncheck Use the logo supplied by the theme and enter the path in under Path to custom logo.


Grunt SASS compiler slow

I'm using Grunt to detected changes to SASS files to automatically convert to CSS. The problem is I do find it quite slow within the Drupal VM environment. If I go to my theme directory from the command line outside the Drupal VM environment, it works lightning fast - though I need to make sure node.js is installed on my computer (I'm using Windows). When running grunt for the first time, it did give me the error:

>> Error: Missing binding *mysite\web\themes\custom\mytheme\node_modules\node-sass\vendor\win32-x64-48\binding.node
>> Node Sass could not find a binding for your current environment: Windows 64-bit with Node.js 6.x
>>
>> Found bindings for the following environments:
>>   - Linux 64-bit with Node 0.12.x
>>
>> This usually happens because your environment has changed since running `npm install`.
>> Run `npm rebuild node-sass` to build the binding for your current environment.

Following the instructions and running npm rebuild node-sass did fix the problem.


Class not found when creating a service

I get the following message when creating a class as a service:

Fatal error: Class 'Drupal\module\Service\myClass' not found in /var/www/mysite/web/core/lib/Drupal/Component/DependencyInjection/Container.php on line 264

The solution was to put the following in my settings.local.php file:

$settings['class_loader_auto_detect'] = FALSE;

I think it's something to do with Drupal 8 using APC Classloader, and that clearing the cache doesn't fully include the changes I've made because of it.


hook_page_attachments() does not work

Apparently hook_page_attachments() does not work in your THEME.theme file, it needs to be put in your custom module. For specific pages, you can attach assets via twig template, or use a preprocess hook, or use hook_page_attachments_alter().

Access denied for super user after transferring site

I created a site locally and made a copy on my production server. When I go to login as the super user I get 'access denied'. Apparently it's some bug in Chrome and the fix was to do the following:

  1. Go to chrome://settings/
  2. Enter your domain in the search cookies box
  3. Delete the cookies
  4. Go and log in again. It should work now.

Drupal Console escapeTrailingBackslash() error

When I try to use Drupal Console to generate something I get the error message:

PHP Fatal error: Call to undefined method Symfony\Component\Console\Formatter\OutputFormatter::escapeTrailingBackslash() in /var/www/mysite/public_html/vendor/symfony/console/Helper/SymfonyQuestionHelper.php on line 57

Not really sure what's going on but it instead of doing drupal generate:module what seems to work is at the project root using the command vendor/bin/drupal generate:module instead


Tabs not showing [View, Edit]

If the node view/edit tabs do not show, check it's placed as a block in the Block Layout page. In Drupal 8 tabs is a block.


xDebug waiting for incoming connection in PhpStorm

So I enabled xdebug in my Drupal VM vagrant box, followed the instructions to configure debugging in PhpStorm but I kept seeing Waiting for incoming connection with ide key 'PHPSTORM' - nothing was happening. Long story short, I had to add the following lines to Drupal VM config.yml file:

php_xdebug_remote_connect_back: 0
php_xdebug_remote_host: 10.0.2.2

 and voila, it worked.


AJAX form submit error when using commands

When I submit an AJAX button when custom commands I was getting the error:

Uncaught Drupal.AjaxError {message: "↵An AJAX HTTP error occurred.↵HTTP Result Code: 20…rmCallback(  ).../FormAjaxResponseBuilder.php:69↵", name: "AjaxError"}

This was because I was not including the namespaces for the AJAX response and commands. For example:

use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\InvokeCommand;
use Drupal\Core\Ajax\ChangedCommand;
use Drupal\Core\Ajax\CssCommand;
use Drupal\Core\Ajax\HtmlCommand;

User profile field missing due to minimal install

If you installed Drupal 8 using the minimal profile, the user profile picture field is missing. The way to get it is to search for all files that include the string 'user_picture' in the config folder of the standard profile (at core/profiles/standard/config/install). Copy those files into your sites config/sync folder. Then go to the syncronize page (at /admin/config/development/configuration) and click the 'import all' button.