Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / path / src / Plugin / Field / FieldType / PathItem.php
1 <?php
2
3 namespace Drupal\path\Plugin\Field\FieldType;
4
5 use Drupal\Component\Utility\Random;
6 use Drupal\Core\Field\FieldDefinitionInterface;
7 use Drupal\Core\Field\FieldStorageDefinitionInterface;
8 use Drupal\Core\Field\FieldItemBase;
9 use Drupal\Core\TypedData\DataDefinition;
10
11 /**
12  * Defines the 'path' entity field type.
13  *
14  * @FieldType(
15  *   id = "path",
16  *   label = @Translation("Path"),
17  *   description = @Translation("An entity field containing a path alias and related data."),
18  *   no_ui = TRUE,
19  *   default_widget = "path",
20  *   list_class = "\Drupal\path\Plugin\Field\FieldType\PathFieldItemList",
21  *   constraints = {"PathAlias" = {}},
22  * )
23  */
24 class PathItem extends FieldItemBase {
25
26   /**
27    * {@inheritdoc}
28    */
29   public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
30     $properties['alias'] = DataDefinition::create('string')
31       ->setLabel(t('Path alias'));
32     $properties['pid'] = DataDefinition::create('integer')
33       ->setLabel(t('Path id'));
34     $properties['langcode'] = DataDefinition::create('string')
35       ->setLabel(t('Language Code'));
36     return $properties;
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public static function schema(FieldStorageDefinitionInterface $field_definition) {
43     return [];
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   public function isEmpty() {
50     return ($this->alias === NULL || $this->alias === '') && ($this->pid === NULL || $this->pid === '') && ($this->langcode === NULL || $this->langcode === '');
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function preSave() {
57     if ($this->alias !== NULL) {
58       $this->alias = trim($this->alias);
59     }
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function postSave($update) {
66     if (!$update) {
67       if ($this->alias) {
68         $entity = $this->getEntity();
69         if ($path = \Drupal::service('path.alias_storage')->save('/' . $entity->urlInfo()->getInternalPath(), $this->alias, $this->getLangcode())) {
70           $this->pid = $path['pid'];
71         }
72       }
73     }
74     else {
75       // Delete old alias if user erased it.
76       if ($this->pid && !$this->alias) {
77         \Drupal::service('path.alias_storage')->delete(['pid' => $this->pid]);
78       }
79       // Only save a non-empty alias.
80       elseif ($this->alias) {
81         $entity = $this->getEntity();
82         \Drupal::service('path.alias_storage')->save('/' . $entity->urlInfo()->getInternalPath(), $this->alias, $this->getLangcode(), $this->pid);
83       }
84     }
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
91     $random = new Random();
92     $values['alias'] = '/' . str_replace(' ', '-', strtolower($random->sentences(3)));
93     return $values;
94   }
95
96   /**
97    * {@inheritdoc}
98    */
99   public static function mainPropertyName() {
100     return 'alias';
101   }
102
103 }