ConfigTest.php

Same filename in this branch
  1. 9 core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/d8/ConfigTest.php
  2. 9 core/modules/migrate/tests/src/Unit/destination/ConfigTest.php
  3. 9 core/modules/system/tests/src/Functional/File/ConfigTest.php
  4. 9 core/tests/Drupal/Tests/Composer/Plugin/ProjectMessage/ConfigTest.php
  5. 9 core/tests/Drupal/Tests/Composer/Plugin/VendorHardening/ConfigTest.php
  6. 9 core/tests/Drupal/Tests/Core/Config/ConfigTest.php
Same filename in other branches
  1. 8.9.x core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/d8/ConfigTest.php
  2. 8.9.x core/modules/config/tests/config_test/src/Entity/ConfigTest.php
  3. 8.9.x core/modules/migrate/tests/src/Unit/destination/ConfigTest.php
  4. 8.9.x core/modules/system/tests/src/Functional/File/ConfigTest.php
  5. 8.9.x core/tests/Drupal/Tests/Composer/Plugin/ProjectMessage/ConfigTest.php
  6. 8.9.x core/tests/Drupal/Tests/Composer/Plugin/VendorHardening/ConfigTest.php
  7. 8.9.x core/tests/Drupal/Tests/Core/Config/ConfigTest.php
  8. 10 core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/d8/ConfigTest.php
  9. 10 core/modules/config/tests/config_test/src/Entity/ConfigTest.php
  10. 10 core/modules/migrate/tests/src/Unit/destination/ConfigTest.php
  11. 10 core/modules/system/tests/src/Functional/File/ConfigTest.php
  12. 10 core/tests/Drupal/Tests/Composer/Plugin/ProjectMessage/ConfigTest.php
  13. 10 core/tests/Drupal/Tests/Composer/Plugin/VendorHardening/ConfigTest.php
  14. 10 core/tests/Drupal/Tests/Core/Config/ConfigTest.php
  15. 11.x core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/d8/ConfigTest.php
  16. 11.x core/modules/config/tests/config_test/src/Entity/ConfigTest.php
  17. 11.x core/modules/migrate/tests/src/Unit/destination/ConfigTest.php
  18. 11.x core/modules/system/tests/src/Functional/File/ConfigTest.php
  19. 11.x core/tests/Drupal/Tests/Composer/Plugin/ProjectMessage/ConfigTest.php
  20. 11.x core/tests/Drupal/Tests/Composer/Plugin/VendorHardening/ConfigTest.php
  21. 11.x core/tests/Drupal/Tests/Core/Config/ConfigTest.php

Namespace

Drupal\config_test\Entity

File

core/modules/config/tests/config_test/src/Entity/ConfigTest.php

View source
<?php

namespace Drupal\config_test\Entity;

use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\config_test\ConfigTestInterface;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;

/**
 * Defines the ConfigTest configuration entity.
 *
 * @ConfigEntityType(
 *   id = "config_test",
 *   label = @Translation("Test configuration"),
 *   handlers = {
 *     "storage" = "Drupal\config_test\ConfigTestStorage",
 *     "list_builder" = "Drupal\config_test\ConfigTestListBuilder",
 *     "form" = {
 *       "default" = "Drupal\config_test\ConfigTestForm",
 *       "delete" = "Drupal\Core\Entity\EntityDeleteForm"
 *     },
 *     "access" = "Drupal\config_test\ConfigTestAccessControlHandler"
 *   },
 *   config_prefix = "dynamic",
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "label",
 *     "status" = "status"
 *   },
 *   config_export = {
 *     "id",
 *     "label",
 *     "weight",
 *     "style",
 *     "size",
 *     "size_value",
 *     "protected_property",
 *   },
 *   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",
 *     "disable" = "/admin/structure/config_test/manage/{config_test}/disable",
 *     "collection" = "/admin/structure/config_test",
 *   }
 * )
 */
class ConfigTest extends ConfigEntityBase implements ConfigTestInterface {
    
    /**
     * The machine name for the configuration entity.
     *
     * @var string
     */
    protected $id;
    
    /**
     * The human-readable name of the configuration entity.
     *
     * @var string
     */
    public $label;
    
    /**
     * The weight of the configuration entity.
     *
     * @var int
     */
    public $weight = 0;
    
    /**
     * The image style to use.
     *
     * @var string
     */
    public $style;
    
    /**
     * A protected property of the configuration entity.
     *
     * @var string
     */
    protected $protected_property;
    
    /**
     * {@inheritdoc}
     */
    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 calculateDependencies() {
        parent::calculateDependencies();
        if ($module = \Drupal::state()->get('config_test_new_dependency', FALSE)) {
            $this->addDependency('module', $module);
        }
        return $this;
    }
    
    /**
     * {@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);
        $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;
        }
        return $changed;
    }
    
    /**
     * 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');
    }

}

Classes

Title Deprecated Summary
ConfigTest Defines the ConfigTest configuration entity.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.