本文整理汇总了PHP中Drupal\Core\TypedData\TypedDataManager::create方法的典型用法代码示例。如果您正苦于以下问题:PHP TypedDataManager::create方法的具体用法?PHP TypedDataManager::create怎么用?PHP TypedDataManager::create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\TypedData\TypedDataManager
的用法示例。
在下文中一共展示了TypedDataManager::create方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testValidation
/**
* Tests the ComplexData validation constraint validator.
*
* For testing a map including a constraint on one of its keys is defined.
*/
public function testValidation()
{
// Create a definition that specifies some ComplexData constraint.
$definition = MapDataDefinition::create()->setPropertyDefinition('key', DataDefinition::create('integer'))->addConstraint('ComplexData', array('key' => array('AllowedValues' => array(1, 2, 3))));
// Test the validation.
$typed_data = $this->typedData->create($definition, array('key' => 1));
$violations = $typed_data->validate();
$this->assertEqual($violations->count(), 0, 'Validation passed for correct value.');
// Test the validation when an invalid value is passed.
$typed_data = $this->typedData->create($definition, array('key' => 4));
$violations = $typed_data->validate();
$this->assertEqual($violations->count(), 1, 'Validation failed for incorrect value.');
// Make sure the information provided by a violation is correct.
$violation = $violations[0];
$this->assertEqual($violation->getMessage(), t('The value you selected is not a valid choice.'), 'The message for invalid value is correct.');
$this->assertEqual($violation->getRoot(), $typed_data, 'Violation root is correct.');
$this->assertEqual($violation->getInvalidValue(), 4, 'The invalid value is set correctly in the violation.');
// Test using the constraint with a map without the specified key. This
// should be ignored as long as there is no NotNull or NotBlank constraint.
$typed_data = $this->typedData->create($definition, array('foo' => 'bar'));
$violations = $typed_data->validate();
$this->assertEqual($violations->count(), 0, 'Constraint on non-existing key is ignored.');
$definition = MapDataDefinition::create()->setPropertyDefinition('key', DataDefinition::create('integer'))->addConstraint('ComplexData', array('key' => array('NotNull' => array())));
$typed_data = $this->typedData->create($definition, array('foo' => 'bar'));
$violations = $typed_data->validate();
$this->assertEqual($violations->count(), 1, 'Key is required.');
}
示例2: testGetContextValuesContext
/**
* @covers ::getContextValues
*/
public function testGetContextValuesContext()
{
$data_definition = DataDefinition::createFromDataType('integer');
$typed_data = IntegerData::createInstance($data_definition);
$this->typedDataManager->createDataDefinition('integer')->willReturn($data_definition);
$this->typedDataManager->getDefaultConstraints($data_definition)->willReturn([]);
$this->typedDataManager->create($data_definition, 5)->willReturn($typed_data);
$input = ['foo' => ['label' => 'Foo', 'type' => 'integer', 'value' => 5]];
$expected = new Context(new ContextDefinition('integer', 'Foo'), 5);
$actual = $this->staticContext->getContextValues($input)['foo'];
$this->assertEquals($expected, $actual);
}
示例3: getTypedData
/**
* Returns a typed data object.
*
* This helper for quick creation of typed data objects.
*
* @param string $data_type
* The data type to create an object for.
* @param mixed[] $value
* The value to set.
*
* @return \Drupal\Core\TypedData\TypedDataInterface
* The created object.
*/
protected function getTypedData($data_type, $value)
{
$definition = $this->typedDataManager->createDataDefinition($data_type);
$data = $this->typedDataManager->create($definition);
$data->setValue($value);
return $data;
}
示例4: testValidation
/**
* Tests the AllowedValues validation constraint validator.
*
* For testing we define an integer with a set of allowed values.
*/
public function testValidation()
{
// Create a definition that specifies some AllowedValues.
$definition = DataDefinition::create('integer')->addConstraint('AllowedValues', array(1, 2, 3));
// Test the validation.
$typed_data = $this->typedData->create($definition, 1);
$violations = $typed_data->validate();
$this->assertEqual($violations->count(), 0, 'Validation passed for correct value.');
// Test the validation when an invalid value is passed.
$typed_data = $this->typedData->create($definition, 4);
$violations = $typed_data->validate();
$this->assertEqual($violations->count(), 1, 'Validation failed for incorrect value.');
// Make sure the information provided by a violation is correct.
$violation = $violations[0];
$this->assertEqual($violation->getMessage(), t('The value you selected is not a valid choice.'), 'The message for invalid value is correct.');
$this->assertEqual($violation->getRoot(), $typed_data, 'Violation root is correct.');
$this->assertEqual($violation->getInvalidValue(), 4, 'The invalid value is set correctly in the violation.');
}
示例5: assertValidation
/**
* Executes the BundleConstraintValidator test for a given bundle.
*
* @param string|array $bundle
* Bundle/bundles to use as constraint option.
*/
protected function assertValidation($bundle)
{
// Create a typed data definition with a Bundle constraint.
$definition = DataDefinition::create('entity_reference')->addConstraint('Bundle', $bundle);
// Test the validation.
$node = $this->container->get('entity.manager')->getStorage('node')->create(array('type' => 'foo'));
$typed_data = $this->typedData->create($definition, $node);
$violations = $typed_data->validate();
$this->assertEqual($violations->count(), 0, 'Validation passed for correct value.');
// Test the validation when an invalid value is passed.
$page_node = $this->container->get('entity.manager')->getStorage('node')->create(array('type' => 'baz'));
$typed_data = $this->typedData->create($definition, $page_node);
$violations = $typed_data->validate();
$this->assertEqual($violations->count(), 1, 'Validation failed for incorrect value.');
// Make sure the information provided by a violation is correct.
$violation = $violations[0];
$this->assertEqual($violation->getMessage(), t('The entity must be of bundle %bundle.', array('%bundle' => implode(', ', (array) $bundle))), 'The message for invalid value is correct.');
$this->assertEqual($violation->getRoot(), $typed_data, 'Violation root is correct.');
$this->assertEqual($violation->getInvalidValue(), $page_node, 'The invalid value is set correctly in the violation.');
}
示例6: testValidation
/**
* Tests the EntityTypeConstraintValidator.
*/
public function testValidation()
{
// Create a typed data definition with an EntityType constraint.
$entity_type = 'node';
$definition = DataDefinition::create('entity_reference')->setConstraints(array('EntityType' => $entity_type));
// Test the validation.
$node = $this->container->get('entity.manager')->getStorage('node')->create(array('type' => 'page'));
$typed_data = $this->typedData->create($definition, $node);
$violations = $typed_data->validate();
$this->assertEqual($violations->count(), 0, 'Validation passed for correct value.');
// Test the validation when an invalid value (in this case a user entity)
// is passed.
$account = $this->createUser();
$typed_data = $this->typedData->create($definition, $account);
$violations = $typed_data->validate();
$this->assertEqual($violations->count(), 1, 'Validation failed for incorrect value.');
// Make sure the information provided by a violation is correct.
$violation = $violations[0];
$this->assertEqual($violation->getMessage(), t('The entity must be of type %type.', array('%type' => $entity_type)), 'The message for invalid value is correct.');
$this->assertEqual($violation->getRoot(), $typed_data, 'Violation root is correct.');
$this->assertEqual($violation->getInvalidValue(), $account, 'The invalid value is set correctly in the violation.');
}
示例7: testValidation
/**
* Tests the ValidReferenceConstraintValidator.
*/
public function testValidation()
{
// Create a test entity to be referenced.
$entity = $this->createUser();
// By default entity references already have the ValidReference constraint.
$definition = BaseFieldDefinition::create('entity_reference')->setSettings(array('target_type' => 'user'));
$typed_data = $this->typedData->create($definition, array('target_id' => $entity->id()));
$violations = $typed_data->validate();
$this->assertFalse($violations->count(), 'Validation passed for correct value.');
// NULL is also considered a valid reference.
$typed_data = $this->typedData->create($definition, array('target_id' => NULL));
$violations = $typed_data->validate();
$this->assertFalse($violations->count(), 'Validation passed for correct value.');
$typed_data = $this->typedData->create($definition, array('target_id' => $entity->id()));
// Delete the referenced entity.
$entity->delete();
$violations = $typed_data->validate();
$this->assertTrue($violations->count(), 'Validation failed for incorrect value.');
// Make sure the information provided by a violation is correct.
$violation = $violations[0];
$this->assertEqual($violation->getMessage(), t('The referenced entity (%type: %id) does not exist.', array('%type' => 'user', '%id' => $entity->id())), 'The message for invalid value is correct.');
$this->assertEqual($violation->getRoot(), $typed_data, 'Violation root is correct.');
}
示例8: setupTypedData
/**
* Setups a typed data object used for test purposes.
*
* @param array $tree
* An array of value, constraints and properties.
*
* @return \Drupal\Core\TypedData\TypedDataInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected function setupTypedData(array $tree, $name = '')
{
$callback = function ($value, ExecutionContextInterface $context) {
$context->addViolation('violation: ' . (is_array($value) ? count($value) : $value));
};
$tree += ['constraints' => []];
if (isset($tree['properties'])) {
$map_data_definition = MapDataDefinition::create();
$map_data_definition->addConstraint('Callback', ['callback' => $callback]);
foreach ($tree['properties'] as $property_name => $property) {
$sub_typed_data = $this->setupTypedData($property, $property_name);
$map_data_definition->setPropertyDefinition($property_name, $sub_typed_data->getDataDefinition());
}
$typed_data = $this->typedDataManager->create($map_data_definition, $tree['value'], $name);
} else {
/** @var \Drupal\Core\TypedData\TypedDataInterface $typed_data */
$typed_data = $this->typedDataManager->create(DataDefinition::create('string')->addConstraint('Callback', ['callback' => $callback]), $tree['value'], $name);
}
return $typed_data;
}
示例9: build
/**
* {@inheritdoc}
*/
public function build()
{
// Create a temporary node object to which our fake field value can be
// added.
$node = Node::create(array('type' => '_entity_embed'));
$definition = $this->getFieldDefinition();
/* @var \Drupal\Core\Field\FieldItemListInterface $items $items */
// Create a field item list object, 1 is the value, array('target_id' => 1)
// would work too, or multiple values. 1 is passed down from the list to the
// field item, which knows that an integer is the ID.
$items = $this->typedDataManager->create($definition, $this->getFieldValue($definition), $definition->getName(), $node->getTypedData());
// Prepare, expects an array of items, keyed by parent entity ID.
$formatter = $this->getFieldFormatter();
$formatter->prepareView(array($node->id() => $items));
$build = $formatter->viewElements($items, $this->getLangcode());
// For some reason $build[0]['#printed'] is TRUE, which means it will fail
// to render later. So for now we manually fix that.
// @todo Investigate why this is needed.
show($build[0]);
return $build[0];
}
示例10: testTypedDataValidation
/**
* Tests typed data validation.
*/
public function testTypedDataValidation()
{
$definition = DataDefinition::create('integer')->setConstraints(array('Range' => array('min' => 5)));
$violations = $this->typedDataManager->create($definition, 10)->validate();
$this->assertEqual($violations->count(), 0);
$integer = $this->typedDataManager->create($definition, 1);
$violations = $integer->validate();
$this->assertEqual($violations->count(), 1);
// Test translating violation messages.
$message = t('This value should be %limit or more.', array('%limit' => 5));
$this->assertEqual($violations[0]->getMessage(), $message, 'Translated violation message retrieved.');
$this->assertEqual($violations[0]->getPropertyPath(), '');
$this->assertIdentical($violations[0]->getRoot(), $integer, 'Root object returned.');
// Test translating violation messages when pluralization is used.
$definition = DataDefinition::create('string')->setConstraints(array('Length' => array('min' => 10)));
$violations = $this->typedDataManager->create($definition, "short")->validate();
$this->assertEqual($violations->count(), 1);
$message = t('This value is too short. It should have %limit characters or more.', array('%limit' => 10));
$this->assertEqual($violations[0]->getMessage(), $message, 'Translated violation message retrieved.');
// Test having multiple violations.
$definition = DataDefinition::create('integer')->setConstraints(array('Range' => array('min' => 5), 'Null' => array()));
$violations = $this->typedDataManager->create($definition, 10)->validate();
$this->assertEqual($violations->count(), 1);
$violations = $this->typedDataManager->create($definition, 1)->validate();
$this->assertEqual($violations->count(), 2);
// Test validating property containers and make sure the NotNull and Null
// constraints work with typed data containers.
$definition = BaseFieldDefinition::create('integer')->setConstraints(array('NotNull' => array()));
$field_item = $this->typedDataManager->create($definition, array('value' => 10));
$violations = $field_item->validate();
$this->assertEqual($violations->count(), 0);
$field_item = $this->typedDataManager->create($definition, array('value' => 'no integer'));
$violations = $field_item->validate();
$this->assertEqual($violations->count(), 1);
$this->assertEqual($violations[0]->getPropertyPath(), '0.value');
// Test that the field item may not be empty.
$field_item = $this->typedDataManager->create($definition);
$violations = $field_item->validate();
$this->assertEqual($violations->count(), 1);
// Test the Null constraint with typed data containers.
$definition = BaseFieldDefinition::create('float')->setConstraints(array('Null' => array()));
$field_item = $this->typedDataManager->create($definition, array('value' => 11.5));
$violations = $field_item->validate();
$this->assertEqual($violations->count(), 1);
$field_item = $this->typedDataManager->create($definition);
$violations = $field_item->validate();
$this->assertEqual($violations->count(), 0);
// Test getting constraint definitions by type.
$definitions = $this->typedDataManager->getValidationConstraintManager()->getDefinitionsByType('entity');
$this->assertTrue(isset($definitions['EntityType']), 'Constraint plugin found for type entity.');
$this->assertTrue(isset($definitions['Null']), 'Constraint plugin found for type entity.');
$this->assertTrue(isset($definitions['NotNull']), 'Constraint plugin found for type entity.');
$definitions = $this->typedDataManager->getValidationConstraintManager()->getDefinitionsByType('string');
$this->assertFalse(isset($definitions['EntityType']), 'Constraint plugin not found for type string.');
$this->assertTrue(isset($definitions['Null']), 'Constraint plugin found for type string.');
$this->assertTrue(isset($definitions['NotNull']), 'Constraint plugin found for type string.');
// Test automatic 'required' validation.
$definition = DataDefinition::create('integer')->setRequired(TRUE);
$violations = $this->typedDataManager->create($definition)->validate();
$this->assertEqual($violations->count(), 1);
$violations = $this->typedDataManager->create($definition, 0)->validate();
$this->assertEqual($violations->count(), 0);
// Test validating a list of a values and make sure property paths starting
// with "0" are created.
$definition = BaseFieldDefinition::create('integer');
$violations = $this->typedDataManager->create($definition, array(array('value' => 10)))->validate();
$this->assertEqual($violations->count(), 0);
$violations = $this->typedDataManager->create($definition, array(array('value' => 'string')))->validate();
$this->assertEqual($violations->count(), 1);
$this->assertEqual($violations[0]->getInvalidValue(), 'string');
$this->assertIdentical($violations[0]->getPropertyPath(), '0.value');
}