3 namespace Drupal\path\Plugin\Field\FieldType;
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;
12 * Represents a configurable entity path field.
14 class PathFieldItemList extends FieldItemList {
16 use ComputedItemListTrait;
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()];
28 $entity = $this->getEntity();
29 if (!$entity->isNew()) {
31 'source' => '/' . $entity->toUrl()->getInternalPath(),
32 'langcode' => $this->getLangcode(),
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);
48 $this->list[0] = $this->createItem(0, $value);
54 public function defaultAccess($operation = 'view', AccountInterface $account = NULL) {
55 if ($operation == 'view') {
56 return AccessResult::allowed();
58 return AccessResult::allowedIfHasPermissions($account, ['create url aliases', 'administer url aliases'], 'OR')->cachePerPermissions();
64 public function delete() {
65 // Delete all aliases associated with this entity in the current language.
66 $entity = $this->getEntity();
68 'source' => '/' . $entity->toUrl()->getInternalPath(),
69 'langcode' => $entity->language()->getId(),
71 \Drupal::service('path.alias_storage')->delete($conditions);