Home Web-development Drupal 8 Development: Beginner's Guide - Second Edition

Drupal 8 Development: Beginner's Guide - Second Edition

By Neeraj Kumar , Tassos Koutlas , Samuel Keen and 5 more
books-svg-icon Book
Subscription
$10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
BUY NOW $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
Subscription
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
  1. Free Chapter
    Setting Up a Drupal Development Environment
About this book

Drupal is one of the most popular platforms with which to develop websites. With more and more organizations looking to build engaging digital experience for their stakeholders, the Drupal Content Management System offers a mobile-first platform with native support for integrations, better performance, and scalability. The new version brings significant changes to its module development and theme creation techniques, improving performance and refining the development experience.

This book will help you develop your own website using Drupal 8 in a step-by-step manner. You’ll start off by setting up your development environment, enabling you to begin writing custom code for a Drupal-powered website through PHPStorm. You will learn about configuration management and creating custom content types before exploring the HTML5 features included with Drupal 8.

You will then get familiar with Drupal 8’s mobile-first features, explore the built-in WYSIWYG and in-line editing capabilities of Drupal 8, and enhance the overall authoring experience. Later, you will create and enhance a Media Entity Lightbox module, before taking an in-depth look at the Views module.

We then cover some advanced search concepts and walk you through the installation and integration of the Java-based Apache Solr search engine. Finally, you will explore and configure the built-in support for REST and extend its support by installing the RESTful module. By the end of the book, you will have created a recipe sharing website while gaining a solid understanding of development best practices for Drupal 8.

Publication date:
June 2016
Publisher
Packt
Pages
398
ISBN
9781785284885

 

Chapter 1. Setting Up a Drupal Development Environment

In this chapter, we will be setting up a development environment for the Drupal framework. We will be also installing and exploring Drupal 8 from a developer's perspective and learn about the virtual development environment.

In this chapter, we will cover:

  • Installing Drupal for local development

  • Setting up PHPStorm IDE for Drupal development

  • Installing, configuring, and using Drush 7 with Drupal 8

  • Installing Drupal 8

  • GitHub for Drupal development

  • Installing Vagrant using PuPHPet

 

Installing Drupal for local development


You can install Drupal using different web servers and databases. The most commonly used combination is Apache, MySQL, and PHP, often referred as the *AMP stack.

Specific to Drupal, there are two main ways you can do this:

  • Acquia Dev Desktop (for Mac and Windows only)

  • AMP: Manual installation of Apache MySQL PHP

These two ways have the following minimum system requirements:

  • Disk space: A minimum installation requires 15 megabytes. 60 MB is needed for a website with many contributed modules and themes installed.

  • Web server: Apache, Nginx, Microsoft IIS, or any other web server with proper PHP support.

  • Database:

    • MySQL 5.5.3/MariaDB 5.5.20/Percona Server 5.5.8 or higher with PDO and an InnoDB-compatible primary storage engine

    • PostgreSQL 9.1.2 or higher with PDO

    • SQLite 3.6.8 or higher

 

Time for action – installing Drupal using Acquia Dev Desktop


If you are using Mac or Windows, Acquia Dev Desktop is the easiest method to develop Drupal:

  1. Visit https://www.acquia.com/downloads and download the installer you need.

  2. Open the Acquia Dev Desktop file and start the installation process. If you are a Windows user, find the folder that you used to save your Acquia Dev Desktop download and double-click on the .exe installation file.

  3. If you are a Mac user, find the folder that you used to save your Acquia Dev Desktop download. Move the file to the Application folder and double-click on the .dmg installation file. A new folder will pop up, with an icon that says Acquia Dev Desktop Stack Installer. Go ahead and click on that. Allow the installer to run on your Mac or PC.

  4. Let the installer run and click on Next for every step once you have read and agreed to the terms and conditions. Next, you can select where you are going to install the application stack (Apache, MySQL, and PHP) and where you are going to install your first Drupal site.

  5. During this installation, we won't use our Drupal site directly but in other chapters, if you decide to use Dev Desktop for development work, you can import sites from other locations on your hard drive as well. Leave these at the default and click on Next. On the next page, you have to select the port number for Apache and MySQL. In most cases, it will work without any conflict, but if you are using multiple AMP stacks, you can go ahead and make the changes.

  6. On the next screen, we set the defaults for our first Drupal site. Fill this out and click on Next. Make sure you note down the username and password. You will need this to log in to your Drupal site.

    Creating credentials for the first Drupal site

  7. Click on Next to begin the installation process. Once the installation is complete, click on Finish, and this will open up the Acquia Dev Desktop Control Panel.

    Acquia Dev Desktop Control Panel

  8. Go to your new site by clicking on Go to my site. Use Manage my database to browse to PHPMyAdmin.

    Login page for setting up the site

