Pathologic was missing because of a .git folder inside.
[yaffs-website] / web / modules / contrib / pathauto / src / AliasStorageHelper.php
1 <?php
2
3 namespace Drupal\pathauto;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Database\Connection;
7 use Drupal\Core\Entity\EntityInterface;
8 use Drupal\Core\Language\LanguageInterface;
9 use Drupal\Core\Path\AliasStorageInterface;
10 use Drupal\Core\StringTranslation\StringTranslationTrait;
11 use Drupal\Core\StringTranslation\TranslationInterface;
12
13 /**
14  * Provides helper methods for accessing alias storage.
15  */
16 class AliasStorageHelper implements AliasStorageHelperInterface {
17
18   use StringTranslationTrait;
19
20   /**
21    * Alias schema max length.
22    *
23    * @var int
24    */
25   protected $aliasSchemaMaxLength = 255;
26
27   /**
28    * Config factory.
29    *
30    * @var \Drupal\Core\Config\ConfigFactoryInterface
31    */
32   protected $configFactory;
33
34   /**
35    * The alias storage.
36    *
37    * @var \Drupal\Core\Path\AliasStorageInterface
38    */
39   protected $aliasStorage;
40
41   /**
42    * The database connection.
43    *
44    * @var \Drupal\Core\Database\Connection
45    */
46   protected $database;
47
48   /**
49    * The messenger.
50    *
51    * @var \Drupal\pathauto\MessengerInterface
52    */
53   protected $messenger;
54
55   /**
56    * The config factory.
57    *
58    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
59    *   The config factory.
60    * @param \Drupal\Core\Path\AliasStorageInterface $alias_storage
61    *   The alias storage.
62    * @param \Drupal\Core\Database\Connection $database
63    *   The database connection.
64    * @param MessengerInterface $messenger
65    *   The messenger.
66    * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
67    *   The string translation service.
68    */
69   public function __construct(ConfigFactoryInterface $config_factory, AliasStorageInterface $alias_storage, Connection $database, MessengerInterface $messenger, TranslationInterface $string_translation) {
70     $this->configFactory = $config_factory;
71     $this->aliasStorage = $alias_storage;
72     $this->database = $database;
73     $this->messenger = $messenger;
74     $this->stringTranslation = $string_translation;
75   }
76
77   /**
78    * {@inheritdoc}
79    */
80   public function getAliasSchemaMaxLength() {
81     return $this->aliasSchemaMaxLength;
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function save(array $path, $existing_alias = NULL, $op = NULL) {
88     $config = $this->configFactory->get('pathauto.settings');
89
90     // Alert users if they are trying to create an alias that is the same as the
91     // internal path.
92     if ($path['source'] == $path['alias']) {
93       $this->messenger->addMessage($this->t('Ignoring alias %alias because it is the same as the internal path.', array('%alias' => $path['alias'])));
94       return NULL;
95     }
96
97     // Skip replacing the current alias with an identical alias.
98     if (empty($existing_alias) || $existing_alias['alias'] != $path['alias']) {
99       $path += array(
100         'pathauto' => TRUE,
101         'original' => $existing_alias,
102         'pid' => NULL,
103       );
104
105       // If there is already an alias, respect some update actions.
106       if (!empty($existing_alias)) {
107         switch ($config->get('update_action')) {
108           case PathautoGeneratorInterface::UPDATE_ACTION_NO_NEW:
109             // Do not create the alias.
110             return NULL;
111
112           case PathautoGeneratorInterface::UPDATE_ACTION_LEAVE:
113             // Create a new alias instead of overwriting the existing by leaving
114             // $path['pid'] empty.
115             break;
116
117           case PathautoGeneratorInterface::UPDATE_ACTION_DELETE:
118             // The delete actions should overwrite the existing alias.
119             $path['pid'] = $existing_alias['pid'];
120             break;
121         }
122       }
123
124       // Save the path array.
125       $this->aliasStorage->save($path['source'], $path['alias'], $path['language'], $path['pid']);
126
127       if (!empty($existing_alias['pid'])) {
128         $this->messenger->addMessage($this->t(
129             'Created new alias %alias for %source, replacing %old_alias.',
130             array(
131               '%alias' => $path['alias'],
132               '%source' => $path['source'],
133               '%old_alias' => $existing_alias['alias'],
134             )
135           )
136         );
137       }
138       else {
139         $this->messenger->addMessage($this->t('Created new alias %alias for %source.', array(
140           '%alias' => $path['alias'],
141           '%source' => $path['source'],
142         )));
143       }
144
145       return $path;
146     }
147   }
148
149   /**
150    * {@inheritdoc}
151    */
152   public function loadBySource($source, $language = LanguageInterface::LANGCODE_NOT_SPECIFIED) {
153     $alias = $this->aliasStorage->load([
154       'source' => $source,
155       'langcode' => $language,
156     ]);
157     // If no alias was fetched and if a language was specified, fallbacks to
158     // undefined language.
159     if (!$alias && ($language !== LanguageInterface::LANGCODE_NOT_SPECIFIED)) {
160       $alias = $this->aliasStorage->load([
161         'source' => $source,
162         'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
163       ]);
164     }
165     return $alias;
166   }
167
168   /**
169    * {@inheritdoc}
170    */
171   public function deleteBySourcePrefix($source) {
172     $pids = $this->loadBySourcePrefix($source);
173     if ($pids) {
174       $this->deleteMultiple($pids);
175     }
176   }
177
178   /**
179    * {@inheritdoc}
180    */
181   public function deleteAll() {
182     $this->database->truncate('url_alias')->execute();
183   }
184
185   /**
186    * {@inheritdoc}
187    */
188   public function deleteEntityPathAll(EntityInterface $entity, $default_uri = NULL) {
189     $this->deleteBySourcePrefix('/' . $entity->toUrl('canonical')->getInternalPath());
190     if (isset($default_uri) && $entity->toUrl('canonical')->toString() != $default_uri) {
191       $this->deleteBySourcePrefix($default_uri);
192     }
193   }
194
195   /**
196    * {@inheritdoc}
197    */
198   public function loadBySourcePrefix($source) {
199     $select = $this->database->select('url_alias', 'u')
200       ->fields('u', array('pid'));
201
202     $or_group = $select->orConditionGroup()
203       ->condition('source', $source)
204       ->condition('source', rtrim($source, '/') . '/%', 'LIKE');
205
206     return $select
207       ->condition($or_group)
208       ->execute()
209       ->fetchCol();
210   }
211
212   /**
213    * {@inheritdoc}
214    */
215   public function countBySourcePrefix($source) {
216     $select = $this->database->select('url_alias', 'u')
217       ->fields('u', array('pid'));
218
219     $or_group = $select->orConditionGroup()
220       ->condition('source', $source)
221       ->condition('source', rtrim($source, '/') . '/%', 'LIKE');
222
223     return $select
224       ->condition($or_group)
225       ->countQuery()
226       ->execute()
227       ->fetchField();
228   }
229
230   /**
231    * {@inheritdoc}
232    */
233   public function countAll() {
234     return $this->database->select('url_alias')
235       ->countQuery()
236       ->execute()
237       ->fetchField();
238   }
239
240   /**
241    * Delete multiple URL aliases.
242    *
243    * Intent of this is to abstract a potential path_delete_multiple() function
244    * for Drupal 7 or 8.
245    *
246    * @param int[] $pids
247    *   An array of path IDs to delete.
248    */
249   public function deleteMultiple($pids) {
250     foreach ($pids as $pid) {
251       $this->aliasStorage->delete(array('pid' => $pid));
252     }
253   }
254
255 }