Newer
Older
Angie Byron
committed
<?php
namespace Drupal\config_test\Entity;
Angie Byron
committed
use Drupal\Core\Config\Entity\ConfigEntityBase;
Alex Pott
committed
use Drupal\config_test\ConfigTestInterface;
Alex Pott
committed
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
Angie Byron
committed
/**
* Defines the ConfigTest configuration entity.
*
Alex Pott
committed
* @ConfigEntityType(
Angie Byron
committed
* id = "config_test",
* label = @Translation("Test configuration"),
Alex Pott
committed
* handlers = {
catch
committed
* "storage" = "Drupal\config_test\ConfigTestStorage",
* "list_builder" = "Drupal\config_test\ConfigTestListBuilder",
Alex Pott
committed
* "default" = "Drupal\config_test\ConfigTestForm",
Alex Pott
committed
* "delete" = "Drupal\Core\Entity\EntityDeleteForm"
* "access" = "Drupal\config_test\ConfigTestAccessControlHandler"
Angie Byron
committed
* },
* config_prefix = "dynamic",
Angie Byron
committed
* entity_keys = {
* "id" = "id",
* "label" = "label",
* "status" = "status"
Alex Pott
committed
* },
* links = {
* "edit-form" = "/admin/structure/config_test/manage/{config_test}",
* "delete-form" = "/admin/structure/config_test/manage/{config_test}/delete",
* "enable" = "/admin/structure/config_test/manage/{config_test}/enable",
Alex Pott
committed
* "disable" = "/admin/structure/config_test/manage/{config_test}/disable",
* "collection" = "/admin/structure/config_test",
Angie Byron
committed
* }
* )
*/
Alex Pott
committed
class ConfigTest extends ConfigEntityBase implements ConfigTestInterface {
Angie Byron
committed
/**
* The machine name for the configuration entity.
*
* @var string
*/
protected $id;
Angie Byron
committed
/**
* The human-readable name of the configuration entity.
*
* @var string
*/
public $label;
Alex Pott
committed
/**
* The weight of the configuration entity.
*
* @var int
*/
public $weight = 0;
Angie Byron
committed
/**
* The image style to use.
*
* @var string
*/
public $style;
Angie Byron
committed
/**
* A protected property of the configuration entity.
*
* @var string
*/
protected $protected_property;
/**
* {@inheritdoc}
*/
Alex Pott
committed
public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
\Drupal::state()->set('config_entity_sort', TRUE);
return parent::sort($a, $b);
}
/**
* {@inheritdoc}
*/
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
// Used to test secondary writes during config sync.
if ($this->id() == 'primary') {
$secondary = $storage->create([
'id' => 'secondary',
'label' => 'Secondary Default',
]);
$secondary->save();
}
if ($this->id() == 'deleter') {
$deletee = $storage->load('deletee');
$deletee->delete();
}
}
/**
* {@inheritdoc}
*/
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
foreach ($entities as $entity) {
if ($entity->id() == 'deleter') {
$deletee = $storage->load('deletee');
$deletee->delete();
}
}
}
/**
* {@inheritdoc}
*/
public function onDependencyRemoval(array $dependencies) {
// Record which entities have this method called on and what dependencies
// are passed.
$called = \Drupal::state()->get('config_test.on_dependency_removal_called', []);
$called[$this->id()] = $dependencies;
$called[$this->id()]['config'] = array_keys($called[$this->id()]['config']);
$called[$this->id()]['content'] = array_keys($called[$this->id()]['content']);
\Drupal::state()->set('config_test.on_dependency_removal_called', $called);
catch
committed
$changed = parent::onDependencyRemoval($dependencies);
if (!isset($this->dependencies['enforced']['config'])) {
return $changed;
}
$fix_deps = \Drupal::state()->get('config_test.fix_dependencies', []);
foreach ($dependencies['config'] as $entity) {
if (in_array($entity->getConfigDependencyName(), $fix_deps)) {
$key = array_search($entity->getConfigDependencyName(), $this->dependencies['enforced']['config']);
if ($key !== FALSE) {
$changed = TRUE;
unset($this->dependencies['enforced']['config'][$key]);
}
}
}
// If any of the dependencies removed still exists, return FALSE.
if (array_intersect_key(array_flip($this->dependencies['enforced']['config']), $dependencies['config'])) {
return FALSE;
}
catch
committed
return $changed;
}
catch
committed
/**
* Sets the enforced dependencies.
*
* @param array $dependencies
* A config dependency array.
*
* @return $this
*
* @see \Drupal\Core\Config\Entity\ConfigDependencyManager
*/
public function setEnforcedDependencies(array $dependencies) {
$this->dependencies['enforced'] = $dependencies;
return $this;
}
/**
* {@inheritdoc}
*/
public function isInstallable() {
return $this->id != 'isinstallable' || \Drupal::state()->get('config_test.isinstallable');
}