ConfigTest.php 3.14 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\config_test\Entity\ConfigTest.
6 7
 */

8
namespace Drupal\config_test\Entity;
9 10

use Drupal\Core\Config\Entity\ConfigEntityBase;
11
use Drupal\config_test\ConfigTestInterface;
12
use Drupal\Core\Config\Entity\ConfigEntityInterface;
13
use Drupal\Core\Entity\EntityStorageInterface;
14 15 16 17

/**
 * Defines the ConfigTest configuration entity.
 *
18
 * @ConfigEntityType(
19 20
 *   id = "config_test",
 *   label = @Translation("Test configuration"),
21
 *   controllers = {
22
 *     "storage" = "Drupal\config_test\ConfigTestStorage",
23
 *     "list_builder" = "Drupal\config_test\ConfigTestListBuilder",
24
 *     "form" = {
25
 *       "default" = "Drupal\config_test\ConfigTestForm",
26
 *       "delete" = "Drupal\config_test\Form\ConfigTestDeleteForm"
27 28
 *     },
 *     "access" = "Drupal\config_test\ConfigTestAccessController"
29
 *   },
30
 *   config_prefix = "dynamic",
31 32 33
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "label",
34
 *     "status" = "status"
35 36
 *   },
 *   links = {
37 38 39 40
 *     "edit-form" = "config_test.entity",
 *     "delete-form" = "config_test.entity_delete",
 *     "enable" = "config_test.entity_enable",
 *     "disable" = "config_test.entity_disable"
41 42 43
 *   }
 * )
 */
44
class ConfigTest extends ConfigEntityBase implements ConfigTestInterface {
45 46 47 48 49 50

  /**
   * The machine name for the configuration entity.
   *
   * @var string
   */
51
  protected $id;
52 53 54 55 56 57 58 59

  /**
   * The human-readable name of the configuration entity.
   *
   * @var string
   */
  public $label;

60 61 62 63 64 65 66
  /**
   * The weight of the configuration entity.
   *
   * @var int
   */
  public $weight = 0;

67 68 69 70 71 72 73
  /**
   * The image style to use.
   *
   * @var string
   */
  public $style;

74 75 76 77 78 79 80
  /**
   * Test dependencies.
   *
   * @var array;
   */
  public $test_dependencies = array();

81 82 83 84 85 86 87
  /**
   * A protected property of the configuration entity.
   *
   * @var string
   */
  protected $protected_property;

88 89 90
  /**
   * Overrides \Drupal\Core\Config\Entity\ConfigEntityBase::sort().
   */
91
  public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
92
    \Drupal::state()->set('config_entity_sort', TRUE);
93 94 95
    return parent::sort($a, $b);
  }

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  /**
   * {@inheritdoc}
   */
  public function postSave(EntityStorageInterface $storage, $update = TRUE) {
    // Used to test secondary writes during config sync.
    if ($this->id() == 'primary') {
      $secondary = $storage->create(array(
        '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();
      }
    }
  }

127 128 129 130 131 132 133 134 135 136 137 138
  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    parent::calculateDependencies();
    foreach ($this->test_dependencies as $type => $deps) {
      foreach ($deps as $dep) {
        $this->addDependency($type, $dep);
      }
    }
  }

139
}