What just happened?

You have installed Drupal using Acquia Dev Desktop. From here, you can begin working on your new Drupal site.

Note that the site is running using Acquia Drupal, which is little different in that you would download from your https://www.drupal.org/. Installing Acquia Dev Desktop on Windows is almost similar.

Installing Drupal the localhost way

Now we will go through installation steps of AMP packages for both Mac OS X and Windows operating systems.

 

Time for action – installing a Mac OS X AMP stack


For Mac OS X, we will use the MAMP package to install an AMP stack.

  1. First, download the latest version of MAMP from https://www.mamp.info/en. Once MAMP is completed downloading, double-click on the downloaded .pkg file. Move the file to the application folder and double-click on the MAMP .pkg file. This will launch the MAMP installer. The system installer will guide you through the installation process.

  2. After completing the installation process, launch your local server.

  3. Start MAMP and click on the Start Servers button. You will see the server status in the top-right corner. Click on the Open WebStart page button. You will see the default MAMP start page having links to access utilities such as phpMyAdmin, phpInfo, SQLite Manager, phpLiteAdmin, FAQ, MyFavoriteLink, and the MAMP website.

    The default MAMP start page

  4. After successful installation, you can launch your local servers. Start MAMP and click on the Start Servers button. In the status display in the top-right corner, the launch status of the servers is displayed. If necessary, you will be asked for your administrator password.

    Tip

    The process for setting up a development environment for Mac OS X is very similar to setting a development environment for a Linux distribution. They are both Unix-based operating systems. If you aren't already tied to a particular Linux distribution and would like to set up a development environment in Linux, then I highly recommend Ubuntu. There are excellent directions on setting up a Drupal development environment available at https://help.ubuntu.com/community/Drupal.

What just happened?

Congratulations! You now have a working AMP stack installed on your Mac.

 

Time for action – installing a Windows AMP Stack


For windows, we will use XAMP:

Tip

I do most of my Drupal development on Mac OS or Ubuntu. Unix-based OS is a better fit for Drupal development as there are many development-oriented aspects of Drupal that are Unix-centric. From cron to .htaccess based clean URL, a lot of documentation on http://drupal.org/ will be biased towards the Unix OS.

  1. Download the latest version of XAMPP from https://www.apachefriends.org/download.html. Once your download is complete, double-click on the .exe file to install the program. Accept the default settings and complete the installation process. You can change the settings by editing the configuration files later at any point in time.

  2. After completing the installation process, start the XAMPP control panel.

    XAMPP Control Panel

  3. Inside the XAMPP Control Panel application, click on the Start button next to Apache and MySQL. Now open up your favorite web browser, navigate to http://localhost, and you should see something similar to the following screenshot:

  4. XAMPP is not meant for production use but only for development environments. Secure XAMPP before publishing anything online. You should go to the URL http://localhost/security/. With the security console, you can set a password for the MySQL user root and phpMyAdmin.

What just happened?

Congratulations! You now have a working AMP stack installed on your Windows PC. And also you have a working AMP stack installed on your Mac!

PHP configuration

