3 namespace Drupal\path\Plugin\Field\FieldType;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Field\FieldItemList;
7 use Drupal\Core\Session\AccountInterface;
8 use Drupal\Core\TypedData\ComputedItemListTrait;
11 * Represents a configurable entity path field.
13 class PathFieldItemList extends FieldItemList {
15 use ComputedItemListTrait;
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()];
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(),
41 $this->list[0] = $this->createItem(0, $value);
47 public function defaultAccess($operation = 'view', AccountInterface $account = NULL) {
48 if ($operation == 'view') {
49 return AccessResult::allowed();
51 return AccessResult::allowedIfHasPermissions($account, ['create url aliases', 'administer url aliases'], 'OR')->cachePerPermissions();
57 public function delete() {
58 // Delete all aliases associated with this entity in the current language.
59 $entity = $this->getEntity();
61 'source' => '/' . $entity->toUrl()->getInternalPath(),
62 'langcode' => $entity->language()->getId(),
64 \Drupal::service('path.alias_storage')->delete($conditions);