PathFieldItemList.php in Drupal 9
File
core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php
View source
<?php
namespace Drupal\path\Plugin\Field\FieldType;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Field\FieldItemList;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TypedData\ComputedItemListTrait;
class PathFieldItemList extends FieldItemList {
use ComputedItemListTrait;
protected function computeValue() {
$value = [
'langcode' => $this
->getLangcode(),
];
$entity = $this
->getEntity();
if (!$entity
->isNew()) {
$path_alias_repository = \Drupal::service('path_alias.repository');
if ($path_alias = $path_alias_repository
->lookupBySystemPath('/' . $entity
->toUrl()
->getInternalPath(), $this
->getLangcode())) {
$value = [
'alias' => $path_alias['alias'],
'pid' => $path_alias['id'],
'langcode' => $path_alias['langcode'],
];
}
}
$this->list[0] = $this
->createItem(0, $value);
}
public function defaultAccess($operation = 'view', AccountInterface $account = NULL) {
if ($operation == 'view') {
return AccessResult::allowed();
}
return AccessResult::allowedIfHasPermissions($account, [
'create url aliases',
'administer url aliases',
], 'OR')
->cachePerPermissions();
}
public function delete() {
$entity = $this
->getEntity();
$path_alias_storage = \Drupal::entityTypeManager()
->getStorage('path_alias');
$entities = $path_alias_storage
->loadByProperties([
'path' => '/' . $entity
->toUrl()
->getInternalPath(),
'langcode' => $entity
->language()
->getId(),
]);
$path_alias_storage
->delete($entities);
}
}