Drupal 8: Making the Switch as a Developer

October 11, 2016

Blog | Development | Drupal 8: Making the Switch as a Developer

If you have been around Drupal for a while, you know that the release of Drupal 8 has forever changed the Drupal landscape. Gone (well, mostly gone) are the days of writing procedural code through hooks, hook alters, etc. We in the Drupal community are welcoming a new (to Drupal), object oriented approach. For some developers, that means entering a scary, unknown world, for others, it means getting back to what they’re comfortable with. Although it can seem overwhelming at first, there are a lot of tools and documentation that can help get you started.

What is changing under the hood in Drupal 8?

Before getting too deep into how we can ease the transition, let’s understand what exactly has changed. Drupal 7 was simply a framework written in PHP. Developers looking to use Drupal were forced to learn Drupal’s API and conform to its “Drupalisms” for better or worse.

Symfony

Drupal 8, on the other hand, is written on top of the popular PHP framework Symfony 2. Why is this a good thing you may ask? Well, there are many answers to that question, but let’s focus on a big one. Symfony encourages Drupal developers to conform to the common coding patterns and principles of many existing PHP frameworks. By using such patterns and principles, it opens the door for 3rd party solutions to integrate almost seamlessly into Drupal. In addition, the Symfony framework has already solved a lot of common problems Drupal developers run into, thus allowing developers to focus on their specific solution. Check out the examples below:

Drupal 7: We used hook_menu() in our .module file as a way to define a path to a page callback:


$items['mypage'] = array(
  'title' => 'My Page',
  'page callback' => 'mypage_display',
  'access arguments' => array('_view_mypage'),
  'type' => MENU_LOCAL_TASK,
  'file' => 'mypage.pages.inc',
);

Drupal 8: We rely Drupal 8’s routing system which has been closely modeled after symfony’s to replace hook_menu().  We now simply create a mymodule.routing.yml file to define the route to our page:


mymodule.mypage:
  path: '/mypage'
  defaults:
    _controller: '\Drupal\mymodule\Controller\MypageController::content'
    _title: 'My Page'
  requirements:
    _permission: 'view_mypage’'

Twig

If Symfony hasn’t gotten you excited, maybe Twig will. Twig, a modern templating engine for PHP has come to save front-end developers / themers. Removing PHPTemplate and replacing it with Twig allows us to finally ditch the idea of PHP inside our template files forever. Twig opens up a whole new world of fast, secure, and flexible templating. Here’s an example of how twig works:

PHPTemplate (Drupal 7): example.tpl.php


<?php print t('My Page'); ?>

<?php if ($mycontent): ?>
<div class="myclass">
  <?php print $mycontent; ?>
</div>
<?php endif; ?>

Twig (Drupal 8): example.html.twig


{{ ‘My Page’ |t }}

{% if mycontent %}
<div class="myclass">
  {{ mycontent }}
</div>
{% endif %}

Tools to help you get started

Okay, so Drupal 8 may still seem a little scary, I’ll admit it. Yes, everything has changed, everythinggggg. BUT there are a lot of tools out there that can get you up and running and ease the fear that this change inspires.

Pantheon

If you have never heard of Pantheon, you should check it out. In addition to being a great hosting solution for your end client, it’s also a great place to set up a free sandbox account. Within minutes you can have a Drupal 8 (or 7), install up and running. You have complete access to your sites’ git repository, including the ability for multiple branches and environments.

Simplytestme

If you’re not looking to actually dig into code, and just want to spin up Drupal 8 or even try out your favorite Drupal module, then check out simplytestme. Within minutes you can have an entire Drupal install including your favorite module, up and running. One thing to keep in mind, unlike Pantheon, your simplytestme site is only available for 24 hours.

PHPStorm

Okay, that’s all great, but you want to actually start writing some code. When making the transition, I found it extremely helpful to have a good IDE. Personally, I love PHPStorm. In addition to your general code editing, formatting, etc, it provides some great Drupal, Symfony, and Twig specific integrations.  For example, PHPStorm will auto detect that you have a Drupal project open, and format any code you write to follow that version of Drupal’s coding guidelines.  Little details such as this, help not only you, but your team as a whole write cleaner, more consistent code without having to memorize every detail of the Drupal’s coding standards.

Drupal Console

Like Drupal 7, Drupal 8 also has the ability to integrate with Drush, but the bigger win here is Drupal Console. Drupal Console has enough features to be a blog post on its own, but the thing I’ve found to be most helpful getting started on Drupal 8 is the generate command. Through generate, you can easily generate most of the code needed for a Drupal 8 module. How sweet is that?! You don’t have to worry about the structure of things like info files, forms, routes, services, etc. Console will handle that for you. Let’s take a first-hand look at how simple module creation can be in the video below:

Feel the fear and do it anyway!

We all know change can be a scary thing, but it really doesn’t have to be.  Drupal 8 is a good thing for not only Drupal, but for everyone who develops in it.  Taking the dive into Drupal 8 will absolutely have its unique pain points, but using some of the tools I outlined will help alleviate some of the initial struggles.  Soon you’ll be finding yourself developing faster, cleaner, and more secure code than you could’ve ever imagined.  Give Drupal 8 a try, I promise it won’t disappoint.

Drew Nackers

Technical Lead
Tags
  • Development
  • Drupal
  • Drupal 8
  • Drupal Upgrade
  • Innovation
  • Web Development

Recent Work

Check out what else we've been working on