createDataDefinition example

public static function createFromDataType($type) {
    $definition = parent::createFromDataType($type);
    // If nothing else given, default to a list of 'any' items.     $definition->itemDefinition = DataDefinition::create('any');
    return $definition;
  }

  /** * {@inheritdoc} */
  public static function createFromItemType($item_type) {
    return new static([], \Drupal::typedDataManager()->createDataDefinition($item_type));
  }

  /** * {@inheritdoc} */
  public function __construct(array $values = [], DataDefinitionInterface $item_definition = NULL) {
    $this->definition = $values;
    $this->itemDefinition = $item_definition;
  }

  /** * {@inheritdoc} */
return $this;
  }

  /** * {@inheritdoc} */
  public function getDataDefinition() {
    if ($this->isMultiple()) {
      $definition = $this->getTypedDataManager()->createListDataDefinition($this->getDataType());
    }
    else {
      $definition = $this->getTypedDataManager()->createDataDefinition($this->getDataType());
    }

    if (!$definition) {
      throw new \Exception("The data type '{$this->getDataType()}' is invalid");
    }
    $definition->setLabel($this->getLabel())
      ->setDescription($this->getDescription())
      ->setRequired($this->isRequired());
    $constraints = $definition->getConstraints() + $this->getConstraints();
    $definition->setConstraints($constraints);
    return $definition;
  }


  /** * Tests that types are derived for entity types with and without bundles. * * @dataProvider derivativesProvider */
  public function testDerivatives($data_type$expect_exception) {
    if ($expect_exception) {
      $this->expectException(PluginNotFoundException::class);
    }
    $this->typedDataManager->createDataDefinition($data_type);
  }

  /** * Provides test data for ::testDerivatives(). */
  public function derivativesProvider() {
    return [
      'unbundleable entity type with no bundle type' => ['entity:user', FALSE],
      'unbundleable entity type with bundle type' => ['entity:user:user', TRUE],
      'bundleable entity type with no bundle type' => ['entity:node', FALSE],
      'bundleable entity type with bundle type' => [
        
$this->assertEquals('value', $field_item_definition->getMainPropertyName());
    $this->assertNull($field_item_definition->getPropertyDefinition('invalid'));

    // Test accessing field item property metadata via the field definition.     $this->assertInstanceOf(FieldDefinitionInterface::class$field_definition);
    $this->assertEquals(['value']array_keys($field_definition->getPropertyDefinitions()));
    $this->assertEquals('integer', $field_definition->getPropertyDefinition('value')->getDataType());
    $this->assertEquals('value', $field_definition->getMainPropertyName());
    $this->assertNull($field_definition->getPropertyDefinition('invalid'));

    // Test using the definition factory for field item lists and field items.     $field_item = $this->typedDataManager->createDataDefinition('field_item:integer');
    $this->assertNotInstanceOf(ListDataDefinitionInterface::class$field_item);
    $this->assertInstanceOf(ComplexDataDefinitionInterface::class$field_item);
    // Comparison should ignore the internal static cache, so compare the     // serialized objects instead.     $this->assertEquals(serialize($field_item_definition)serialize($field_item));

    $field_definition2 = $this->typedDataManager->createListDataDefinition('field_item:integer');
    $this->assertInstanceOf(ListDataDefinitionInterface::class$field_definition2);
    $this->assertNotInstanceOf(ComplexDataDefinitionInterface::class$field_definition2);
    $this->assertEquals(serialize($field_definition)serialize($field_definition2));
  }

  
/** * {@inheritdoc} */
  protected static $modules = ['link'];

  /** * Tests link validation. */
  public function testExternalLinkValidation() {
    $definition = \Drupal::typedDataManager()
      ->createDataDefinition('field_item:link');
    $link_item = \Drupal::typedDataManager()->create($definition);
    $test_links = $this->getTestLinks();

    foreach ($test_links as $data) {
      [$value$expected_violations] = $data;
      $link_item->setValue($value);
      $violations = $link_item->validate();
      $expected_count = count($expected_violations);
      $this->assertCount($expected_count$violationssprintf('Violation message count error for %s', $value));
      if ($expected_count) {
        $i = 0;
        
$item_definition = $list_definition->getItemDefinition();
    $this->assertInstanceOf(DataDefinitionInterface::class$item_definition);
    $this->assertEquals('string', $item_definition->getDataType());

    // Test using the definition factory.     $list_definition2 = $this->typedDataManager->createListDataDefinition('string');
    $this->assertInstanceOf(ListDataDefinitionInterface::class$list_definition2);
    $this->assertEquals($list_definition$list_definition2);

    // Test creating the definition of data with type 'list', which is the same     // as creating a definition of a list of items of type 'any'.     $list_definition = $this->typedDataManager->createDataDefinition('list');
    $this->assertInstanceOf(ListDataDefinitionInterface::class$list_definition);
    $this->assertEquals('list', $list_definition->getDataType());
    $this->assertEquals('any', $list_definition->getItemDefinition()->getDataType());
  }

  /** * Tests deriving metadata about maps. */
  public function testMaps() {
    $map_definition = MapDataDefinition::create()
      ->setPropertyDefinition('one', DataDefinition::create('string'))
      
/** * 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')));
  }
if (isset($name)) {
        $replace['%key'] = $name;
      }
      $type = $this->replaceName($type$replace);
      // Remove the type from the definition so that it is replaced with the       // concrete type from schema definitions.       unset($definition['type']);
    }
    // Add default values from type definition.     $definition += $this->getDefinitionWithReplacements($type$replace);

    $data_definition = $this->createDataDefinition($definition['type']);

    // Pass remaining values from definition array to data definition.     foreach ($definition as $key => $value) {
      if (!isset($data_definition[$key])) {
        $data_definition[$key] = $value;
      }
    }
    return $data_definition;
  }

  /** * Determines the typed config type for a plugin ID. * * @param string $base_plugin_id * The plugin ID. * @param array $definitions * An array of typed config definitions. * * @return string * The typed config type for the given plugin ID. */
/** * Asserts a set of validation errors is raised when the entity is validated. * * @param array<string, string|string[]> $expected_messages * The expected validation error messages. Keys are property paths, values * are the expected messages: a string if a single message is expected, an * array of strings if multiple are expected. */
  protected function assertValidationErrors(array $expected_messages): void {
    /** @var \Drupal\Core\TypedData\TypedDataManagerInterface $typed_data */
    $typed_data = $this->container->get('typed_data_manager');
    $definition = $typed_data->createDataDefinition('entity:' . $this->entity->getEntityTypeId());
    $violations = $typed_data->create($definition$this->entity)->validate();

    $actual_messages = [];
    foreach ($violations as $violation) {
      if (!isset($actual_messages[$violation->getPropertyPath()])) {
        $actual_messages[$violation->getPropertyPath()] = (string) $violation->getMessage();
      }
      else {
        // Transform value from string to array.         if (is_string($actual_messages[$violation->getPropertyPath()])) {
          $actual_messages[$violation->getPropertyPath()] = (array) $actual_messages[$violation->getPropertyPath()];
        }
Home | Imprint | This part of the site doesn't use cookies.