typedDataManager example


    ];
  }

  /** * {@inheritdoc} */
  public function getConstraints() {
    $constraints = parent::getConstraints();

    if ($max_length = $this->getSetting('max_length')) {
      $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
      $constraints[] = $constraint_manager->create('ComplexData', [
        'value' => [
          'Length' => [
            'max' => $max_length,
            'maxMessage' => $this->t('%name: may not be longer than @max characters.', ['%name' => $this->getFieldDefinition()->getLabel(), '@max' => $max_length]),
          ],
        ],
      ]);
    }

    return $constraints;
  }
$item = [];
        foreach ($column_map as $db_column => $field_column) {
          $item[$field_column] = $item_row[$db_column];
        }
        $items_by_entity[$item_row['revision_id']][] = $item;
      }
    }

    // Create field item objects and return.     foreach ($items_by_entity as $revision_id => $values) {
      $entity_adapter = $entities[$revision_id]->getTypedData();
      $items_by_entity[$revision_id] = \Drupal::typedDataManager()->create($field_definition$values$field_definition->getName()$entity_adapter);
    }
    return $items_by_entity;
  }

  /** * {@inheritdoc} */
  protected function purgeFieldItems(ContentEntityInterface $entity, FieldDefinitionInterface $field_definition) {
    $storage_definition = $field_definition->getFieldStorageDefinition();
    $is_deleted = $storage_definition->isDeleted();
    $table_mapping = $this->getTableMapping();
    


  /** * Tests recursive validation against given constraints against an entity. */
  public function testRecursiveValidate() {
    $entity = EntityTest::create();
    $adapter = EntityAdapter::createFromEntity($entity);
    // This would trigger the ValidReferenceConstraint due to EntityTest     // defaulting uid to 1, which doesn't exist. Ensure that we don't get a     // violation for that.     $this->assertCount(0, \Drupal::typedDataManager()->getValidator()->validate($adapter$adapter->getConstraints()));
  }

  /** * Tests recursive propagation of violations. */
  public function testRecursiveViolationPropagation() {
    // We create an entity reference field with a constraint which will     // trigger the validation of the referenced entities. Then we add a     // required field and populate it only on the parent entity, so that     // the child entity fails the validation.     $definitions['field_test'] = BaseFieldDefinition::create('entity_reference')
      
'#description' => $this->t('The number of digits to the right of the decimal.'),
      '#disabled' => $has_data,
    ];

    return $element;
  }

  /** * {@inheritdoc} */
  public function getConstraints() {
    $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
    $constraints = parent::getConstraints();

    $constraints[] = $constraint_manager->create('ComplexData', [
      'value' => [
        'Regex' => [
          'pattern' => '/^[+-]?((\d+(\.\d*)?)|(\.\d+))$/i',
        ],
      ],
    ]);

    return $constraints;
  }
    $this->entity = \Drupal::entityTypeManager()->getStorage('config_test')->create([
      'id' => 'system',
      'label' => 'foobar',
      'weight' => 1,
    ]);
  }

  /** * @covers \Drupal\Core\Entity\Plugin\DataType\Deriver\EntityDeriver::getDerivativeDefinitions */
  public function testEntityDeriver() {
    $definition = \Drupal::typedDataManager()->getDefinition('entity:config_test');
    $this->assertEquals(ConfigEntityAdapter::class$definition['class']);
  }

  /** * @covers ::validate */
  public function testValidate() {
    $adapter = ConfigEntityAdapter::createFromEntity($this->entity);
    $violations = $adapter->validate();
    $this->assertEmpty($violations);
    $this->entity = \Drupal::entityTypeManager()->getStorage('config_test')->create([
      
return $this;
  }

  /** * {@inheritdoc} */
  public function getClass() {
    if (isset($this->definition['class'])) {
      return $this->definition['class'];
    }
    else {
      $type_definition = \Drupal::typedDataManager()->getDefinition($this->getDataType());
      return $type_definition['class'];
    }
  }

  /** * Sets the class used for creating the typed data object. * * @param string|null $class * The class to use. * * @return static * The object itself for chaining. */
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} */
class LinkItemUrlValidationTest extends FieldKernelTestBase {

  /** * {@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) {
        


  /** * Returns the typed data class name for this entity. * * @return string * The string representing the typed data class name. * * @see \Drupal\Core\Entity\Plugin\DataType\EntityAdapter */
  private function getTypedDataClass(): string {
    $typed_data_manager = \Drupal::typedDataManager();

    // Check more specific data types that could apply to this entity.     $candidate_data_types = [
      "entity:{$this->getEntityTypeId()}:{$this->bundle()}",
      "entity:{$this->getEntityTypeId()}",
    ];
    foreach ($candidate_data_types as $candidate_data_type) {
      if ($typed_data_manager->hasDefinition($candidate_data_type)) {
        return $typed_data_manager->getDefinition($candidate_data_type)['class'];
      }
    }

    

  public function setConfig($key$value) {
    if ($definition = $this->getConfigDefinition($key)) {
      $typed_data = \Drupal::typedDataManager()->create($definition$value);

      if ($typed_data->validate()->count() > 0) {
        throw new PluginException("The provided configuration value does not pass validation.");
      }
    }
    $this->configuration[$key] = $value;
    return $this;
  }

}
class ConstraintFactoryTest extends KernelTestBase {

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

  /** * @covers ::createInstance */
  public function testCreateInstance() {
    $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();

    // If the plugin is a \Symfony\Component\Validator\Constraint, they will be     // created first.     $this->assertInstanceOf(Constraint::class$constraint_manager->create('Uuid', []));

    // If the plugin implements the     // \Drupal\Core\Plugin\ContainerFactoryPluginInterface, they will be created     // second.     $container_factory_plugin = $constraint_manager->create('EntityTestContainerFactoryPlugin', []);
    $this->assertNotInstanceOf(Constraint::class$container_factory_plugin);
    $this->assertInstanceOf(ContainerFactoryPluginInterface::class$container_factory_plugin);

    
return $this;
  }

  /** * Gets the typed data manager. * * @return \Drupal\Core\TypedData\TypedDataManagerInterface * The typed data manager. */
  public function getTypedDataManager() {
    if (empty($this->typedDataManager)) {
      $this->typedDataManager = \Drupal::typedDataManager();
    }

    return $this->typedDataManager;
  }

}
public function isEmpty() {
    if (empty($this->value) && (string) $this->value !== '0') {
      return TRUE;
    }
    return FALSE;
  }

  /** * {@inheritdoc} */
  public function getConstraints() {
    $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
    $constraints = parent::getConstraints();

    $settings = $this->getSettings();
    $label = $this->getFieldDefinition()->getLabel();

    if (isset($settings['min']) && $settings['min'] !== '') {
      $min = $settings['min'];
      $constraints[] = $constraint_manager->create('ComplexData', [
        'value' => [
          'Range' => [
            'min' => $min,
            
$this->setMockContainerService('module_handler');
    $this->assertNotNull(\Drupal::moduleHandler());
  }

  /** * Tests the typedDataManager() method. * * @covers ::typedDataManager */
  public function testTypedDataManager() {
    $this->setMockContainerService('typed_data_manager');
    $this->assertNotNull(\Drupal::typedDataManager());
  }

  /** * Tests the token() method. * * @covers ::token */
  public function testToken() {
    $this->setMockContainerService('token');
    $this->assertNotNull(\Drupal::token());
  }

  


  /** * {@inheritdoc} */
  public function getConstraints() {
    $constraints = parent::getConstraints();

    // If this is an unsigned integer, add a validation constraint for the     // integer to be positive.     if ($this->getSetting('unsigned')) {
      $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
      $constraints[] = $constraint_manager->create('ComplexData', [
        'value' => [
          'Range' => [
            'min' => 0,
            'minMessage' => $this->t('%name: The integer must be larger or equal to %min.', [
              '%name' => $this->getFieldDefinition()->getLabel(),
              '%min' => 0,
            ]),
          ],
        ],
      ]);
    }
Home | Imprint | This part of the site doesn't use cookies.