Entity validation in Drupal 8 - part 2

In the first part, we saw how entity validation works in Drupal 8, why it is a separate component and how most parts are adopted from Symfony's entity validation framework. We will try our hands on creating our own custom validator in this post.This validator will fail to create a node if the user ID is not specified. In other words, prevent creation of anonymous nodes.

Entity validation in Drupal 8 - part 1 - how validation works

Drupal 8 has its entity validation separate and decoupled from the typical validation given by its form API. This is done for a lot of reasons. For one, entities might get added from other non UI means, like via the REST API, or programmatically, while importing data from an external source. Under these circumstances, the entity validation API comes in handy.

Using the entity API in Drupal 8

There is a lot of literature about entities and their purpose in Drupal 7 context. Most of it has been adopted in Drupal 8 as well. In this post, I'll highlight the differences between D7 and D8 entities and how to use the entity API in 8.

Entities have their own classes in 8. Also, Drupal 8 introduces the concept of config entities. These are used to store user-created configuration if its more than a piece of text, boolean or integer. They differ from the usual entities in the following ways:

Writing a custom authenticator in Drupal 8

Drupal 8 allows module developers to write their own customized authentication schemes. In this post, we shall see how we create one. Let's take a hypothetical custom authentication mechanism called the token authentication mechanism. It works like this:

The site administrator has a limited set of auto generated tokens. They issue these tokens to users who want to access the site's resources. These resources can only be accessed by giving the correct token as a part of the URL parameter, like my-page?token=ABCXYZ.

Routing and controllers in Drupal 8

The routing system of Drupal 8 is a complete rewrite of Drupal 7 and its previous versions' hook_menu. A Drupal route is a URL path with a specific return content. This return content is usually specified as a method of a controller class which returns a render array.

The routing system is powered by Symfony's HTTP Kernel component, which we will revisit later. It is not necessary to understand this in order to work with routes.

Let's dive straight away and create a new route.

Tagged services in Drupal 8

We saw how to write a simple service container in Drupal earlier. We shall build a tagged service now. To illustrate a proper use case for tagged services, let's contrive a scenario where you add a pipeline custom filters to user text before rendering it on the page.

First, clone the code which will be used in this post.

Drupal composer workflow - part 2

In the previous post, we saw how to add and manage modules and module dependencies in Drupal 8 using Composer.

In this post we shall see how to use an exclusive composer based Drupal 8 workflow. Let's start with a vanilla Drupal install. The recommended way to go about it is to use Drupal Composer project.

Drinking the Drupal-Composer Kool-Aid?

Do you want to manage modules and dependencies the PHP way instead of the "Drupal" way? Don't know how to use composer with Drupal? Are you planning to ditch drush make approach and adopt a composer based workflow?

If you answered yes to any of the above questions, then you should read this post.

Compose what?

Composer is PHP equivalent of Python's pip, Ruby's bundler and Node's npm. It helps manage projects and dependencies for a PHP project.

Publishing a book using org-mode

I've been flirting with org-mode lately and writing a book at Leanpub. Why not use org mode to do it, I thought, and this is the result. There is already an awesome org-mode exporter for leanpub. Go ahead and download it. This post builds upon it.

Before we dive into the org-mode side of things, let's look at how Leanpub publishing works.

Writing your first Drupal service

We got a basic understanding of what a Drupal service is previously. Let's put that into action by creating a simple service in Drupal 8. Take note that this service what we are about to create doesn't do anything useful yet, and is written with a pedagogical purpose in mind.