本文整理汇总了PHP中Drupal\Core\Field\BaseFieldDefinition类的典型用法代码示例。如果您正苦于以下问题:PHP BaseFieldDefinition类的具体用法?PHP BaseFieldDefinition怎么用?PHP BaseFieldDefinition使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BaseFieldDefinition类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: createFromBaseFieldDefinition
/**
* Creates a base field override object.
*
* @param \Drupal\Core\Field\BaseFieldDefinition $base_field_definition
* The base field definition to override.
* @param string $bundle
* The bundle to which the override applies.
*
* @return \Drupal\Core\Field\Entity\BaseFieldOverride
* A new base field override object.
*/
public static function createFromBaseFieldDefinition(BaseFieldDefinition $base_field_definition, $bundle)
{
$values = $base_field_definition->toArray();
$values['bundle'] = $bundle;
$values['baseFieldDefinition'] = $base_field_definition;
return \Drupal::entityManager()->getStorage('base_field_override')->create($values);
}
示例2: testFields
/**
* Tests deriving metadata about fields.
*/
public function testFields()
{
$field_definition = BaseFieldDefinition::create('integer');
// Fields are lists of complex data.
$this->assertTrue($field_definition instanceof ListDataDefinitionInterface);
$this->assertFalse($field_definition instanceof ComplexDataDefinitionInterface);
$field_item_definition = $field_definition->getItemDefinition();
$this->assertFalse($field_item_definition instanceof ListDataDefinitionInterface);
$this->assertTrue($field_item_definition instanceof ComplexDataDefinitionInterface);
// Derive metadata about field item properties.
$this->assertEqual(array_keys($field_item_definition->getPropertyDefinitions()), array('value'));
$this->assertEqual($field_item_definition->getPropertyDefinition('value')->getDataType(), 'integer');
$this->assertEqual($field_item_definition->getMainPropertyName(), 'value');
$this->assertNull($field_item_definition->getPropertyDefinition('invalid'));
// Test accessing field item property metadata via the field definition.
$this->assertTrue($field_definition instanceof FieldDefinitionInterface);
$this->assertEqual(array_keys($field_definition->getPropertyDefinitions()), array('value'));
$this->assertEqual($field_definition->getPropertyDefinition('value')->getDataType(), 'integer');
$this->assertEqual($field_definition->getMainPropertyName(), 'value');
$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->assertFalse($field_item instanceof ListDataDefinitionInterface);
$this->assertTrue($field_item instanceof ComplexDataDefinitionInterface);
// Comparison should ignore the internal static cache, so compare the
// serialized objects instead.
$this->assertEqual(serialize($field_item_definition), serialize($field_item));
$field_definition2 = $this->typedDataManager->createListDataDefinition('field_item:integer');
$this->assertTrue($field_definition2 instanceof ListDataDefinitionInterface);
$this->assertFalse($field_definition2 instanceof ComplexDataDefinitionInterface);
$this->assertEqual(serialize($field_definition), serialize($field_definition2));
}
示例3: testTestItem
/**
* Tests using entity fields of the field field type.
*/
public function testTestItem()
{
// Verify entity creation.
$entity = EntityTest::create();
$value = rand(1, 10);
$entity->field_test = $value;
$entity->name->value = $this->randomMachineName();
$entity->save();
// Verify entity has been created properly.
$id = $entity->id();
$entity = entity_load('entity_test', $id);
$this->assertTrue($entity->{$this->fieldName} instanceof FieldItemListInterface, 'Field implements interface.');
$this->assertTrue($entity->{$this->fieldName}[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEqual($entity->{$this->fieldName}->value, $value);
$this->assertEqual($entity->{$this->fieldName}[0]->value, $value);
// Verify changing the field value.
$new_value = rand(1, 10);
$entity->field_test->value = $new_value;
$this->assertEqual($entity->{$this->fieldName}->value, $new_value);
// Read changed entity and assert changed values.
$entity->save();
$entity = entity_load('entity_test', $id);
$this->assertEqual($entity->{$this->fieldName}->value, $new_value);
// Test the schema for this field type.
$expected_schema = array('columns' => array('value' => array('type' => 'int', 'size' => 'medium')), 'unique keys' => array(), 'indexes' => array('value' => array('value')), 'foreign keys' => array());
$field_schema = BaseFieldDefinition::create('test_field')->getSchema();
$this->assertEqual($field_schema, $expected_schema);
}
示例4: revisionLogBaseFieldDefinitions
/**
* Provides revision-related base field definitions for an entity type.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
*
* @return \Drupal\Core\Field\FieldDefinitionInterface[]
* An array of base field definitions for the entity type, keyed by field
* name.
*
* @see \Drupal\Core\Entity\FieldableEntityInterface::baseFieldDefinitions()
*/
public static function revisionLogBaseFieldDefinitions(EntityTypeInterface $entity_type)
{
$fields['revision_created'] = BaseFieldDefinition::create('created')->setLabel(t('Revision create time'))->setDescription(t('The time that the current revision was created.'))->setRevisionable(TRUE);
$fields['revision_user'] = BaseFieldDefinition::create('entity_reference')->setLabel(t('Revision user'))->setDescription(t('The user ID of the author of the current revision.'))->setSetting('target_type', 'user')->setRevisionable(TRUE);
$fields['revision_log_message'] = BaseFieldDefinition::create('string_long')->setLabel(t('Revision log message'))->setDescription(t('Briefly describe the changes you have made.'))->setRevisionable(TRUE)->setDefaultValue('')->setDisplayOptions('form', ['type' => 'string_textarea', 'weight' => 25, 'settings' => ['rows' => 4]]);
return $fields;
}
示例5: baseFieldDefinitions
public static function baseFieldDefinitions(EntityTypeInterface $entity_type)
{
$fields['gid'] = BaseFieldDefinition::create('integer')->setLabel(t('Gallery ID'))->setDescription(t('The gallery ID.'))->setReadOnly(TRUE)->setSetting('unsigned', TRUE);
$fields['title'] = BaseFieldDefinition::create('string')->setLabel(t('Title'))->setRequired(TRUE)->setSetting('max_length', 255)->setDisplayOptions('view', array('label' => 'hidden', 'type' => 'string', 'weight' => -5))->setDisplayOptions('form', array('type' => 'string_textfield', 'weight' => -5))->setDisplayConfigurable('form', TRUE);
$fields['description'] = BaseFieldDefinition::create('text')->setLabel(t('Description'))->setRequired(TRUE)->setDisplayOptions('view', array('label' => 'hidden', 'type' => 'text', 'weight' => 2))->setDisplayOptions('form', array('type' => 'text_textfield', 'weight' => 2))->setDisplayConfigurable('form', TRUE);
return $fields;
}
示例6: baseFieldDefinitions
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type)
{
$fields['id'] = BaseFieldDefinition::create('integer')->setLabel(t('Slider ID'))->setDescription(t('The slider ID.'))->setReadOnly(TRUE)->setSetting('unsigned', TRUE);
$fields['name'] = BaseFieldDefinition::create('string')->setLabel(t('Name'))->setTranslatable(TRUE)->setDisplayOptions('view', array('label' => 'above', 'type' => 'string', 'weight' => -5))->setDisplayOptions('form', array('type' => 'string_textfield', 'weight' => -5))->setDisplayConfigurable('view', TRUE)->setDisplayConfigurable('form', TRUE);
$fields['description'] = BaseFieldDefinition::create('string')->setLabel(t('Slider description'))->setTranslatable(TRUE)->setDescription(t('A brief description of your slider.'))->setDisplayOptions('view', array('label' => 'above', 'type' => 'string', 'weight' => -2))->setDisplayOptions('form', array('type' => 'string_textfield', 'weight' => -2))->setDisplayConfigurable('view', TRUE)->setDisplayConfigurable('form', TRUE);
$fields['langcode'] = BaseFieldDefinition::create('language')->setLabel(t('Language'))->setDescription(t('The node language code.'))->setTranslatable(TRUE)->setDisplayOptions('view', array('type' => 'hidden'))->setDisplayOptions('form', array('type' => 'language_select', 'weight' => 2));
return $fields;
}
示例7: getFieldDefinition
/**
* Gets the field definition.
*
* A View works on an entity type across bundles, and thus only has access to
* field storage definitions. In order to be able to use widgets and
* formatters, we create a generic field definition out of that storage
* definition.
*
* @see BaseFieldDefinition::createFromFieldStorageDefinition()
*
* @return \Drupal\Core\Field\FieldDefinitionInterface
* The field definition used by this handler.
*/
protected function getFieldDefinition()
{
if (!$this->fieldDefinition) {
$field_storage_config = $this->getFieldStorageDefinition();
$this->fieldDefinition = BaseFieldDefinition::createFromFieldStorageDefinition($field_storage_config);
}
return $this->fieldDefinition;
}
示例8: baseFieldDefinitions
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type)
{
$fields = parent::baseFieldDefinitions($entity_type);
$fields['test_no_display'] = BaseFieldDefinition::create('text')->setLabel(t('Field with no display'));
$fields['test_display_configurable'] = BaseFieldDefinition::create('text')->setLabel(t('Field with configurable display'))->setDisplayOptions('view', array('type' => 'text_default', 'weight' => 10))->setDisplayConfigurable('view', TRUE)->setDisplayOptions('form', array('type' => 'text_textfield', 'weight' => 10))->setDisplayConfigurable('form', TRUE);
$fields['test_display_non_configurable'] = BaseFieldDefinition::create('text')->setLabel(t('Field with non-configurable display'))->setDisplayOptions('view', array('type' => 'text_default', 'weight' => 11))->setDisplayOptions('form', array('type' => 'text_textfield', 'weight' => 11));
return $fields;
}
示例9: baseFieldDefinitions
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type)
{
$fields = [];
$fields += static::entityKeysBaseFieldDefinitions($entity_type);
$fields += static::entityRevisionLogBaseFieldDefinitions();
$fields['name'] = BaseFieldDefinition::create('string')->setLabel('Name')->setRevisionable(TRUE)->setDisplayOptions('view', ['label' => 'hidden', 'type' => 'string', 'weight' => -5]);
return $fields;
}
示例10: baseFieldDefinitions
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type)
{
$fields = parent::baseFieldDefinitions($entity_type);
$fields['parent_id'] = BaseFieldDefinition::create('string')->setLabel(t('Parent ID'))->setDescription(t('The ID of the parent entity of which this entity is referenced.'));
$fields['parent_type'] = BaseFieldDefinition::create('string')->setLabel(t('Parent type'))->setDescription(t('The entity parent type to which this entity is referenced.'));
$fields['parent_field_name'] = BaseFieldDefinition::create('string')->setLabel(t('Parent field name'))->setDescription(t('The entity parent field name to which this entity is referenced.'));
return $fields;
}
示例11: baseFieldDefinitions
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type)
{
$fields = parent::baseFieldDefinitions($entity_type);
$fields['name'] = BaseFieldDefinition::create('string')->setLabel(t('Name'))->setDescription(t('The name of the test entity.'))->setTranslatable(TRUE)->setSetting('max_length', 32)->setDisplayOptions('view', array('label' => 'hidden', 'type' => 'string', 'weight' => -5))->setDisplayOptions('form', array('type' => 'string_textfield', 'weight' => -5));
$fields['created'] = BaseFieldDefinition::create('created')->setLabel(t('Authored on'))->setDescription(t('Time the entity was created'))->setTranslatable(TRUE);
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')->setLabel(t('User ID'))->setDescription(t('The ID of the associated user.'))->setSetting('target_type', 'user')->setSetting('handler', 'default')->setDefaultValue(array(0 => array('target_id' => 1)))->setTranslatable(TRUE)->setDisplayOptions('form', array('type' => 'entity_reference_autocomplete', 'weight' => -1, 'settings' => array('match_operator' => 'CONTAINS', 'size' => '60', 'placeholder' => '')));
return $fields;
}
示例12: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
// Setup some fields for entity_test_extra to create.
$definitions['multivalued_base_field'] = BaseFieldDefinition::create('string')->setName('multivalued_base_field')->setTargetEntityTypeId('entity_test_mulrev')->setTargetBundle('entity_test_mulrev')->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$this->state->set('entity_test_mulrev.additional_base_field_definitions', $definitions);
$this->entityManager->clearCachedDefinitions();
$this->tableMapping = $this->entityManager->getStorage('entity_test_mulrev')->getTableMapping();
}
示例13: baseFieldDefinitions
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type)
{
$fields = parent::baseFieldDefinitions($entity_type);
$fields['test_no_display'] = BaseFieldDefinition::create('text')->setLabel(t('Field with no display'));
$fields['test_display_configurable'] = BaseFieldDefinition::create('text')->setLabel(t('Field with configurable display'))->setDisplayOptions('view', array('type' => 'text_default', 'weight' => 10))->setDisplayConfigurable('view', TRUE)->setDisplayOptions('form', array('type' => 'text_textfield', 'weight' => 10))->setDisplayConfigurable('form', TRUE);
$fields['test_display_non_configurable'] = BaseFieldDefinition::create('text')->setLabel(t('Field with non-configurable display'))->setDisplayOptions('view', array('type' => 'text_default', 'weight' => 11))->setDisplayOptions('form', array('type' => 'text_textfield', 'weight' => 11));
$fields['test_display_multiple'] = BaseFieldDefinition::create('text')->setLabel(t('A field with multiple values'))->setCardinality(FieldStorageDefinition::CARDINALITY_UNLIMITED)->setDisplayOptions('view', array('type' => 'text_default', 'weight' => 12))->setDisplayOptions('form', array('type' => 'text_textfield', 'weight' => 12));
return $fields;
}
示例14: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$field_definition = BaseFieldDefinition::create('plugin:plugin_test_helper_mock');
/** @var \Drupal\Core\Field\FieldItemListInterface $field_item_list */
$field_item_list = \Drupal::typedDataManager()->create($field_definition);
$field_item_list->appendItem();
$this->fieldItem = $field_item_list->first();
}
示例15: baseFieldDefinitions
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type)
{
$fields = parent::baseFieldDefinitions($entity_type);
$fields['revision_id'] = BaseFieldDefinition::create('integer')->setLabel(t('Revision ID'))->setDescription(t('The version id of the test entity.'))->setReadOnly(TRUE)->setSetting('unsigned', TRUE);
$fields['langcode']->setRevisionable(TRUE);
$fields['name']->setRevisionable(TRUE);
$fields['user_id']->setRevisionable(TRUE);
return $fields;
}