Drupal is a registered trademark of Dries Buytaert
drupal 11.3.7 Update released for Drupal core (11.3.7)! drupal 11.2.11 Update released for Drupal core (11.2.11)! drupal 10.6.7 Update released for Drupal core (10.6.7)! drupal 10.5.9 Update released for Drupal core (10.5.9)! cms 2.1.1 Update released for Drupal core (2.1.1)! drupal 11.3.6 Update released for Drupal core (11.3.6)! drupal 10.6.6 Update released for Drupal core (10.6.6)! cms 2.1.0 Update released for Drupal core (2.1.0)! bootstrap 8.x-3.40 Minor update available for theme bootstrap (8.x-3.40). menu_link_attributes 8.x-1.7 Minor update available for module menu_link_attributes (8.x-1.7). eca 3.1.1 Minor update available for module eca (3.1.1). layout_paragraphs 2.1.3 Minor update available for module layout_paragraphs (2.1.3). ai 1.3.3 Minor update available for module ai (1.3.3). ai 1.2.14 Minor update available for module ai (1.2.14). node_revision_delete 2.0.3 Minor update available for module node_revision_delete (2.0.3). moderated_content_bulk_publish 2.0.52 Minor update available for module moderated_content_bulk_publish (2.0.52). klaro 3.0.10 Minor update available for module klaro (3.0.10). klaro 3.0.9 Minor update available for module klaro (3.0.9). layout_paragraphs 2.1.2 Minor update available for module layout_paragraphs (2.1.2). geofield_map 11.1.8 Minor update available for module geofield_map (11.1.8).

entity_model

74 sites Security covered
View on drupal.org

Adds annotation-based entity type and bundle models for entities.

Why?

Improve the developer experience of the entity bundle classes functionality
by making it possible to register them through annotations and by providing field and translation related helper functions

How does it work?

Creating models

Models are Drupal plugins with the @Model annotation, extending their entity type class. Classes with this annotation should be placed in the Entity namespace of your module.

The annotation two required parameters:

  • if only entity_type is provided, the entity type class is overridden using hook_entity_type_alter
  • if both entity_type and bundle are provided, the bundle class is overridden using hook_entity_bundle_info_alter.
<?php

namespace Drupal\mymodule\Entity\Node;

use Drupal\node\Entity\Node;

/**
 * @Model(
 *     entity_type = "node",
 *     bundle = "page"
 * )
 */
class Page extends Node
{
}

To make sure bundles are mapped to the right classes, you can use the entity_model:list Drush command.

> drush entity_model:list
 Model "media.image" is not mapped.
 Model "node.page" is mapped against "Drupal\mymodule\Entity\Node\Page".

Controller resolving

If a controller is handling a route with entity parameters, the models can be automatically injected in the arguments of the controller method by using the right type hint. If the route has two parameters of the same type, matching is done based on the parameter/argument name.

<?php

namespace Drupal\mymodule\Controller\Node;

use Drupal\mymodule\Entity\Node\Page;
use Drupal\wmcontroller\Controller\ControllerBase;

class PageController extends ControllerBase
{
    public function show(Page $page)
    {
        return $this->view(
            'node.page.detail',
            ['page' => $page]
        );
    }
}

Injecting the user entity

This module provides an alternative implementation of
Drupal\Core\Session\AccountProxyInterface (returned by the
current_user service) that makes the getAccount method return the actual User entity instead of an instance of Drupal\Core\Session\UserSession.

A Drupal core issue about this subject can be found here.

To enable this feature, set override_account_proxy to true in the entity_model.settings config.

Resolving FormStateInterface arguments

Drupal core only resolves form state arguments if the argument name matches form_state. This module provides the option to use the entity argument resolving logic as explained above for FormStateInterface arguments as well.

A Drupal core issue about this subject can be found here.

To enable this feature, set resolve_form_state_argument_type to true in the entity_model.settings config.

Contributing

  • Tests are encouraged.
  • Keep the documentation up to date. Make sure README.md and other relevant documentation is kept up to date with your changes.
  • One pull request per feature. Try to keep your changes focused on solving a single problem. This will make it easier for us to review the change and easier for you to make sure you have updated the necessary tests and documentation.

Activity

Total releases
1
First release
Apr 2025
Latest release
1 year ago
Release cadence
Stability
100% stable

Releases

Version Type Release date
3.3.0 Stable Apr 16, 2025