"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php" (26 Nov 2020, 2134 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "PathFieldItemList.php" see the Fossies "Dox" file reference documentation.

    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       /** @var \Drupal\path_alias\AliasRepositoryInterface $path_alias_repository */
   30       $path_alias_repository = \Drupal::service('path_alias.repository');
   31 
   32       if ($path_alias = $path_alias_repository->lookupBySystemPath('/' . $entity->toUrl()->getInternalPath(), $this->getLangcode())) {
   33         $value = [
   34           'alias' => $path_alias['alias'],
   35           'pid' => $path_alias['id'],
   36           'langcode' => $path_alias['langcode'],
   37         ];
   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     $path_alias_storage = \Drupal::entityTypeManager()->getStorage('path_alias');
   61     $entities = $path_alias_storage->loadByProperties([
   62       'path' => '/' . $entity->toUrl()->getInternalPath(),
   63       'langcode' => $entity->language()->getId(),
   64     ]);
   65     $path_alias_storage->delete($entities);
   66   }
   67 
   68 }