📜 ⬆️ ⬇️

Development for Drupal 7 using the new entity concept (Entity)

The concept of entities (Entity), which will be considered in this article is one of the new products presented in Drupal 7. In order to realize the novelty of the proposed approach, you should take a brief history and recall how everything was in Drupal 6.

What is, what to eat?


All modules written under Drupal can be divided into two categories. The first is modules that do not actually declare new data types and work with data already somewhere defined and stored. For example, the lightbox2 module allows you to change the presentation of images on the site, and the devel module provides various utilities that are useful to the developer. And although the devel module stores some information in the database (execution time of sql queries, for example), in fact it cannot be called a full-fledged data model.
The second category is the modules that allow you to create new types of objects, define new data models. Such modules, for example, include the webform module (allows you to create survey forms), as well as the user module (create (register) new users entering the kernel, perform various operations with them).

When you, as a developer, have the module need to declare a new data type, you have two options. The first option is to define your data type as a new material type (node ​​type). The second option is to create everything from scratch. Both approaches have their advantages and disadvantages. It is clear that the second option is more flexible and does not impose on you virtually any restrictions in implementation. The first option is more convenient and faster in execution due to the ready tools provided by the Node API.

However, not only because of the speed and convenience of development, most of the serious modules created for Drupal 6 and implementing some new data model, declare it as a node type. An important feature is that all materials, regardless of type, have a certain general structure and a general scheme of external interaction. This approach allows you to write a single module to extend the functionality of all types of material existing in the system. In fact, you may not even be aware of the existence of a certain type of material and at the same time create a module that will influence it and be able to operate with it. The best examples are the CCK and Views modules.
')
It would seem that everything is fine, but one problem remains. As mentioned earlier, the implementation through the node type has some limitations that, in some cases, make it inconvenient or impossible to use this approach. In particular, it’s hard to imagine that users or comments in Drupal 6 are implemented as content types. Of course, here, for as long as you like, you can make controversies about the possibility or impossibility of realizing users in the form of types of materials, but this approach cannot be called elegant or correct.

What has changed with the arrival of Drupal 7? In fact, a new level of abstraction was created. And his name is Entity. In fact, Entity is a lower level of abstraction than a node in Drupal 6. Immediately, I’ll make a reservation that in Drupal 7 the nodes have not disappeared anywhere, however, now they are a superstructure above Entity. The most beautiful thing is that users, comments and more in Drupal 7 are also Entity. You can already make a difference out of the box. For example, earlier, using the CCK module, you could add new fields only to material types. In Drupal 7, you can do the same with users, comments, and even taxonomy tags. That's because tags, content, comments, and users are all subclasses of Entity.

Autopsy will show


Before you start a discussion on the internal structure of the entity system, you should be reminded of such a wonderful resource as api.drupal.org , where you can always familiarize yourself with all the interfaces, classes and methods discussed further in the language of Shakespeare.

So, it's time to see how it all works. Despite the fact that (as will be seen later), Object Oriented Programming has become much more active in using Drupal 7, in order to inform the system that our module implements a new entity, we will need to implement a hook. The hook we are interested in is hook_entity_info () . From it we must return an associative array, the keys of which will be the names of the entities that we will implement. In turn, each entity will correspond to an array of its parameters. Below is a list of parameters that we can specify:

That's all the parameters that can be specified for an entity type. It should be noted that add-ins over DrupalDefaultEntityController, such as, for example, EntityAPIController may require \ use additional parameters. EntityAPIController for example uses the 'entity class' parameter. This possibility is achieved through the function entity_get_info ($ entity_type = NULL), which allows you to get data about the desired type of entity.

Entity controller

Now let's take a closer look at the entity controller, which is defined in the 'controller class' parameter. In essence, a controller is a way to manage the data of your entity. What I like about OOP is that it allows not only to make the code itself more efficient, but also makes it easier to understand the processes inside it. In order to understand how the controller works, we will start the review from the very bottom. By definition, in the 'controller class' parameter a class should be specified that implements the DrupalEntityControllerInterface interface. And despite the apparent complexity of the concept of entities, this interface is surprisingly minimalist. It assumes that only three methods are implemented in our class:
DrupalEntityControllerInterface :: load - loads one or more entities. As the first argument takes an array of ID entities that you want to load. As a second argument, it takes a certain array of conditions in the form of a 'field' => 'value'.
DrupalEntityControllerInterface :: resetCache - clears (resets) the static cache for all entities (if the argument is not specified) or for the selected set of entities (if the corresponding id array is passed as an argument)
DrupalEntityControllerInterface :: __ construct - Constructor. As the only argument takes the type of entity for which you want to create an instance of the controller. Thus, knowing the type of entity for which the controller is created, you can get the parameters of this type using entity_get_info ($ entity_type) and customize the controller instance that is created.

That's all the methods that should be implemented in your class, so that it has every right to be called an entity controller. However, in most cases it makes sense not to implement the interface from scratch, but to inherit your controller class from the DrupalDefaultEntityController class , which we will now consider.
What gives us DrupalDefaultEntityController?
  1. Ability to load any entity, regardless of type, from database tables specified in 'base table' and 'revision table' parameters of entity type.
  2. Attaches fields to our entity using the Field API if the fieldable parameter is set to TRUE.
  3. Ability to load entities efficiently. The class supports static caching. (Although in essence, the resetCache method of the DrupalEntityControllerInterface hints that static caching should be implemented in any entity controller).

Thus, DrupalDefaultEntityController does not set as the purpose implementation of such functionality for the end user, it only gives some point of reference for the developer. Moreover, in those cases where your entity must somehow implement the CRUD (Create Read Update Delete) concept, it makes sense to inherit your controller from an even higher-level class - EntityAPIController, presented in the Entity API module. However, a review of how to work with the Entity API is beyond the scope of this article and will be consecrated another time.

Source: https://habr.com/ru/post/129543/


All Articles