Commit 4db19864 authored by Dries's avatar Dries
Browse files

Issue #1849480 by tim.plunkett, sdboyer: Fixed Allow ConfigEntity classes to...

Issue #1849480 by tim.plunkett, sdboyer: Fixed Allow ConfigEntity classes to control how they are saved.
parent 5956f8c4
......@@ -110,4 +110,20 @@ public static function sort($a, $b) {
}
return ($a_weight < $b_weight) ? -1 : 1;
}
/**
* Overrides \Drupal\Core\Entity\Entity::getExportProperties().
*/
public function getExportProperties() {
// Configuration objects do not have a schema. Extract all key names from
// class properties.
$class_info = new \ReflectionClass($this);
$properties = array();
foreach ($class_info->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
$name = $property->getName();
$properties[$name] = $this->get($name);
}
return $properties;
}
}
......@@ -299,7 +299,7 @@ public function save(EntityInterface $entity) {
$this->invokeHook('presave', $entity);
// Retrieve the desired properties and set them in config.
foreach ($this->getProperties($entity) as $key => $value) {
foreach ($entity->getExportProperties() as $key => $value) {
$config->set($key, $value);
}
......@@ -335,29 +335,6 @@ public function save(EntityInterface $entity) {
return $return;
}
/**
* Retrieves the exportable properties of an entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity being saved.
*
* @return array
* An array of exportable properties and their values.
*
* @see \Drupal\Core\Config\Entity\ConfigStorageController::save()
*/
protected function getProperties(EntityInterface $entity) {
// Configuration objects do not have a schema. Extract all key names from
// class properties.
$class_info = new \ReflectionClass($entity);
$properties = array();
foreach ($class_info->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
$name = $property->getName();
$properties[$name] = $entity->$name;
}
return $properties;
}
/**
* Acts on an entity before the presave hook is invoked.
*
......
......@@ -371,4 +371,12 @@ public function isDefaultRevision($new_value = NULL) {
}
return $return;
}
/**
* Implements Drupal\Core\Entity\EntityInterface::getExportProperties().
*/
public function getExportProperties() {
return array();
}
}
......@@ -181,4 +181,13 @@ public function getRevisionId();
* $new_value was passed, the previous value is returned.
*/
public function isDefaultRevision($new_value = NULL);
/**
* Retrieves the exportable properties of the entity.
*
* @return array
* An array of exportable properties and their values.
*/
public function getExportProperties();
}
......@@ -371,4 +371,29 @@ public function getPaths() {
return array_unique($all_paths);
}
/**
* Overrides \Drupal\Core\Config\Entity\ConfigEntityBase::getExportProperties();
*/
public function getExportProperties() {
$names = array(
'api_version',
'base_field',
'base_table',
'core',
'description',
'disabled',
'display',
'human_name',
'module',
'name',
'tag',
'uuid',
);
$properties = array();
foreach ($names as $name) {
$properties[$name] = $this->get($name);
}
return $properties;
}
}
......@@ -99,29 +99,4 @@ protected function attachDisplays(EntityInterface $entity) {
}
}
/**
* Overrides Drupal\config\ConfigStorageController::getProperties();
*/
protected function getProperties(EntityInterface $entity) {
$names = array(
'api_version',
'base_field',
'base_table',
'core',
'description',
'disabled',
'display',
'human_name',
'module',
'name',
'tag',
'uuid',
);
$properties = array();
foreach ($names as $name) {
$properties[$name] = $entity->get($name);
}
return $properties;
}
}
......@@ -968,6 +968,13 @@ public function enforceIsNew($value = TRUE) {
return $this->__call(__FUNCTION__, func_get_args());
}
/**
* Implements \Drupal\Core\Entity\EntityInterface::getExportProperties().
*/
public function getExportProperties() {
return $this->__call(__FUNCTION__, func_get_args());
}
/**
* Implements \Drupal\Core\TypedData\TranslatableInterface::getTranslation().
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment