Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / path / src / Plugin / Field / FieldType / PathFieldItemList.php
1 <?php
2
3 namespace Drupal\path\Plugin\Field\FieldType;
4
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Field\FieldItemList;
7 use Drupal\Core\Session\AccountInterface;
8 use Drupal\Core\TypedData\ComputedItemListTrait;
9
10 /**
11  * Represents a configurable entity path field.
12  */
13 class PathFieldItemList extends FieldItemList {
14
15   use ComputedItemListTrait;
16
17   /**
18    * {@inheritdoc}
19    */
20   protected function computeValue() {
21     // Default the langcode to the current language if this is a new entity or
22     // there is no alias for an existent entity.
23     // @todo Set the langcode to not specified for untranslatable fields
24     //   in https://www.drupal.org/node/2689459.
25     $value = ['langcode' => $this->getLangcode()];
26
27     $entity = $this->getEntity();
28     if (!$entity->isNew()) {
29       // @todo Support loading language neutral aliases in
30       //   https://www.drupal.org/node/2511968.
31       $alias = \Drupal::service('path.alias_storage')->load([
32         'source' => '/' . $entity->toUrl()->getInternalPath(),
33         'langcode' => $this->getLangcode(),
34       ]);
35
36       if ($alias) {
37         $value = $alias;
38       }
39     }
40
41     $this->list[0] = $this->createItem(0, $value);
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public function defaultAccess($operation = 'view', AccountInterface $account = NULL) {
48     if ($operation == 'view') {
49       return AccessResult::allowed();
50     }
51     return AccessResult::allowedIfHasPermissions($account, ['create url aliases', 'administer url aliases'], 'OR')->cachePerPermissions();
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public function delete() {
58     // Delete all aliases associated with this entity in the current language.
59     $entity = $this->getEntity();
60     $conditions = [
61       'source' => '/' . $entity->toUrl()->getInternalPath(),
62       'langcode' => $entity->language()->getId(),
63     ];
64     \Drupal::service('path.alias_storage')->delete($conditions);
65   }
66
67 }