Pull merge.
[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\Language\LanguageInterface;
8 use Drupal\Core\Session\AccountInterface;
9 use Drupal\Core\TypedData\ComputedItemListTrait;
10
11 /**
12  * Represents a configurable entity path field.
13  */
14 class PathFieldItemList extends FieldItemList {
15
16   use ComputedItemListTrait;
17
18   /**
19    * {@inheritdoc}
20    */
21   protected function computeValue() {
22     // Default the langcode to the current language if this is a new entity or
23     // there is no alias for an existent entity.
24     // @todo Set the langcode to not specified for untranslatable fields
25     //   in https://www.drupal.org/node/2689459.
26     $value = ['langcode' => $this->getLangcode()];
27
28     $entity = $this->getEntity();
29     if (!$entity->isNew()) {
30       $conditions = [
31         'source' => '/' . $entity->toUrl()->getInternalPath(),
32         'langcode' => $this->getLangcode(),
33       ];
34       $alias = \Drupal::service('path.alias_storage')->load($conditions);
35       if ($alias === FALSE) {
36         // Fall back to non-specific language.
37         if ($this->getLangcode() !== LanguageInterface::LANGCODE_NOT_SPECIFIED) {
38           $conditions['langcode'] = LanguageInterface::LANGCODE_NOT_SPECIFIED;
39           $alias = \Drupal::service('path.alias_storage')->load($conditions);
40         }
41       }
42
43       if ($alias) {
44         $value = $alias;
45       }
46     }
47
48     $this->list[0] = $this->createItem(0, $value);
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public function defaultAccess($operation = 'view', AccountInterface $account = NULL) {
55     if ($operation == 'view') {
56       return AccessResult::allowed();
57     }
58     return AccessResult::allowedIfHasPermissions($account, ['create url aliases', 'administer url aliases'], 'OR')->cachePerPermissions();
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function delete() {
65     // Delete all aliases associated with this entity in the current language.
66     $entity = $this->getEntity();
67     $conditions = [
68       'source' => '/' . $entity->toUrl()->getInternalPath(),
69       'langcode' => $entity->language()->getId(),
70     ];
71     \Drupal::service('path.alias_storage')->delete($conditions);
72   }
73
74 }