Download & Extend

Entities are now classed objects using a defined interface

Project: 
Drupal core
Introduced in branch: 
8.x
Description: 

Summary

  • Entities are now classed objects that implement the Drupal\entity\EntityInterface.
  • The default implementation is the Drupal\entity\Entity class.
  • Entity create, update, and delete functionality is now provided via the interface.
  • Users, nodes, comments, taxonomy terms and vocabularies have been converted to extend the new base class and interface.
  • Other core entity types will be converted in #1368394: Convert all core entities to classed objects.
  • The base class and interface do not yet support revisioning or language handling; support for these features will be added going forward.
  • entity_uri() and entity_label() are now deprecated and will be removed.
  • Full roadmap for entity changes: #1346204: [META] Drupal 8 Entity API improvements.

Impact on modules

Contributed and custom modules should:

  • Convert custom entity types to either use the base Drupal\entity\Entity class (for simple entity types) or to extend the Drupal\entity\Entity class with a custom class.
  • Use the common interface for CRUD operations.
  • Use the common interface for property getters and setters.
  • Replace use of entity_uri() and entity_label() with the corresponding methods provided in the interface.
  • New entities should be created with entity_create(), directly using the PHP new keyword and a specific class is discouraged.

API Changes

Added interfaces

Added and changed classes

Old name New name Extends/Implements/
- Drupal\entity\Entity Drupal\entity\EntityInterface
- Drupal\entity\EntityStorageException
- Drupal\entity\EntityStorageController
- Drupal\comment\Comment Drupal\entity\Entity
CommentController Drupal\comment\CommentStorageController Drupal\entity\EntityStorageInterface
- Drupal\taxonomy\Term Drupal\entity\Entity
- Drupal\taxonomy\Vocabulary Drupal\entity\Entity
TaxonomyTermController Drupal\taxonomy\TermStorageController Drupal\entity\EntityStorageController
TaxonomyVocabularyController Drupal\taxonomy\VocabularyStorageController Drupal\entity\EntityStorageController
- Drupal\node\Node Drupal\entity\Entity
NodeController Drupal\node\NodeStorageController Drupal\entity\EntityStorageController
- Drupal\userUser Drupal\entity\Entity
UserController Drupal\user\UserStorageController Drupal\entity\EntityStorageController

Added API functions

  • entity_delete_multiple()
  • entity_create()

Example implementation

  • Custom entity that extends the base class and adds a property:
    <?php
    namespace Drupal
    *my_module;

    use
    Drupal*entity*Entity;

    class
    MyForum extends Entity {
     
    /**
       * @var int
       */
     
    public $threadId = 0;
    }
    ?>
  • Custom storage controller that extends the controller class and overrides postSave():
    <?php
    namespace Drupal
    *my_module;

    use
    Drupal*entity*EntityDatabaseStorageController;
    use
    Drupal*entity*EntityInterface;

    class
    MyForumStorageController extends EntityDatabaseStorageController {
     
    /**
       * Overrides Drupal\entity\EntityDatabaseStorageController::postSave().
       */
     
    protected function postSave(EntityInterface $forum) {
        if (
    $forum->threadId) {
         
    module_invoke_all('forum_thread_save', $forum);
        }
      }
    }
    ?>
  • Entity usage:
    <?php
    // Create a new forum instance.
    $forum = entity_create('myforum', array('threadId' => 5));
    // Save entity to the database.
    $forum->save();
    // Do some arbitrary processing of the entity.
    myforum_process_forum_data($forum);
    // Save entity again.
    $forum->save();
    ?>
Impacts: 
Site builders, administrators, editors
Module developers
Themers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done