2 namespace Drupal\pathauto;
4 use Drupal\Core\TypedData\TypedData;
7 * A property that stores in keyvalue whether an entity should receive an alias.
9 class PathautoState extends TypedData {
12 * An automatic alias should not be created.
17 * An automatic alias should be created.
29 * @var \Drupal\Core\Field\FieldItemInterface
36 public function getValue() {
37 if ($this->value === NULL) {
38 // If no value has been set or loaded yet, try to load a value if this
39 // entity has already been saved.
40 $this->value = \Drupal::keyValue($this->getCollection())
41 ->get($this->parent->getEntity()->id());
42 // If it was not yet saved or no value was found, then set the flag to
43 // create the alias if there is a matching pattern.
44 if ($this->value === NULL) {
45 $entity = $this->parent->getEntity();
46 $pattern = \Drupal::service('pathauto.generator')->getPatternByEntity($entity);
47 $this->value = !empty($pattern) ? static::CREATE : static::SKIP;
56 public function setValue($value, $notify = TRUE) {
57 $this->value = $value;
58 // Notify the parent of any changes.
59 if ($notify && isset($this->parent)) {
60 $this->parent->onChange($this->name);
65 * Returns TRUE if a value was set.
67 public function hasValue() {
68 return $this->value !== NULL;
74 public function persist() {
75 \Drupal::keyValue($this->getCollection())->set(
76 $this->parent->getEntity()
82 * Deletes the stored state.
84 public function purge() {
85 \Drupal::keyValue($this->getCollection())
86 ->delete($this->parent->getEntity()->id());
90 * Returns the key value collection that should be used for the given entity.
93 protected function getCollection() {
94 return 'pathauto_state.' . $this->parent->getEntity()->getEntityTypeId();