U
nderstand of anything needs to understand the underlying concepts first.
In this section we will describe the most important and useful concepts that will drive and thrive you further with Drupal development. We will try to keep it simple and succinct so that you take much in less time.
1. Entity types
- An Entity Type are used for persistence storage
- An instance of an Entity Type is called entity ( Just like instance of class is called object)
- Entity Type may have subtypes called bundles ( Like Parent, Child class in oop)
- Node (Entity Type) has subtypes(bundles) called Content Types: Article, Basic page etc
- User (Entity Type) has only one subtype/bundle i.e user
2. Information types
Data: A collection of facts and figures in raw format is called data. When we do some processing (sorting, graphs, filtering) the data becomes meaningful and decision making and hence called information. We usually deal with information in our applications.
Drupal has several distinct types of information, each with its own methods for storage and retrieval:
- Content: Information meant to be displayed on your site: articles, basic pages, images, files, custom blocks, etc. Content is stored and accessed using Entities.
- Session: Information about individual users' interactions with the site, such as whether they are logged in. This is really "state" information, but it is not stored the same way so it's a separate type here. Session data is accessed via \Symfony\Component\HttpFoundation\Request::getSession(), which returns an instance of \Symfony\Component\HttpFoundation\Session\SessionInterface. See the Sessions topic for more information.
- State: Information of a temporary nature, generally machine-generated and not human-edited, about the current state of your site. Examples: the time when Cron was last run, whether node access permissions need rebuilding, etc. See the State API topic for more information.
- Configuration: Information about your site that is generally (or at least can be) human-edited, but is not Content, and is meant to be relatively permanent. Examples: the name of your site, the content types and views you have defined, etc. See the Configuration API topic for more information.
3. Hooks
- Hooks allow module to alter or use the functionality of other modules
- It is a way for modules (in a modular system) to communicate hence extending
- There are two concerns: Implementing hooks and Invoking/using hooks
- A hook has three parts
- Name: The name of the hook, it follows a special pattern like MODULE_name
- Definition: It is the process of defining / creating a hook for your own module. This definition(name and arguments) will be used by other modules to use/implement your hook.
- Implementation: It is the process of using or implementing your hook by another module or in other words extending your module
- There are three types of hook
- Hooks that answer a question
- Hooks that alter existing data: e.g taxonomy_views_data_alter, hook_form_alter. Hook_form_alter is used to change almost anything in a Render array. As you see it has a special naming convention like: YourModule_OtherModule_HookName. Act is more like act on my data (other module)
- Hooks that react to an action: It is more like act on your own data (your module)
- All the three forms are live events and called at different positions of code execution. They differ by the kind of information/action they are dealing with, e.g alter hook will change data of the module, info hook takes some data but not modify it, action hook will take some action on its end.
- For example, hook_user_cancel() is invoked every time a user account is cancelled. Modules that store data about users that need to perform cleanup tasks can implement this hook to be notified about the account that is being cancelled and take the necessary actions on their own data.
- The comment module is a good example of this, see comment_user_cancel() which removes comments created by a user when their account is cancelled.
- Hooks are basically extending the functionality of one module by another very similar to class inheritance. The difference is that hooks allow extending the functionality on a higher level and between modules whereas inheritance is more granular and between classes.
4. Articles for reading
- https://www.drupal.org/docs/theming-drupal/twig-in-drupal
- https://www.drupal.org/docs/theming-drupal/twig-in-drupal/twig-best-practices-preprocess-functions-and-templates
- https://www.youtube.com/playlist?list=PLtaXuX0nEZk8NY8L4weAUJljPRG5vzT_g
- https://refactoring.guru/design-patterns/strategy/php/example#example-0