Entities are the building blocks that make up just about everything on a Drupal site. Regardless of whether entities provide configuration information or content, they are absolutely crucial to Drupal's data model.
In this tutorial we'll:
- Look at the overall class hierarchy between the various entity types to see how they're related.
- Examine differences between configuration and content entities in the code that defines them.
- Look at some of the core code required to create an entity type.
By the end of this lesson you should be able to use an example to create a custom entity type in code.
Often when building a site in Drupal you'll find yourself wanting to display a list of nodes, or find entities created by a particular author, or locate some content based on a particular set of criteria. Rather than querying the database directly, Drupal provides a helper class, EntityQuery
, to make things a bit easier. The EntityQuery
class will probably look very familiar to you if you're familiar with the Database API, or the EntityFieldQuery
class in Drupal 7.
In this tutorial we'll:
- Go through several examples of using
EntityQuery
to find subsets of content. - Demonstrate how to iterate over the results of an
EntityQuery
query.
By the end of this tutorial, you should understand how to use entity queries to create custom sets of data from entities.
Entity API Overview
FreeWhen learning Drupal development, it won't be long before you encounter the word "entity" and the Entity API. But what are entities in Drupal? How can you use them to build your site? When should you use the Entity API?
This tutorial will explain Drupal's Entity system from a high level. We'll look at:
- The main problems the Entity system solves
- Key terms you should know
- Key concepts we'll explore as we dive into Drupal's Entity API
By the end of this tutorial you should be able to explain the problems that the Entity API solves, and when you should use it in your own code.
Drupal includes the Symfony Validator component, and provides an Entity Validation API to assist in validating the values of fields on an entity. By using the Entity Validation API you can ensure that you're validation logic is applied to Entity CRUD operations regardless of how they are triggered. Whether editing an Entity via a Form API form, or creating a new Entity via the REST API, the same validation code will be used.
Using the Entity Validation API in order to validate the value of a field on an entity requires:
- Defining (or choosing) a constraint plugin
- Defining (or choosing) a validation plugin
- Adding the constraint to the field definition
In this tutorial, we'll look at how this Validation API works and how it can be used in custom code to ensure our entities have properly constructed values. We'll look at how this validation works in Drupal core and how we can add our own additional constraints. We'll also see how to work with the error messages returned from the validator when our entity doesn't pass validation.
What Is Docker?
FreeDocker often seems like an impenetrable product. Is it a VM system? A suite of development tools? A clustering product? A software distribution facility? When the answer is "yes" to each of these, it only becomes more confusing. For the Drupal developer, Docker is a way to provide a local development environment to run web server software.
In this tutorial, we'll:
- Define the terms hypervisor, virtual machine (VM), and containers
- List the advantages of containers over VMs
- List the advantages of Docker for Drupal developers
Install Docker
FreeInstalling Docker is easy, but there are some details you may want to consider before you download and run the installer.
In this tutorial, we'll focus on:
- Why Linux is Docker’s native environment
- The difference between Docker edge vs. Docker stable
- Why Docker for non-Linux requires a VM
Now that we know what Docker is, what containers are, and how to install Docker, just how do we use containers? While graphical user interfaces (GUI) exist for Docker, the primary way to interact with it is via the command line.
In this tutorial, we'll:
- Start Docker for Mac or Docker for Windows
- Use the
docker run
command to run a container interactively - Break down the arguments of the
docker run
command
Containers are sandboxed applications that can be run anywhere, no matter the underlying host OS. Docker containers are quite different from VMs in a number of ways that need to be understood before we can use them to develop Drupal sites.
In this tutorial, we'll:
- Define a container
- Explain how a container is a more limited environment than a VM
When we use docker run
to start a container, we download a compressed, ready-to-use container called an image. Images make containers easy to share via a registry like Docker Hub, but also affect how file storage works when using containers.
In this tutorial, we'll:
- Discuss how file storage works in Docker containers
- Describe images, base images, and the scratch image
- Identify layers and show how layers make up Docker's filesystem
Running a container interactively can be useful, but often it's not what we really need. A web server stack is made up of several components such as the Linux OS, the Apache web server, a PHP runtime, and a database such as MySQL. Collectively, we call this a LAMP stack. If we were to run these in Docker with what we now know, we'd have to keep open several terminal windows!
Obviously that's not what we want to do. Instead, we want to run the containers in the background. That way, we can use them like we would any web server. Fortunately, Docker makes running and managing a container in the a background easy with just a few commands.
In this tutorial, we'll:
- Start a container in the background
- Use
docker ps
to list running Docker containers - Use
docker run
to enter a container running in the background - Use
docker kill
to stop a container running in the background
Often we don't want to run just one container at a time, but a set of containers that act together to provide a unit of functionality. Yet, docker run
only starts one container at a time, with one command in each container at a time.
Docker Compose lets us overcome this limitation by allowing us to define a single file that describes multiple containers, their relationship to each other, and utilities to manage that set of containers as a single unit.
In this tutorial, we'll:
- Introduce Docker Compose
- Run multiple containers at once using Docker Compose
- Identify the purpose of docker-compose.yml
- Learn what resources a set of containers share
Docker Compose allows us to manage several related containers as a single group. We define container sets by creating a creating the Compose file, docker-compose.yml.
In this tutorial, we'll:
- Create the basic structure of the Compose file
- Define a container set using off-the-shelf containers
- Describe where to place it in your project
- See how directory names are significant in Compose
Once we have the docker-compose.yml file created, we can use it to work with a set of containers. Instead of the docker
command, Docker Compose has its own command to work with multiple containers at once: docker-compose
.
In this tutorial, we'll:
- Cover the basic usage of the Compose command
- Describe how to start, stop, and list running container sets
One of the biggest questions when first learning Docker is "How do we get data into and out of containers?" We can use docker-compose exec
to interact with them on the command line, but that doesn't fulfill our needs as developers. Docker provides several mechanisms to share data with the container, each with specific purposes. Docker Compose lets us leverage each of those easily with just a few lines of YAML.
In this tutorial, we'll:
- Identify the various ways we can get data into containers
- Define volumes
- Describe how to use environment variables in Docker
- Describe how to expose network ports from a container set
Bind Volumes are essential to the Drupal developer when using Docker. They allow you to synchronize a directory on your laptop or workstation with a directory in the container. Changes can be replicated in either direction -- from the container to the host OS, or from the host OS to the container. You can add a bind volume to a Compose file with just a single line of code.
In this tutorial, we'll:
- Describe how to use the
volumes
key in your Compose file - Best practices for describing mount points
- Introduce different synchronization strategies for volumes and which to use
Docker's goal is to treat containers as reusable, off-the-shelf pieces of infrastructure. Often, however, we need to tailor a container to our specific needs. We may need to enable debugging facilities, enable key configuration options, create databases, and set logins. Many development-oriented containers rely on environment variables to configure containers at runtime.
In this tutorial, we'll:
- Set environment variables using a static value in the Compose file
- Use an environment file to pass multiple variables at once
Network communication is essential when developing for a multi-tier web application like Drupal. Docker automatically isolates each container it runs, only allowing explicit ports to be exposed to the host OS. Docker Compose takes this one step further.
In this tutorial, we'll:
- Explain how Docker isolates containers
- Expose a container's ports to the host OS
- Re-map ports from the container to the host OS
A common task when developing a Drupal site is loading the database into your local development environment. When working with non-Docker local development environments, command line tools or a graphical application are used to load the database dump. These methods also work for Docker with a bit of container configuration. With Docker, you can include all the tooling in containers, reducing the need for utilities on the host OS.
In this tutorial, we'll:
- Outline the challenges for loading a database into a container
- Identify the methods by which a database can be loaded into a container
One of Docker's goals is to make it as simple to deploy and update infrastructure as it is to pull a product off of a shelf. At the center of this goal is Docker Hub, a massive, public, and free-to-use library of Docker images for you to use. As a free service, additional care is required to select images that will provide you with updated, secure, and well-maintained infrastructure.
In this tutorial, we'll:
- Show how Hub is an integral part of using Docker
- Describe registries and private registries
- Identify official images vs. contributed images
- Outline best practices for selecting an image on Hub
When we issue docker run
, Docker will attempt to download any images it doesn't have cached locally. Locally cached images take up disk space and are not automatically managed. Furthermore, once downloaded, Docker never updates them for you. You can list, update, or delete locally cached images using the docker
command.
In this tutorial, we'll:
- Show how to list all images currently stored on your host OS
- Identify the disk space occupied by cached images
- Learn to explicitly download and update a locally cached image
- Learn to delete images on your system