Updated on 10 October 2017

[ This section needs to be completed, see the project roadmap for this site. ]

Why your code turns to pasta and what to do about it

A session from DrupalCamp Antwerp 2017 by Chris Jansen (legolasbo) that every developer should watch.

Basically, follow the KISS and SOLID principles.

See also Development best practices learned from the Drupal Dev Days Seville 2017

Use contrib and custom directories

Place contributed modules and themes in a contrib directory: /modules/contrib and /themes/contrib.
Do the same for custom ones: /modules/custom and /themes/custom.

Changing this when your site is already in production will be harder than adopting the right standard when starting your project.

This is already handled by default by the Composer template for Drupal project.
If you use your own composer.json file, just add this to the extra section (where web is your docroot directory).

"installer-paths": {
  "web/modules/contrib/{$name}": ["type:drupal-module"],
  "web/profiles/contrib/{$name}": ["type:drupal-profile"],
  "web/themes/contrib/{$name}": ["type:drupal-theme"],
}

Check your logs

During development, it is a good habit to frequently have a look at the recent logs (/admin/.
You will not want to have errors like undefined variables to be logged and mixed with other system messages (nodes created, deleted, ...).

Recent logs

Use a debugger

Use a tool like Xdebug that you can configure on your IDE.

PHP debugging session on PHPStorm

Use Drupal coding standards

Favor dependency injection when using services in an object

Dependency injection is the preferred method for accessing and using services in Drupal 8 and should be used whenever possible. Rather than calling out to the global services container, services are instead passed as arguments to a constructor or injected via setter methods. Many of the controller and plugin classes provided by modules in core make use of this pattern and serve as a good resource for seeing it in action.

Services and dependency injection in Drupal 8

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Only images hosted on this site may be used in <img> tags.
CAPTCHA This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.