EntityDataDefinition.php 4.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<?php

namespace Drupal\Core\Entity\TypedData;

use Drupal\Core\TypedData\ComplexDataDefinitionBase;

/**
 * A typed data definition class for describing entities.
 */
class EntityDataDefinition extends ComplexDataDefinitionBase implements EntityDataDefinitionInterface {

  /**
   * Creates a new entity definition.
   *
   * @param string $entity_type_id
   *   (optional) The ID of the entity type, or NULL if the entity type is
   *   unknown. Defaults to NULL.
18 19 20
   * @param string $bundle
   *   (optional) The bundle of the entity type, or NULL if the bundle is
   *   unknown. Defaults to NULL.
21 22 23
   *
   * @return static
   */
24 25
  public static function create($entity_type_id = NULL, $bundle = NULL) {
    // If the entity type is known, use the derived definition.
26
    if (isset($entity_type_id)) {
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
      $data_type = "entity:{$entity_type_id}";

      // If a bundle was given, use the bundle-specific definition.
      if ($bundle) {
        $data_type .= ":{$bundle}";
      }

      // It's possible that the given entity type ID or bundle wasn't discovered
      // by the TypedData plugin manager and/or weren't created by the
      // EntityDeriver. In that case, this is a new definition and we'll just
      // create the definition from defaults by using an empty array.
      $values = \Drupal::typedDataManager()->getDefinition($data_type, FALSE);
      $definition = new static(is_array($values) ? $values : []);

      // Set the EntityType constraint using the given entity type ID.
42
      $definition->setEntityTypeId($entity_type_id);
43 44 45 46 47 48 49

      // If available, set the Bundle constraint.
      if ($bundle) {
        $definition->setBundles([$bundle]);
      }

      return $definition;
50
    }
51 52

    return new static([]);
53 54 55 56 57 58 59 60 61 62
  }

  /**
   * {@inheritdoc}
   */
  public static function createFromDataType($data_type) {
    $parts = explode(':', $data_type);
    if ($parts[0] != 'entity') {
      throw new \InvalidArgumentException('Data type must be in the form of "entity:ENTITY_TYPE:BUNDLE."');
    }
63 64 65 66
    return static::create(
      isset($parts[1]) ? $parts[1] : NULL,
      isset($parts[2]) ? $parts[2] : NULL
    );
67 68 69 70 71 72 73 74
  }

  /**
   * {@inheritdoc}
   */
  public function getPropertyDefinitions() {
    if (!isset($this->propertyDefinitions)) {
      if ($entity_type_id = $this->getEntityTypeId()) {
75
        // Return an empty array for entities that are not content entities.
76
        $entity_type_class = \Drupal::entityTypeManager()->getDefinition($entity_type_id)->getClass();
77
        if (!in_array('Drupal\Core\Entity\FieldableEntityInterface', class_implements($entity_type_class))) {
78
          $this->propertyDefinitions = [];
79 80
        }
        else {
81
          // @todo: Add support for handling multiple bundles.
82
          // See https://www.drupal.org/node/2169813.
83 84
          $bundles = $this->getBundles();
          if (is_array($bundles) && count($bundles) == 1) {
85
            $this->propertyDefinitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type_id, reset($bundles));
86 87
          }
          else {
88
            $this->propertyDefinitions = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions($entity_type_id);
89
          }
90
        }
91 92 93
      }
      else {
        // No entity type given.
94
        $this->propertyDefinitions = [];
95 96 97 98 99 100 101 102 103 104 105 106
      }
    }
    return $this->propertyDefinitions;
  }

  /**
   * {@inheritdoc}
   */
  public function getDataType() {
    $type = 'entity';
    if ($entity_type = $this->getEntityTypeId()) {
      $type .= ':' . $entity_type;
107 108
      // Append the bundle only if we know it for sure and it is not the default
      // bundle.
109
      if (($bundles = $this->getBundles()) && count($bundles) == 1) {
110 111 112 113
        $bundle = reset($bundles);
        if ($bundle != $entity_type) {
          $type .= ':' . $bundle;
        }
114 115 116 117 118 119 120 121 122
      }
    }
    return $type;
  }

  /**
   * {@inheritdoc}
   */
  public function getEntityTypeId() {
123
    return isset($this->definition['constraints']['EntityType']) ? $this->definition['constraints']['EntityType'] : NULL;
124 125 126 127 128 129 130 131 132 133 134 135 136
  }

  /**
   * {@inheritdoc}
   */
  public function setEntityTypeId($entity_type_id) {
    return $this->addConstraint('EntityType', $entity_type_id);
  }

  /**
   * {@inheritdoc}
   */
  public function getBundles() {
137
    $bundle = isset($this->definition['constraints']['Bundle']) ? $this->definition['constraints']['Bundle'] : NULL;
138
    return is_string($bundle) ? [$bundle] : $bundle;
139 140 141 142 143 144 145 146 147 148 149
  }

  /**
   * {@inheritdoc}
   */
  public function setBundles(array $bundles = NULL) {
    if (isset($bundles)) {
      $this->addConstraint('Bundle', $bundles);
    }
    else {
      // Remove the constraint.
150
      unset($this->definition['constraints']['Bundle']);
151 152 153 154 155
    }
    return $this;
  }

}