Drupal 8 recommends PHP version 5.4 or higher with the CURL extension. The latest version of MAMP includes PHP version 5.5 (it also includes an older PHP version and allows you to switch between them). The latest version of XAMPP for Windows includes PHP version 5. Although this version of PHP meets the requirements of Drupal 7, there are some PHP-related settings that need to be tweaked before we install Drupal in order to ensure that things will run smoothly. The PHP requirements list is from Drupal 8 at https://www.drupal.org/requirements/php.

 

Time for action – modifying the php.ini settings


To modify the php.ini settings in Mac OS X and Windows:

  • Mac OS X: Use your favorite text editor to open the php.ini file located at /Applications/MAMP/bin/php/php5.4.6/conf.

  • Windows: Use your favorite text editor to open the php.ini file located at C:\xampp\php. I like Notepad++.

Navigate to the respective folder location in Mac OS X and Windows, and edit the settings to match the following values:

max_execution_time = 60;
max_input_time = 120;
memory_limit = 128M;
error_reporting = E_ALL & ~E_NOTICE

What just happened?

Congratulations! You have successfully modified the php.ini file in Mac OS X and Windows systems.

Modifying the MySQL my.cnf settings

To modify the MySQL my.cnf settings in Mac OS X and Windows:

  • Mac OS X: MAMP does not use a my.cnf file by default. So you must copy the file at /Applications/MAMP/Library/support-files/my-medium.cnf to /Applications/MAMP/conf/my.cnf (notice the new name of the file).

  • Windows: For XAMMP, open the my.ini file located at C:\xampp\mysql\bin.

    Tip

    You may want to make a backup copy of this file before you begin to edit it.

 

Time for action – setting up MySQL for Drupal


Open the my.cnf/my.ini file in your text editor, and find and edit the following settings to match these values:

# * Fine Tuning
#
key_buffer = 16M
key_buffer_size = 32M
max_allowed_packet = 16M
thread_stack = 512K
thread_cache_size = 8
max_connections = 300

One of the real goals for the Drupal MySQL configuration is the max_allowed_packet setting. This has always been a source of bewildering errors in the past for me and many other Drupal developers that I know, and it is a setting that is specifically mentioned on the http://drupal.org/requirements#database page, under the database server section.

When you are ready to make your site live, there are some excellent performance tuning tips available at http://drupal.org/node/2601.

What just happened?

Congratulations! You have successfully updated the MySQL settings in Mac OS X and Windows systems.

 

Time for action – creating an empty MySQL database


Before we can install Drupal, we need to create a new and empty MySQL database.

Both MAMP and XAMPP include phpMyAdmin—a web-based administration tool for MySQL. We will use phpMyAdmin to create an empty database for Drupal.

  • Mac OS X: With MAMP running, open your favorite web browser and go to http://localhost:8888/phpMyAdmin.

  • Windows: With XAMPP running, open your favorite web browser and go to http://localhost/phpmyadmin/.

You will see the following screen:

What just happened?

You have installed a fully functional AMP stack that has been configured specifically for Drupal, and you have created an empty MySQL database as a preliminary step for installing Drupal.

 

Installing Git


