createFromDataType example


  public static function create($item_type) {
    return static::createFromItemType($item_type);
  }

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

  
protected function initializeFieldUsingFactory($factory_name) {
    switch ($factory_name) {
      case 'createFromFieldStorageDefinition':
        return FieldDefinition::createFromFieldStorageDefinition($this->storageDefinition);

      case 'create':
        $definition = FieldDefinition::create($this->fieldType);
        $definition->setFieldStorageDefinition($this->storageDefinition);
        return $definition;

      case 'createFromDataType':
        $definition = FieldDefinition::createFromDataType($this->fieldType);
        $definition->setFieldStorageDefinition($this->storageDefinition);
        return $definition;

      case 'createFromItemType':
        $definition = FieldDefinition::createFromItemType($this->fieldType);
        $definition->setFieldStorageDefinition($this->storageDefinition);
        return $definition;
    }
    throw new \InvalidArgumentException("Invalid factory name '$factory_name' passed to " . __METHOD__);
  }

}

  public function testNormalize($primitive_data$expected) {
    $this->assertSame($expected$this->normalizer->normalize($primitive_data));
  }

  /** * Data provider for testNormalize(). */
  public function dataProviderPrimitiveData() {
    $data = [];

    $definition = DataDefinition::createFromDataType('string');
    $string = new StringData($definition, 'string');
    $string->setValue('test');

    $data['string'] = [$string, 'test'];

    $definition = DataDefinition::createFromDataType('string');
    $string = new StringData($definition, 'string');
    $string->setValue(NULL);

    $data['string-null'] = [$string, NULL];

    


  /** * {@inheritdoc} */
  public function createDataDefinition($data_type) {
    $type_definition = $this->getDefinition($data_type);
    if (!isset($type_definition)) {
      throw new \InvalidArgumentException("Invalid data type '$data_type' has been given");
    }
    $class = $type_definition['definition_class'];
    $data_definition = $class::createFromDataType($data_type);

    if (method_exists($data_definition, 'setTypedDataManager')) {
      $data_definition->setTypedDataManager($this);
    }

    return $data_definition;
  }

  /** * {@inheritdoc} */
  
// Forward to the field definition class for creating new data definitions     // via the typed manager.     return BaseFieldDefinition::createFromItemType($item_type);
  }

  /** * {@inheritdoc} */
  public static function createFromDataType($type) {
    // Forward to the field definition class for creating new data definitions     // via the typed manager.     return BaseFieldDefinition::createFromDataType($type);
  }

  /** * {@inheritdoc} */
  public function getDataType() {
    // This object serves as data definition for field item lists, thus     // the correct data type is 'list'. This is not to be confused with     // the config schema type, 'field_config_base', which is used to     // describe the schema of the configuration backing this objects.     // @see \Drupal\Core\Field\FieldItemList
$default_value = [
      'value' => $this->randomMachineName(),
    ];
    $expected_default_value = [$default_value];
    $definition->setDefaultValue($default_value);
    $entity = $this->getMockBuilder('Drupal\Core\Entity\ContentEntityBase')
      ->disableOriginalConstructor()
      ->getMock();
    // Set the field item list class to be used to avoid requiring the typed     // data manager to retrieve it.     $definition->setClass('Drupal\Core\Field\FieldItemList');
    $definition->setItemDefinition(DataDefinition::createFromDataType('string')->setClass(FieldItemBase::class));
    $this->assertEquals($expected_default_value$definition->getDefaultValue($entity));

    $data_definition = $this->getMockBuilder('Drupal\Core\TypedData\DataDefinition')
      ->disableOriginalConstructor()
      ->getMock();
    $data_definition->expects($this->any())
      ->method('getClass')
      ->willReturn('Drupal\Core\Field\FieldItemBase');
    $definition->setItemDefinition($data_definition);

    // Set default value only with a literal.
Home | Imprint | This part of the site doesn't use cookies.