📜 ⬆️ ⬇️

Drupal Bundle Inherit - module for inheriting entity types

General idea


The idea of ​​developing this module was (as is often the case) dictated by the urgent need for it.

Imagine that you are organizing an online store (or any other project where you plan to create many different types of content). Suppose that you have selected a bunch of Drupal 7 + Commerce as a software package. Commerce makes it possible to create different types of products. This is useful when you are trading in different categories of goods, each of which is characterized by some of its data. For example, for monitors, you most likely want to specify the type of matrix, and for processors it would be nice to specify the clock frequency.

Now imagine a situation where you need to organize a directory with many categories of interrelated hierarchical relationships. For clarity, we present a similar structure.

Suppose for all products you want to specify the country of manufacturer, manufacturer and year of manufacture. Let's call this the base type. Suppose you sell computers. All computers have common characteristics (operating system, number of usb ports, etc.). You sell laptops, desktops and tablets. All of them have the characteristics of computers and each has characteristics that are characteristic of them only (for example, battery life for laptops).
')
Thus, the following scheme is obtained.
  1. Base type
  2. Computers (inherit base type)
  3. Laptops, PCs, Tablets (inherit type "computers")

What gives the module


The Bundle Inherit module allows you to inherit it from one of the existing types of the same entity type when creating a new content type (entity bundles). In this case, all the fields available in the parent type are automatically attached to the newly created type.

Further more interesting. The module supports two types of inheritance: soft (soft) and strict (strict).

In the case of soft inheritance, the process will be as follows. When creating an entity type, fields of the selected parent type will simply be attached to it. At this point, the module will finish its work and will not track these types in any way.

In the case of strict inheritance, the following occurs. As in the case of soft inheritance, all fields of the parent type are attached to the new type. In this case, instances of inherited fields in the new type are blocked and become inaccessible for editing and deletion. Then the module starts tracking the parent type. The module responds to 3 situations:
  1. When creating a new field, an instance of this field is automatically attached to all child types (types inherited from the parent).
  2. When you change an instance of a field in the parent type, all changes are automatically transferred to the inherited instances.
  3. When you delete an instance of a field from its parent type, in all child types, the field is unlocked and becomes available for editing and deletion. In the future, it is planned to provide the user with the opportunity to agree or refuse to automatically delete fields and all associated data from child types.

How is the module organized?


A module is actually a set of two modules.

The first (module bundle_inherit) provides an API for developers of modules and does not provide any functionality for the end user.

The second (module bundle_inherit_node) is an example of using the Bundle Inherit API. Consisting of only 40 lines of code, including comments, the module extends the node system module allowing you to inherit content types.

Use of the module


From the end user's point of view

When creating a new content type, find the Inherit tab (Inheritance), tick the corresponding item and select the parent type. This is it.

From a developer’s point of view

See the implementation using the example of the bundle_inherit_node module.
All you need to add the inheritance feature to an arbitrary type of entity is to find the form for creating a new type and use the function bundle_inherit_attach_inherit_form on it. Also, do not forget to add your submit callback to it and call the function bundle_inherit_attach_inherit_form_submit in it. And what is called "that is the magic".

Perspectives


They are. It is planned to extend the functionality of the Bundle Inherit API to allow the use of inheritance logic not only to fields but also to integrate with the internal logic of different types of entities.
Another possible direction for expanding a module is the ability to create abstract types of entities. Types that can be used for inheritance only.

Where to get


Drupal.org: drupal.org/project/bundle_inherit
GitHub: github.com/numesmat/Drupal-Bundle-Inherit

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


All Articles