Commit 42d5f005 authored by webchick's avatar webchick
Browse files

Issue #1668820 by sun, xjm, tim.plunkett, fgm: Concept, base class, and...

Issue #1668820 by sun, xjm, tim.plunkett, fgm: Concept, base class, and interface for configurable objects.
parent 6d6e73a4
......@@ -3542,6 +3542,7 @@ function form_process_machine_name($element, &$form_state) {
// 'source' only) would leave all other properties undefined, if the defaults
// were defined in hook_element_info(). Therefore, we apply the defaults here.
$element['#machine_name'] += array(
// @todo Use 'label' by default.
'source' => array('name'),
'target' => '#' . $element['#id'],
'label' => t('Machine name'),
......
......@@ -354,6 +354,7 @@ public function sortByKey(array &$data) {
* Deletes the configuration object.
*/
public function delete() {
// @todo Consider to remove the pruning of data for Config::delete().
$this->data = array();
$this->storage->delete($this->name);
$this->isNew = TRUE;
......
......@@ -8,7 +8,7 @@
namespace Drupal\Core\File;
use Drupal\entity\DatabaseStorageController;
use Drupal\entity\EntityInterface;
use Drupal\entity\StorableInterface;
/**
* File storage controller for files.
......@@ -34,7 +34,7 @@ public function create(array $values) {
/**
* Overrides Drupal\entity\DatabaseStorageController::presave().
*/
protected function preSave(EntityInterface $entity) {
protected function preSave(StorableInterface $entity) {
$entity->timestamp = REQUEST_TIME;
$entity->filesize = filesize($entity->uri);
if (!isset($entity->langcode)) {
......
......@@ -7,7 +7,7 @@
namespace Drupal\comment;
use Drupal\entity\EntityInterface;
use Drupal\entity\StorableInterface;
use Drupal\entity\DatabaseStorageController;
use LogicException;
......@@ -58,7 +58,7 @@ protected function attachLoad(&$comments, $load_revision = FALSE) {
* @see comment_int_to_alphadecimal()
* @see comment_alphadecimal_to_int()
*/
protected function preSave(EntityInterface $comment) {
protected function preSave(StorableInterface $comment) {
global $user;
if (!isset($comment->status)) {
......@@ -151,7 +151,7 @@ protected function preSave(EntityInterface $comment) {
/**
* Overrides Drupal\entity\DatabaseStorageController::postSave().
*/
protected function postSave(EntityInterface $comment, $update) {
protected function postSave(StorableInterface $comment, $update) {
$this->releaseThreadLock();
// Update the {node_comment_statistics} table prior to executing the hook.
$this->updateNodeStatistics($comment->nid);
......
......@@ -30,13 +30,13 @@
* @param Drupal\Core\Config\Config $old_config
* A configuration object containing the old configuration data.
*/
function MODULE_config_import_create($name, $new_config, $old_config) {
// Only configurable thingies require custom handling. Any other module
function hook_config_import_create($name, $new_config, $old_config) {
// Only configurable entities require custom handling. Any other module
// settings can be synchronized directly.
if (strpos($name, 'config_test.dynamic.') !== 0) {
return FALSE;
}
$config_test = new ConfigTest($new_config);
$config_test = entity_create('config_test', $new_config->get());
$config_test->save();
return TRUE;
}
......@@ -59,14 +59,28 @@ function MODULE_config_import_create($name, $new_config, $old_config) {
* @param Drupal\Core\Config\Config $old_config
* A configuration object containing the old configuration data.
*/
function MODULE_config_import_change($name, $new_config, $old_config) {
// Only configurable thingies require custom handling. Any other module
function hook_config_import_change($name, $new_config, $old_config) {
// Only configurable entities require custom handling. Any other module
// settings can be synchronized directly.
if (strpos($name, 'config_test.dynamic.') !== 0) {
return FALSE;
}
$config_test = new ConfigTest($new_config);
$config_test->setOriginal($old_config);
// @todo Make this less ugly.
list($entity_type) = explode('.', $name);
$entity_info = entity_get_info($entity_type);
$id = substr($name, strlen($entity_info['config prefix']) + 1);
$config_test = entity_load('config_test', $id);
$config_test->original = clone $config_test;
foreach ($old_config->get() as $property => $value) {
$config_test->original->$property = $value;
}
foreach ($new_config->get() as $property => $value) {
$config_test->$property = $value;
}
$config_test->save();
return TRUE;
}
......@@ -89,8 +103,8 @@ function MODULE_config_import_change($name, $new_config, $old_config) {
* @param Drupal\Core\Config\Config $old_config
* A configuration object containing the old configuration data.
*/
function MODULE_config_import_delete($name, $new_config, $old_config) {
// Only configurable thingies require custom handling. Any other module
function hook_config_import_delete($name, $new_config, $old_config) {
// Only configurable entities require custom handling. Any other module
// settings can be synchronized directly.
if (strpos($name, 'config_test.dynamic.') !== 0) {
return FALSE;
......@@ -100,8 +114,10 @@ function MODULE_config_import_delete($name, $new_config, $old_config) {
// But that is impossible currently, since the config system only knows
// about deleted and added changes. Introduce an 'old_ID' key within
// config objects as a standard?
$config_test = new ConfigTest($old_config);
$config_test->delete();
list($entity_type) = explode('.', $name);
$entity_info = entity_get_info($entity_type);
$id = substr($name, strlen($entity_info['config prefix']) + 1);
config_test_delete($id);
return TRUE;
}
<?php
/**
* @file
* Definition of Drupal\config\ConfigStorageController.
*/
namespace Drupal\config;
use Drupal\Component\Uuid\Uuid;
use Drupal\entity\StorableInterface;
use Drupal\entity\StorageControllerInterface;
/**
* Defines the storage controller class for configurable entities.
*/
class ConfigStorageController implements StorageControllerInterface {
/**
* Entity type for this controller instance.
*
* @var string
*/
protected $entityType;
/**
* Array of information about the entity.
*
* @var array
*
* @see entity_get_info()
*/
protected $entityInfo;
/**
* Additional arguments to pass to hook_TYPE_load().
*
* Set before calling Drupal\config\ConfigStorageController::attachLoad().
*
* @var array
*/
protected $hookLoadArguments;
/**
* Name of the entity's ID field in the entity database table.
*
* @var string
*/
protected $idKey;
/**
* Implements Drupal\entity\StorageControllerInterface::__construct().
*
* Sets basic variables.
*/
public function __construct($entityType) {
$this->entityType = $entityType;
$this->entityInfo = entity_get_info($entityType);
$this->hookLoadArguments = array();
$this->idKey = $this->entityInfo['entity keys']['id'];
// The UUID key and property is hard-coded for all configurables.
$this->uuidKey = 'uuid';
}
/**
* Implements Drupal\entity\StorageControllerInterface::resetCache().
*/
public function resetCache(array $ids = NULL) {
// The configuration system is fast enough and/or implements its own
// (advanced) caching mechanism already.
}
/**
* Implements Drupal\entity\StorageControllerInterface::load().
*/
public function load(array $ids = NULL) {
$entities = array();
// Create a new variable which is either a prepared version of the $ids
// array for later comparison with the entity cache, or FALSE if no $ids
// were passed.
$passed_ids = !empty($ids) ? array_flip($ids) : FALSE;
// Load any remaining entities. This is the case if $ids is set to NULL (so
// we load all entities).
if ($ids === NULL || $ids) {
$queried_entities = $this->buildQuery($ids);
}
// Pass all entities loaded from the database through $this->attachLoad(),
// which calls the
// entity type specific load callback, for example hook_node_type_load().
if (!empty($queried_entities)) {
$this->attachLoad($queried_entities);
$entities += $queried_entities;
}
// Ensure that the returned array is ordered the same as the original
// $ids array if this was passed in and remove any invalid ids.
if ($passed_ids) {
// Remove any invalid ids from the array.
$passed_ids = array_intersect_key($passed_ids, $entities);
foreach ($entities as $entity) {
$passed_ids[$entity->{$this->idKey}] = $entity;
}
$entities = $passed_ids;
}
return $entities;
}
/**
* Implements Drupal\entity\StorageControllerInterface::loadRevision().
*/
public function loadRevision($revision_id) {
return FALSE;
}
/**
* Implements Drupal\entity\StorageControllerInterface::loadByProperties().
*/
public function loadByProperties(array $values = array()) {
return array();
}
/**
* Builds the query to load the entity.
*
* This has full revision support. For entities requiring special queries,
* the class can be extended, and the default query can be constructed by
* calling parent::buildQuery(). This is usually necessary when the object
* being loaded needs to be augmented with additional data from another
* table, such as loading node type into comments or vocabulary machine name
* into terms, however it can also support $conditions on different tables.
* See Drupal\comment\CommentStorageController::buildQuery() or
* Drupal\taxonomy\TermStorageController::buildQuery() for examples.
*
* @param $ids
* An array of entity IDs, or FALSE to load all entities.
* @param $revision_id
* The ID of the revision to load, or FALSE if this query is asking for the
* most current revision(s).
*
* @return SelectQuery
* A SelectQuery object for loading the entity.
*/
protected function buildQuery($ids, $revision_id = FALSE) {
$config_class = $this->entityInfo['entity class'];
$prefix = $this->entityInfo['config prefix'] . '.';
if ($ids === NULL) {
$names = drupal_container()->get('config.storage')->listAll($prefix);
$result = array();
foreach ($names as $name) {
$config = config($name);
$result[$config->get($this->idKey)] = new $config_class($config->get(), $this->entityType);
}
return $result;
}
else {
$result = array();
foreach ($ids as $id) {
$config = config($prefix . $id);
if (!$config->isNew()) {
$result[$id] = new $config_class($config->get(), $this->entityType);
}
}
return $result;
}
}
/**
* Attaches data to entities upon loading.
*
* This will attach fields, if the entity is fieldable. It calls
* hook_entity_load() for modules which need to add data to all entities.
* It also calls hook_TYPE_load() on the loaded entities. For example
* hook_node_load() or hook_user_load(). If your hook_TYPE_load()
* expects special parameters apart from the queried entities, you can set
* $this->hookLoadArguments prior to calling the method.
* See Drupal\node\NodeStorageController::attachLoad() for an example.
*
* @param $queried_entities
* Associative array of query results, keyed on the entity ID.
* @param $revision_id
* ID of the revision that was loaded, or FALSE if the most current revision
* was loaded.
*/
protected function attachLoad(&$queried_entities, $revision_id = FALSE) {
// Call hook_entity_load().
foreach (module_implements('entity_load') as $module) {
$function = $module . '_entity_load';
$function($queried_entities, $this->entityType);
}
// Call hook_TYPE_load(). The first argument for hook_TYPE_load() are
// always the queried entities, followed by additional arguments set in
// $this->hookLoadArguments.
$args = array_merge(array($queried_entities), $this->hookLoadArguments);
foreach (module_implements($this->entityType . '_load') as $module) {
call_user_func_array($module . '_' . $this->entityType . '_load', $args);
}
}
/**
* Implements Drupal\entity\StorageControllerInterface::create().
*/
public function create(array $values) {
$class = isset($this->entityInfo['entity class']) ? $this->entityInfo['entity class'] : 'Drupal\entity\Entity';
$entity = new $class($values, $this->entityType);
// Assign a new UUID if there is none yet.
if (!isset($entity->{$this->uuidKey})) {
$uuid = new Uuid();
$entity->{$this->uuidKey} = $uuid->generate();
}
return $entity;
}
/**
* Implements Drupal\entity\StorageControllerInterface::delete().
*/
public function delete($ids) {
$entities = $ids ? $this->load($ids) : FALSE;
if (!$entities) {
// If no IDs or invalid IDs were passed, do nothing.
return;
}
$this->preDelete($entities);
foreach ($entities as $id => $entity) {
$this->invokeHook('predelete', $entity);
}
foreach ($entities as $id => $entity) {
$config = config($this->entityInfo['config prefix'] . '.' . $entity->id());
$config->delete();
}
$this->postDelete($entities);
foreach ($entities as $id => $entity) {
$this->invokeHook('delete', $entity);
}
}
/**
* Implements Drupal\entity\StorageControllerInterface::save().
*/
public function save(StorableInterface $entity) {
$prefix = $this->entityInfo['config prefix'] . '.';
// Load the stored entity, if any.
if ($entity->getOriginalID()) {
$id = $entity->getOriginalID();
}
else {
$id = $entity->id();
}
$config = config($prefix . $id);
$config->setName($prefix . $entity->id());
if (!$config->isNew() && !isset($entity->original)) {
$entity->original = entity_load_unchanged($this->entityType, $id);
}
$this->preSave($entity);
$this->invokeHook('presave', $entity);
// Configuration objects do not have a schema. Extract all key names from
// class properties.
$class_info = new \ReflectionClass($entity);
foreach ($class_info->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
$name = $property->getName();
$config->set($name, $entity->$name);
}
if (!$config->isNew()) {
$return = SAVED_NEW;
$config->save();
$this->postSave($entity, TRUE);
$this->invokeHook('update', $entity);
}
else {
$return = SAVED_UPDATED;
$config->save();
$entity->enforceIsNew(FALSE);
$this->postSave($entity, FALSE);
$this->invokeHook('insert', $entity);
}
unset($entity->original);
return $return;
}
/**
* Acts on an entity before the presave hook is invoked.
*
* Used before the entity is saved and before invoking the presave hook.
*/
protected function preSave(StorableInterface $entity) {
}
/**
* Acts on a saved entity before the insert or update hook is invoked.
*
* Used after the entity is saved, but before invoking the insert or update
* hook.
*
* @param $update
* (bool) TRUE if the entity has been updated, or FALSE if it has been
* inserted.
*/
protected function postSave(StorableInterface $entity, $update) {
// Delete the original configurable entity, in case the entity ID was
// renamed.
if ($update && !empty($entity->original) && $entity->{$this->idKey} !== $entity->original->{$this->idKey}) {
// @todo This should just delete the original config object without going
// through the API, no?
$entity->original->delete();
}
}
/**
* Acts on entities before they are deleted.
*
* Used before the entities are deleted and before invoking the delete hook.
*/
protected function preDelete($entities) {
}
/**
* Acts on deleted entities before the delete hook is invoked.
*
* Used after the entities are deleted but before invoking the delete hook.
*/
protected function postDelete($entities) {
}
/**
* Invokes a hook on behalf of the entity.
*
* @param $hook
* One of 'presave', 'insert', 'update', 'predelete', or 'delete'.
* @param $entity
* The entity object.
*/
protected function invokeHook($hook, StorableInterface $entity) {
// Invoke the hook.
module_invoke_all($this->entityType . '_' . $hook, $entity);
// Invoke the respective entity-level hook.
module_invoke_all('entity_' . $hook, $entity, $this->entityType);
}
}
<?php
/**
* @file
* Definition of Drupal\config\ConfigurableBase.
*/
namespace Drupal\config;
use Drupal\entity\StorableBase;
/**
* Defines a base configurable entity class.
*/
abstract class ConfigurableBase extends StorableBase implements ConfigurableInterface {
/**
* The original ID of the configurable entity.
*
* The ID of a configurable entity is a unique string (machine name). When a
* configurable entity is updated and its machine name is renamed, the
* original ID needs to be known.
*
* @var string
*/
protected $originalID;
/**
* Overrides Entity::__construct().
*/
public function __construct(array $values = array(), $entity_type) {
parent::__construct($values, $entity_type);
// Backup the original ID, if any.
if ($original_id = $this->id()) {
$this->originalID = $original_id;
}
}
/**
* Implements ConfigurableInterface::getOriginalID().
*/
public function getOriginalID() {
return $this->originalID;
}
/**
* Overrides Entity::isNew().
*
* EntityInterface::enforceIsNew() is not supported by configurable entities,
* since each Configurable is unique.
*/
final public function isNew() {
return !$this->id();
}
/**
* Overrides Entity::bundle().
*
* EntityInterface::bundle() is not supported by configurable entities, since
* a Configurable is a bundle.
*/
final public function bundle() {
return $this->entityType;
}
/**
* Overrides Entity::get().
*
* EntityInterface::get() implements support for fieldable entities, but
* configurable entities are not fieldable.
*/
public function get($property_name, $langcode = NULL) {
// @todo: Add support for translatable properties being not fields.
return isset($this->{$property_name}) ? $this->{$property_name} : NULL;
}
/**
* Overrides Entity::set().
*
* EntityInterface::set() implements support for fieldable entities, but
* configurable entities are not fieldable.
*/
public function set($property_name, $value, $langcode = NULL) {
// @todo: Add support for translatable properties being not fields.
$this->{$property_name} = $value;
}
/**
* Helper callback for uasort() to sort Configurable entities by weight and label.
*/
public static function sort($a, $b) {
$a_weight = isset($a->weight) ? $a->weight : 0;
$b_weight = isset($b->weight) ? $b->weight : 0;
if ($a_weight == $b_weight) {
$a_label = $a->label();
$b_label = $b->label();
return strnatcasecmp($a_label, $b_label);
}
return ($a_weight < $b_weight) ? -1 : 1;
}
}