PathItem.php 3.48 KB
Newer Older
1
2
<?php

3
namespace Drupal\path\Plugin\Field\FieldType;
4

5
6
use Drupal\Component\Utility\Random;
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
15
16
 * @FieldType(
 *   id = "path",
 *   label = @Translation("Path"),
17
 *   description = @Translation("An entity field containing a path alias and related data."),
18
19
 *   no_ui = TRUE,
 *   default_widget = "path",
20
21
 *   list_class = "\Drupal\path\Plugin\Field\FieldType\PathFieldItemList",
 *   constraints = {"PathAlias" = {}},
22
 * )
23
24
25
26
 */
class PathItem extends FieldItemBase {

  /**
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
35
    $properties['langcode'] = DataDefinition::create('string')
      ->setLabel(t('Language Code'));
36
    return $properties;
37
38
  }

39
40
41
  /**
   * {@inheritdoc}
   */
42
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
43
    return [];
44
45
  }

46
47
48
49
  /**
   * {@inheritdoc}
   */
  public function isEmpty() {
50
    return ($this->alias === NULL || $this->alias === '') && ($this->pid === NULL || $this->pid === '') && ($this->langcode === NULL || $this->langcode === '');
51
52
  }

53
54
55
56
  /**
   * {@inheritdoc}
   */
  public function preSave() {
57
58
59
    if ($this->alias !== NULL) {
      $this->alias = trim($this->alias);
    }
60
61
  }

62
63
64
  /**
   * {@inheritdoc}
   */
65
  public function postSave($update) {
66
67
68
    $path_alias_storage = \Drupal::entityTypeManager()->getStorage('path_alias');
    $entity = $this->getEntity();

69
70
71
72
73
    // If specified, rely on the langcode property for the language, so that the
    // existing language of an alias can be kept. That could for example be
    // unspecified even if the field/entity has a specific langcode.
    $alias_langcode = ($this->langcode && $this->pid) ? $this->langcode : $this->getLangcode();

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
    // If we have an alias, we need to create or update a path alias entity.
    if ($this->alias) {
      if (!$update || !$this->pid) {
        $path_alias = $path_alias_storage->create([
          'path' => '/' . $entity->toUrl()->getInternalPath(),
          'alias' => $this->alias,
          'langcode' => $alias_langcode,
        ]);
        $path_alias->save();
        $this->pid = $path_alias->id();
      }
      elseif ($this->pid) {
        $path_alias = $path_alias_storage->load($this->pid);

        if ($this->alias != $path_alias->getAlias()) {
          $path_alias->setAlias($this->alias);
          $path_alias->save();
91
        }
92
93
      }
    }
94
95
96
97
98
    elseif ($this->pid && !$this->alias) {
      // Otherwise, delete the old alias if the user erased it.
      $path_alias = $path_alias_storage->load($this->pid);
      if ($entity->isDefaultRevision()) {
        $path_alias_storage->delete([$path_alias]);
99
      }
100
101
      else {
        $path_alias_storage->deleteRevision($path_alias->getRevisionID());
102
      }
103
104
105
    }
  }

106
107
108
109
110
  /**
   * {@inheritdoc}
   */
  public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
    $random = new Random();
111
    $values['alias'] = '/' . str_replace(' ', '-', strtolower($random->sentences(3)));
112
113
114
    return $values;
  }

115
116
117
118
119
120
121
  /**
   * {@inheritdoc}
   */
  public static function mainPropertyName() {
    return 'alias';
  }

122
}