你在这里

控制器/处理程序*(翻译中)

原文地址: https://www.drupal.org/node/2143497

原文版本: November 25, 2013. 

(原文未成)

 

 

* Final name not yet defined, see #1976158: Rename entity storage/list/form/render "controllers" to handlers

While entities represent a piece of data, controllers are responsible for acting on and with them. The basic concept was introduced in Drupal 7, where the controller provided the API to load entities. That class was then extended in the entity.module to provide full CRUD support and other types of controllers were created.

Those controllers have been included and extend in Drupal 8. Controllers of an entity can be accessed through the entity_manager service:

Storage

The main controller is storage, which has to implement EntityStorageControllerInterface, with the following default controllers:

  • \Drupal\Core\Entity\FieldableDatabaseStorageController
    The main storage controller for content entities, supports revisions, translations and configurable fields.
  • \Drupal\Core\Config\Entity\ConfigStorageController
    The main storage controller for configuration entities.
  • \Drupal\Core\Entity\FieldableDatabaseStorageController
    Alternative storage controller for simple non-content entities. Currently only used temporarly for menu link entities, might not remain in Core

Example usage:

<?php
$storage
= \Drupal::entityManager()->getStorageController('node');
$storage->load(1);
$storage->loadMultiple(array(1, 2, 3));
// Equaivalent to $node->save().
$storage->save($node);
$new_node = $storage->create(array('title' => 'My awesome node'));
?>