Git is a free source control and versioning software that has become very popular over the last few years. In February 2011, Drupal (https://www.drupal.org/) migrated from the outdated CVS versioning system to Git. The migration to Git has enabled a completely new way for Drupal developers to interact with Drupal (https://www.drupal.org/), and we will highlight this enhanced interaction throughout the book. However, we will also immediately start using Git to facilitate setting up a Drupal development environment. So, if you don't have Git already installed on your computer, let's get it set up.

 

Time for action – installing Git for Mac OS X


To install Drush for the Mac, we are going to use Homebrew (an open source package manager for Mac OS X), with installation instructions available at http://brew.sh/.

Once you have Homebrew installed, installing Git is as easy as opening up the Terminal application (in /Applications/Utilities) and typing the following command:

brew install git
 

Time for action – installing Git for Windows


Download the installer for Windows from http://git-scm.com/downloads. Double click on the .exe file to install the program.

  1. Click on the downloaded file link to begin the installation process.

    The Git setup wizard

  2. Click on Next, and then on Next again on the GNU General Public License screen.

  3. On the Select Destination Location screen, click on Next to accept the default destination.

  4. On the Select Components screen, accept the defaults again and click on Next.

  5. On the Adjusting your PATH Environment screen, select Use Git and optional Unix tools from the Windows Command Prompt as this will allow Git to work with Drush, which we will cover next.

    Choosing how to use Git from command line

What just happened?

You have installed the Git version control system—a tool that will greatly facilitate interaction with the existing contributed code at Drupal (https://www.drupal.org/).

 

Installing Drush


Drush, a portmanteau of the words Drupal and shell, is a command-line utility that facilitates the management of a Drupal environment from your favorite shell (the Terminal application on Mac OS X and Ubuntu, and the Command Prompt application on Windows). For example, installing a contributed module on Drupal (https://www.drupal.org/) can be as easy as running the following commands from the command line:

drush dl modulename
drush en modulename -y
 

Time for action – installing Drush for Mac OS X


To install Drush for Mac, we are going to use Homebrew again at https://www.drupal.org/node/954766.

In the Terminal, type the following command:

brew install drush
 

Time for action – installing Drush for Windows


To install Drush in Windows, we will need to use Cygwin that provides a Linux-like environment for Windows.

We are now going to install Drush using Composer in Cygwin:

  1. Download and install Cygwin

  2. In the packages section install the following packages:

    • ncurses: This is a library used for showing text-based user interface.

    • git: This is a version control package

    • bsdtar: This is used to untar compressed tar files.

    • curl: This is a client-side URL transfer package.

  3. Now installer Composer, a PHP dependency package management tool, using the Composer installer.

  4. From Cygwin terminal, type the following command to install Drush 8:

     composer global require drush/drush:8.*
    

Note

Learn more about Cygwin project at http://www.cygwin.com/. For more information on installing Drush, check Install Drush on Windows—the easy way at https://www.drupal.org/node/594744

 

Installing Drupal 8


All right, now we are getting somewhere. Now that we have created a database, installed Git, and installed Drush, we have everything in place to install Drupal 8.

 

Time for action – installing Drupal 8


We are going to use a combination of Drush and Git to install Drupal:

  1. Mac OS X: Open up the Terminal application, and type the following command:

    cd /Applications/MAMP/htdocs
    

    Windows: Open Command Prompt and type the following command:

    cd C:\xampp\htdocs
    
  2. Now, we are going to use Git to locally clone the Drupal core Git repository into a new d8dev folder (this will take a few minutes or so depending on your network bandwidth):

    $ git clone --branch 8.1.x http://git.drupal.org/project/drupal.git d8dev
    Cloning into 'd8dev'...
    remote: Counting objects: 456756, done.
    remote: Compressing objects: 100% (95412/95412), done.
    remote: Total 456756 (delta 325729), reused 445242 (delta 316775)
    Receiving objects: 100% (456756/456756), 100.04 MiB | 8.60 MiB/s, done.
    Resolving deltas: 100% (325729/325729), done.
    cd d8dev
    
  3. Next, we want to use Git to switch to the latest Drupal 8 release. First, we will list all the available releases:

    8.0-alpha1
    8.0-alpha10
    8.0-alpha11
    8.0-alpha12
    8.0-alpha13
    8.0-alpha2
    8.0-alpha3
    8.0-alpha4
    8.0-alpha5
    8.0-alpha6
    8.0-alpha7
    8.0-alpha8
    8.0-alpha9
    8.0.0
    8.0.0-alpha14
    8.0.0-alpha15
    8.0.0-beta1
    8.0.0-beta10
    8.0.0-beta11
    8.0.0-beta12
    8.0.0-beta13
    8.0.0-beta14
    8.0.0-beta15
    8.0.0-beta16
    8.0.0-beta2
    8.0.0-beta3
    8.0.0-beta4
    8.0.0-beta5
    8.0.0-beta6
    8.0.0-beta7
    8.0.0-beta8
    8.0.0-beta9
    8.0.0-rc1
    8.0.0-rc2
    8.0.0-rc3
    8.0.0-rc4
    8.0.1
    8.0.2
    8.0.3
    8.0.4
    8.0.5
    8.0.6
    8.1.0
    8.1.0-beta1
    8.1.0-beta2
    8.1.0-rc1
    8.1.1
    8.1.2
    8.1.3
    
  4. You will see that the latest release is 8.1.3, but you should substitute whatever the latest release may be for you and use that in the following Git command:

    git checkout 8.1.3
    Note: checking out '8.1.3'.
    You are in the detached HEAD state. You can look around, make experimental changes, and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout.
    If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example:
    git checkout -b new_branch_name
    HEAD is now at 079a52b. Revert Issue #2457653 by Gábor Hojtsy: System.site langcode is both used as a file language code and a site language code.
    

    Tip

    By using Git, we are linking our download of Drupal to Drupal's Git repository for our local core Drupal install. This will facilitate an easy update process for future Drupal core updates.

    Drupal 8 uses Symfony components, which is managed by using a vendor directory and has to be downloaded in the Drupal repository using Composer. For Drupal repositories cloned via Git, you need to run composer install, which will download the vendor directory.

    Next, we are going to go through the web-based installation process to set up Drupal:

    • Mac OS X: Open http://localhost:8888/d8dev/ in your web browser

    • Windows: Open http://localhost/d8dev/ in your web browser

  5. You should see the following screen:

    Select your language and click on Save and Continue.

  6. Select the Standard profile and click on Save and Continue.

  7. On the Database configuration screen, select MySQL. Enter d7dev as the database name and root as the database username and password. Click on Save and Continue. You will see the site installation screen.

    For a local development site, it is quite convenient to use the root MySQL user. However, for a live/production site, you should always create a unique MySQL user and password for your Drupal database.

  8. On the Configure site screen, enter the following values (the e-mail address doesn't have to be real but must appear valid):

    • Site name: d8dev

    • Site e-mail address: A valid e-mail address

    • Username: admin

    • Password and Confirm password: admin (this is only a development environment, so keep it simple)

    • E-mail address: The same as used for Site e-mail address

    Fill out the rest of the form and click on Save and continue. Your Drupal site installation is complete. So click on the Visit your new site link to see the site.

What just happened?

You used Git to get a brand new Drupal 8 site up and running. In the coming chapters, you will see how useful Git, along with Drush, can be for day-to-day Drupal development.

 

Installing the PHPStorm IDE


PhpStorm has emerged as one of the most Drupal-friendly IDEs for developing and debugging Drupal modules and themes at https://confluence.jetbrains.com/display/PhpStorm/Drupal+Development+using+PhpStorm.

 

Time for action – installing the PHPStorm IDE


Go to https://www.jetbrains.com/phpstorm/download/, and download the free 30-day trial of the correct version of the PHPStorm IDE for your operating system. Double-click (for Windows and Mac) on the file once it has completed downloading, and follow the instructions given on the page to install PHPStorm as per the documentation.

Tip

By no means should you feel like you have to use PHPStorm. There are a number of other good IDEs out there, and you may already be using a different IDE or may just be happy using your favorite text editor. However, I will be using PHPStorm throughout the book, so it may be easier to follow along if you are also using PHPStorm.

The Drupal development plugin is activated by default in PHPStorm. Otherwise, you can enable it in the settings dialog box.

 

Time for action – creating a new PHPStorm project


Upon opening PHPStorm for the first time, you should see the following screen:

  1. Click on the Create New Project button, select PHP Project, and click on Next. At this point, you will see the New PHP Project window:

  2. Browse to your new Drupal 8 project. For Windows, it is C:\xampp\htdocs\d7dev, and for Mac OS X, it is /Applications/MAMP/htdocs/d7dev. After you have selected the location of your new Drupal 7 installation, click on Finish.

 

Drupalize PHPStorm IDE


As soon as you set up a project in PHPStorm for an existing project or a new Drupal module, the IDE checks if the development environment is configured for Drupal or not. It will suggest you to enable Drupal support with a popup and an event in the Event log.

Clicking on the Enable Drupal support link will open the configuration dialog box. You can change the Drupal project installation path and select version number 8 for Drupal 8.

You can also change the Drupal integration settings later by opening the Preferences dialog.

To get updated information about Drupal integrations with the latest version of PHPStorm, visit https://confluence.jetbrains.com/display/PhpStorm/Drupal+Development+using+PhpStorm.

 

Drupal from a developer's perspective


If you are new to Drupal development, the first thing that you should understand is that being a good Drupal developer means being a part of the Drupal community. Drupal development is very much an open source process, and as such it is a community-driven process.

Drupal has numerous resources to assist developers of all experience levels. However, before you can take advantage of all of these resources, you need to be a member of Drupal (https://www.drupal.org/). So, if you haven't already joined Drupal, now is the time to do so at http://drupal.org/user/register. Once you are a member and have logged in to Drupal (https://www.drupal.org/), you will see the folllowing screen:

As we move forward with Drupal 8 development, we will return to your Drupal (https://www.drupal.org/) dashboard on many occasions to keep a track of issues of the contributed modules that we are using. We will also utilize the new sandbox development feature that Drupal has added as part of the migration to Git.

 

Installing Vagrant


As developers, a lot of times we need to set up a local version of the AMP stack that is similar to the production web server. Vagrant allows developers to build the virtualized environment once and keep sharing it for every case.

Vagrant is written in Ruby, so if you are unfamiliar with Ruby, writing Vagrant files could be challenging. This is where PuPHPet helps. Using PuPHPet, we can generate the package to create a virtual machine the same way as we specify on the PuPHPet website. We just have to provision it.

 

Time for action – installing Vagrant


To install Vagrant, download the appropriate Version of vagrant packages and the VirtualBox for your system from https://www.virtualbox.org/wiki/Downloads:

  1. Create a location: Choose a location where you would like your VM to be saved. This location is regardless of where you save your VM configuration and files. Create a folder where you decide to store your VM. For example, in Windows, we can use C:/drupalvm.

  2. Install VirtualBox: Download and install the latest version of VirtualBox from https://www.virtualbox.org/wiki/Downloads, depending on the OS package.

  3. Install Vagrant: Installing Vagrant is easy. Just download your OS-specific package from https://www.vagrantup.com/downloads.html. Follow the OS-specific normal installation process.

  4. Create your VM at PuPHPet: Until now, we have installed Vagrant and VirtualBox. Now let's create our VM configuration at PuPHPet. Open the URL https://puphpet.com/. Here we will select what we need from our VM.

  5. VM Settings: On the first page, click on Get started right away. This should lead to the following page. Here I have selected the distro as Ubuntu Trusty 14.04 LTS x64. You can select your distro and other configurations depending on your server choice. You can leave other settings as default if you are not aware of them.

    Selecting distro for your machine

Once you have selected your favorite package, hit the System Packages button.

In the next four steps, you can just leave the settings as they are and click on the buttons on the right-hand side to move forward.

You should get the Nginx option. Now I do not want Nginx as a web server. Instead I have opted for Apache.

Just browse to the page's top and uncheck Install Nginx. This should collapse the Nginx configuration form and bring the button to move to the Apache configuration page.

Select the web server of your choice

You can select your web server configuration and move next to select the PHP configuration. Here, I have selected Install Xdebug, a good tool for Debugger, and a profiler tool for PHP.

After this, I just clicked on other language configuration without making any changes and moved on to MySQL.

Selecting database

Here you can select the MySQL version and root password, add more users, and create new databases. You can explore other databases too if you like.

Again, keep moving forward until you reach Create Archive, and you are done. Download your custom server config! This will download a ZIP file with the configuration of your custom VM.

  1. Set up your VM: Extract the ZIP file you have downloaded just now and navigate to the location you just extracted your ZIP file to.

    Open the Terminal in Mac OS X or Ubuntu. In Windows, you should open the command line with administrative privileges.

    In the Terminal, browse to the folder location and type this:

    vagrant up
    

    Hit Enter. It will take a couple of minutes and you should see a message, everything finished installing.

  2. Setting up the host: There is the last step of setting up the hostname before we start using our VM. For Drupal, it is always better to use proper domain names. To create virtual hosts, we need to make changes to the host files on our computer. Browse to the following folders depending on the OS:

    • Windows: C:\windows\system32\drivers\etc\hosts

    • Linux (Ubuntu): /etc/hosts

    • Mac: /private/etc/hosts

      192.168.1.32 drupal8.dev  www.drupal8.dev

    You can use your favorite editor with administrative permission to make changes to the host file. I have selected to use drupal8.dev as my domain name. So let's add this line to the host file:

    We have used this IP address while creating our VM config file on PuPHPet. If you do not remember it now, you can find it by opening config.yaml in Notepad. The config.yaml file is located inside your puphet folder, which you placed inside your VM folder earlier. You should see the IP address top next to private_network.

We have just completed configuring a VM on our computer. You can open the browser and type your domain name—drupal8.dev.

What just happened?

You have just installed Vagrant for project development. Now you can build a VM, which is your actual production server. And you can switch to different VMs depending on your different project requirements. What you need to install a new VM is a config file from PuPHPet.

 

Summary


You have reached the end of the chapter! You should now have a working Drupal 8 website and PHPStorm IDE to begin custom Drupal development. We are yet to write actual code, so I am excited to get started with some development examples. However, this chapter has given us the tools and configuration system needed to make the development process much easier and enjoyable. Now, we are ready to begin developing for Drupal in earnest. In the next chapter, we will create a new content type and a basic custom module to change one of the fields on our content type.

About the Authors
  • Neeraj Kumar

    Neeraj Kumar is a Drupal architect, author, project manager, and overall geeky guy who enjoys using technology for bringing value to businesses. He is also the chief engagement officer of Valuebound, an India-based Drupal consulting firm that provides enterprise Drupal solutions for media and product companies.

    He earned his bachelor's degree in architecture from the Indian Institute of Roorkee. Yes, you heard it right; he is an architect by degree training and used to design buildings. But nowadays, he is more into architecting Drupal solutions for enterprises and advocating best practices, along with contributing to the awesome Drupal community. You can follow Neeraj on Twitter at http://twitter.com/neerajskydiver or his LinkedIn profile at https://in.linkedin.com/in/neerajskydiver.

    Browse publications by this author
  • Tassos Koutlas

    Tassos Koutlas is a senior technical consultant at Cameron & Wilding in London. He has over 13 years of experience in producing web-based projects and machine learning / image processing algorithms, and administering IT systems. He has worked in commercial and research-based environments in the UK, Greece, and Italy. Tassos has successfully delivered projects such as the National Baseball Hall of Fame redevelopment, where he helped develop a better way to deliver content online, utilizing the Hall's vast asset collection. He has been part of award-winning projects such as the Qatar Museums website, where he helped extend their website with an events calendar and a blog section, and has been supporting MoMA online collections. More recently, he has been helping Investors in People embark on becoming an agile organization.

    Prior to joining Cameron and Wilding, Tassos had been a part of the team working at the Southbank Centre, providing support at multiple levels in their digital transformation efforts. His previous projects include Q base R&D, a start-up focused on personalized skin cancer prevention, and the Athens 2004 Olympic games. Tassos graduated from the University of Manchester with a BSc Hons in computing science and holds a PhD in image processing and machine learning. He has been working with Drupal for 10 years. He is an Acquia certified developer and certified Scrum Master.

    Browse publications by this author
  • Samuel Keen

    Samuel Keen first found an interest in web development while traveling abroad, when he learned how to build an online photography portfolio. A trained musician, he found that the same interests drove his web development: "Both music and the Web have frameworks that give us almost endless abilities to build and be creative." Returning to the UK, he began designing and developing websites for people in creative industries. Since 2011, Samuel has worked almost exclusively with Drupal. He is particularly interested in the frontend, UX, and automated workflows, with a passion for cutting edge Sass.

    He currently works as a frontend developer for Cameron & Wilding, a Drupal development agency based in London. Samuel has worked on projects for The Economist, the Imperial War Museums, London South Bank University, and The Telegraph.

    Browse publications by this author
  • Edward Crompton

    Edward Crompton has been developing with Drupal since he was first hooked by version 5 in 2007. His adventures in open source software development have taken him from the so-called Silicon Roundabout in London to the villages of South India, where he built an application for gathering data about school building projects. He's now based back in London, where he can often be found riding a rusty bicycle and digging in his rented potato patch.

    Edward works as a Drupal developer for Cameron & Wilding Ltd. He's a keen supporter of Drupal community events in London and likes to exchange knowledge about Drupal at meetups and in the pub.

    Browse publications by this author
  • Krishna Kanth

    Krishna Kanth is a Drupal developer and an active contributor to the Drupal community who has been building websites since he was a student at engineering college. He is a specialist in the Drupal backend and equally loves to work with other PHP frameworks.

    He is a graduate in information technology from Jawaharlal Nehru Technological University, Anantapur, Andhra Pradesh, India. Krishna started his career as a freelancer and worked with many NGOs. Now he works as a senior Drupal consultant and developer at Valuebound, India. You can follow him on Twitter at https://twitter.com/_krishna_kanth or his LinkedIn profile at https://in.linkedin.com/in/krishnakanthp.

    Browse publications by this author
  • Rakesh James

    Rakesh James is a Drupal developer, evangelist, enthusiast, and contributor to the Drupal community. He is a specialist in Drupal who simply loves to work with Drupal again and again. He is one of those who believes and thanks God for Drupal on a daily basis, because he believes that if Dries hadn't created Drupal, he may not have achieved his current lifestyle. So he is always looking to evangelize, train, and mentor newbies to Drupal.

    Rakesh graduated from the Government Engineering College, Thrissur, which is one of the finest engineering institutes in Kerala. He started his career as a software programmer in PHP and Drupal. Currently, he is hand in hand with Valuebound as a senior Drupal consultant, developer, contributor, mentor, trainer, and architect for Drupal projects.

    Browse publications by this author
  • Malabya Tewari

    Malabya Tewari is a full-stack Drupal developer, Drupal evangelist, trainer, and open source enthusiast who likes to reinvent and improve his working environment continuously, mainly because of his unsatisfied soul which seeks to achieve perfection. He is an active contributor to the Drupal community via code, as well as by organizing Drupal meetups and "Drupal in a day" workshops in Bangalore.

    A graduate in computer science from Sikkim Manipal Institute of Technology, who started his career as an intern in a Drupal firm, Malabya now works as a Drupal consultant and developer at Valuebound, which is a Drupal shop that delivers enterprise Drupal solutions for media and product companies.

    Browse publications by this author
  • Kurt Madel

    Kurt Madel is a senior manager and developer for Captech Consulting in Richmond, Virginia, USA. He has worked on open source CMS projects for over 6 years. Kurt contributes regularly to Drupal.org and is the maintainer of several modules. In addition to Drupal, he has been doing Java EE development since 2000 and has focused on mobile web development over the last 2 years. When he is not writing or programming, Kurt enjoys cycling and spending time with his wife and four boys.

    Browse publications by this author
Latest Reviews (3 reviews total)
So far so good, but much more to read at this stage
Gutes Buch, dass gerne noch ein wenig mehr in die Tiefe gehen könnte, aber einen tollen Überblick bietet. Qualität des zugehörigen Ebooks ist auch sehr gut.
Some codes don't work immediately with latest Drupal 8.1.x. They require me to do little research and fixes. Otherwise its good!
Drupal 8 Development: Beginner's Guide - Second Edition
Unlock this book and the full library FREE for 7 days
Start now