summaryrefslogtreecommitdiffstats
path: root/core/lib/Drupal/Core/TypedData/DataReferenceDefinition.php
blob: 3a2a66861c61a86425c89005705128b5522608c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php

namespace Drupal\Core\TypedData;

/**
 * A typed data definition class for defining references.
 *
 * Note that this definition class assumes that the data type for referencing
 * a certain target type is named "{TARGET_TYPE}_reference".
 *
 * @see \Drupal\Core\TypedData\DataReferenceBase
 */
class DataReferenceDefinition extends DataDefinition implements DataReferenceDefinitionInterface {

  /**
   * @var \Drupal\Core\TypedData\DataDefinitionInterface
   */
  protected $targetDefinition;

  /**
   * Creates a new data reference definition.
   *
   * @param string $target_data_type
   *   The data type of the referenced data.
   *
   * @return static
   */
  public static function create($target_data_type) {
    // This assumes implementations use a "TYPE_reference" naming pattern.
    $definition = parent::create($target_data_type . '_reference');
    return $definition->setTargetDefinition(\Drupal::typedDataManager()->createDataDefinition($target_data_type));
  }

  /**
   * {@inheritdoc}
   */
  public static function createFromDataType($data_type) {
    if (substr($data_type, -strlen('_reference')) != '_reference') {
      throw new \InvalidArgumentException('Data type must be of the form "{TARGET_TYPE}_reference"');
    }
    // Cut of the _reference suffix.
    return static::create(substr($data_type, 0, strlen($data_type) - strlen('_reference')));
  }

  /**
   * {@inheritdoc}
   */
  public function getTargetDefinition() {
    return $this->targetDefinition;
  }

  /**
   * Sets the definition of the referenced data.
   *
   * @param \Drupal\Core\TypedData\DataDefinitionInterface $definition
   *   The target definition to set.
   *
   * @return $this
   */
  public function setTargetDefinition(DataDefinitionInterface $definition) {
    $this->targetDefinition = $definition;
    return $this;
  }

}