OOP in Drupal

Once you've learned object oriented concepts, you're ready to approach a big project like Drupal. This class will explain the OOP concepts that are used in the codebase.

Controllers

A controller is a class that assembles a response for a page request or another function call. It serves as a wrapper for complex underlying functionality to provide a clear endpoint and single point of entry to the logic. You may have heard of the model view controller (MVC) pattern. Model is the storage, View is the display, and Controller is the logic for putting it together. The controller should utilize functions of the underlying classes, keeping reusable logic in the class, and process logic in the controller.

Services

A service is a dynamically loaded class that includes necessary dependencies to perform a process from somewhere in the code. For example, you may want to load the content in a node, so you would invoke the entity.manager service in order to have access to it's functions. Using it to load a node by id will call all kinds of processes to access the database and parse the request. In the code you're writing, the service will simply return what you want, and you don't have to think about how it works.

Tip: On drupal api pages, if a class is used as a service, it's service name will be in a dropdown on the page. If you have a service name, you can look up the class by looking in the services file.

Plugins

A plugin is a class that is used to dynamically extend or modify something.