PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/web/modules/contrib/pathauto/src/PathautoState.php

https://gitlab.com/mohamed_hussein/prodt
PHP | 186 lines | 77 code | 22 blank | 87 comment | 12 complexity | 143959bfd23235d349e5863a31b39a9b MD5 | raw file
  1. <?php
  2. namespace Drupal\pathauto;
  3. use Drupal\Component\Utility\Crypt;
  4. use Drupal\Core\TypedData\TypedData;
  5. /**
  6. * A property that stores in keyvalue whether an entity should receive an alias.
  7. */
  8. class PathautoState extends TypedData {
  9. /**
  10. * An automatic alias should not be created.
  11. */
  12. const SKIP = 0;
  13. /**
  14. * An automatic alias should be created.
  15. */
  16. const CREATE = 1;
  17. /**
  18. * Pathauto state.
  19. *
  20. * @var int
  21. */
  22. protected $value;
  23. /**
  24. * @var \Drupal\Core\Field\FieldItemInterface
  25. */
  26. protected $parent;
  27. /**
  28. * Pathauto State Original value.
  29. *
  30. * @var int
  31. */
  32. protected $originalValue;
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function getValue() {
  37. if ($this->value === NULL) {
  38. $this->value = $this->getOriginalValue();
  39. // If it was not yet saved or no value was found, then set the flag to
  40. // create the alias if there is a matching pattern.
  41. if ($this->value === NULL) {
  42. $entity = $this->parent->getEntity();
  43. $pattern = \Drupal::service('pathauto.generator')->getPatternByEntity($entity);
  44. $this->value = !empty($pattern) ? static::CREATE : static::SKIP;
  45. }
  46. }
  47. return $this->value;
  48. }
  49. /**
  50. * Gets the data value currently stored in database.
  51. *
  52. * @return mixed
  53. * The data value.
  54. */
  55. protected function getOriginalValue() {
  56. if ($this->originalValue === NULL) {
  57. // If no value has been set or loaded yet, try to load a value if this
  58. // entity has already been saved.
  59. $this->originalValue = \Drupal::keyValue($this->getCollection())
  60. ->get(static::getPathautoStateKey($this->parent->getEntity()->id()));
  61. }
  62. return $this->originalValue;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function setValue($value, $notify = TRUE) {
  68. $this->value = $value;
  69. // Notify the parent of any changes.
  70. if ($notify && isset($this->parent)) {
  71. $this->parent->onChange($this->name);
  72. }
  73. }
  74. /**
  75. * Returns TRUE if a value was set.
  76. */
  77. public function hasValue() {
  78. return $this->value !== NULL;
  79. }
  80. /**
  81. * Persists the state.
  82. */
  83. public function persist() {
  84. // Do nothing if current value is same as original value.
  85. if ($this->getValue() === $this->getOriginalValue()) {
  86. return;
  87. }
  88. \Drupal::keyValue($this->getCollection())
  89. ->set(static::getPathautoStateKey($this->parent->getEntity()->id()), $this->getValue());
  90. }
  91. /**
  92. * Deletes the stored state.
  93. */
  94. public function purge() {
  95. \Drupal::keyValue($this->getCollection())
  96. ->delete(static::getPathautoStateKey($this->parent->getEntity()->id()));
  97. }
  98. /**
  99. * Returns the key value collection that should be used for the given entity.
  100. *
  101. * @return string
  102. */
  103. protected function getCollection() {
  104. return 'pathauto_state.' . $this->parent->getEntity()->getEntityTypeId();
  105. }
  106. /**
  107. * Deletes the URL aliases for multiple entities of the same type.
  108. *
  109. * @param string $entity_type_id
  110. * The entity type ID of entities being deleted.
  111. * @param int[] $pids_by_id
  112. * A list of path IDs keyed by entity ID.
  113. */
  114. public static function bulkDelete($entity_type_id, array $pids_by_id) {
  115. foreach ($pids_by_id as $id => $pid) {
  116. // Some key-values store entries have computed keys.
  117. $key = static::getPathautoStateKey($id);
  118. if ($key !== $id) {
  119. $pids_by_id[$key] = $pid;
  120. unset($pids_by_id[$id]);
  121. }
  122. }
  123. $states = \Drupal::keyValue("pathauto_state.$entity_type_id")
  124. ->getMultiple(array_keys($pids_by_id));
  125. $pids = [];
  126. foreach ($pids_by_id as $id => $pid) {
  127. // Only delete aliases that were created by this module.
  128. if (isset($states[$id]) && $states[$id] == PathautoState::CREATE) {
  129. $pids[] = $pid;
  130. }
  131. }
  132. \Drupal::service('pathauto.alias_storage_helper')->deleteMultiple($pids);
  133. }
  134. /**
  135. * Gets the key-value store entry key for 'pathauto_state.*' collections.
  136. *
  137. * Normally we want to use the entity ID as key for 'pathauto_state.*'
  138. * collection entries. But some entity types may use string IDs. When such IDs
  139. * are exceeding 128 characters, which is the limit for the 'name' column in
  140. * the {key_value} table, the insertion of the ID in {key_value} will fail.
  141. * Thus we test if we can use the plain ID or we need to store a hashed
  142. * version of the entity ID. Also, it is not possible to rely on the UUID as
  143. * entity types might not have one or might use a non-standard format.
  144. *
  145. * The code is inspired by
  146. * \Drupal\Core\Cache\DatabaseBackend::normalizeCid().
  147. *
  148. * @param int|string $entity_id
  149. * The entity id for which to compute the key.
  150. *
  151. * @return int|string
  152. * The key used to store the value in the key-value store.
  153. *
  154. * @see \Drupal\Core\Cache\DatabaseBackend::normalizeCid()
  155. */
  156. public static function getPathautoStateKey($entity_id) {
  157. $entity_id_is_ascii = mb_check_encoding($entity_id, 'ASCII');
  158. if ($entity_id_is_ascii && strlen($entity_id) <= 128) {
  159. // The original entity ID, if it's an ASCII of 128 characters or less.
  160. return $entity_id;
  161. }
  162. return Crypt::hashBase64($entity_id);
  163. }
  164. }