Skip to main content
drupalreleases

Use direct release data with your AI assistant.

Learn more

Drupal entity expand

Security covered Under active development 3 sites

Part of the Entity ecosystem · 310 projects

This module provides a framework for extending Drupal entities with custom functionality. It allows developers to create custom classes that can add new methods and behaviors to existing entity types, enabling more object-oriented ways to interact with and manipulate entity data.

This module provides an Entity extension model. You need to write your extension function

I think that many functions should belong to entity instead of a single function to deal with

Features

Extend the original function of Entity

HOW TO USE

---------------------

  1. create your expand file: src/EntityExpand/UserExpand.php
    <?php
    namespace Drupal\my_module\EntityExpand;
    
    use Drupal\entity_expand\EntityExpandBase;
    class UserExpand extends EntityExpandBase {
      public function a() {}
      public function b() {}
      public function c() {}
      public function newname() {
         return $this->entity->name->value . 'newname';
      }
    }
    
    
  2. define a hook import this class hook_entity_expand_load.
  3. function mymodule_entity_expand_load($entity, $entity_type_id) {
      if ($entity_type_id == 'user') {
        $entity = new \Drupal\my_module\EntityExpand\UserExpand($entity);
      }
      // more....
      if ($entity_type_id == 'taxonomy_term') {
        $entity = new \Drupal\my_module\EntityExpand\TaxonomyTermExpand($entity);
      }
      return $entity;
    }
    
  4. use this expand.
    //$user = entity_expand_load(\Drupal\user\Entity\User::load(1));
    $user = _entity_load('user', 1);
    $user->name->value // original method.
    $user->a();    // get expand entity method.
    $user->b();
    $user->c();
    $user->newname();
    $user->field_ref_user->ref()->a();  // get embed entity method.
    $user->field_ref_user->ref()->b();
    $user->field_ref_user->ref()->c();
    
    // default expand method.
    // set multiple values
    $user->setValues([
      'name' => 'new name',
      'field_a' => 'a value',
      'field_b' => 'b value',
    ]);
    $user->field_tags->refs(function($entity) {
      print_r($entity->label());
    });
    $user->field_category->listTextLabel();  // display field value label.
    render($user->view('full'));       // entity view.
    $user->field_a->val('no value');  // get field value and set default value.
    $user->field_ref_value->targets();  // get multiple target_ids.
    
Drupal 8–11 PHP ^7 || ^8.0

Ask your assistant about Drupal entity expand

Check compatibility with your Drupal and PHP version, maintenance and security coverage, from current release data. How it works

Depends on

Dependencies of the latest stable release

No dependencies recorded for this project.

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
5
Tracked since
Jun 2023
Latest release
2 years ago
Releases (12 mo)
0
Maintenance
Dormant

Release Timeline

Releases

Version Type Core PHP Release date
1.0.4 Stable 8–11 PHP ^7 || ^8.0 Aug 23, 2024
1.0.3 Stable 8–11 Aug 23, 2024
1.0.2 Stable 8–11 Aug 23, 2024
1.0.x-dev Dev 8–11 PHP ^7 || ^8.0 Jun 23, 2023
1.0.1 Stable 8–10 Jun 23, 2023