function EntityDataDefinition::create

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Entity/TypedData/EntityDataDefinition.php \Drupal\Core\Entity\TypedData\EntityDataDefinition::create()
  2. 10 core/lib/Drupal/Core/Entity/TypedData/EntityDataDefinition.php \Drupal\Core\Entity\TypedData\EntityDataDefinition::create()
  3. 11.x core/lib/Drupal/Core/Entity/TypedData/EntityDataDefinition.php \Drupal\Core\Entity\TypedData\EntityDataDefinition::create()

Creates a new entity definition.

Parameters

string $entity_type_id: (optional) The ID of the entity type, or NULL if the entity type is unknown. Defaults to NULL.

Return value

static

Overrides DataDefinition::create

5 calls to EntityDataDefinition::create()
EntityContextDefinition::getSampleValues in core/lib/Drupal/Core/Plugin/Context/EntityContextDefinition.php
Returns typed data objects representing this context definition.
EntityDataDefinition::createFromDataType in core/lib/Drupal/Core/Entity/TypedData/EntityDataDefinition.php
Creates a new data definition object.
EntityFieldTest::doTestIntrospection in core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php
Executes the introspection tests for the given entity type.
EntityTypedDataDefinitionTest::testEntities in core/tests/Drupal/KernelTests/Core/Entity/EntityTypedDataDefinitionTest.php
Tests deriving metadata about entities.
EntityTypedDataDefinitionTest::testEntityDefinitionIsInternal in core/tests/Drupal/KernelTests/Core/Entity/EntityTypedDataDefinitionTest.php
Tests that an entity annotation can mark the data definition as internal.

File

core/lib/Drupal/Core/Entity/TypedData/EntityDataDefinition.php, line 21

Class

EntityDataDefinition
A typed data definition class for describing entities.

Namespace

Drupal\Core\Entity\TypedData

Code

public static function create($entity_type_id = NULL, $bundle = NULL) {
    // If the entity type is known, use the derived definition.
    if (isset($entity_type_id)) {
        $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.
        $definition->setEntityTypeId($entity_type_id);
        // If available, set the Bundle constraint.
        if ($bundle) {
            $definition->setBundles([
                $bundle,
            ]);
        }
        return $definition;
    }
    return new static([]);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.