class FieldTestItem
Same name and namespace in other branches
- 11.x core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/FieldTestItem.php \Drupal\entity_test\Plugin\Field\FieldType\FieldTestItem
- 10 core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/FieldTestItem.php \Drupal\entity_test\Plugin\Field\FieldType\FieldTestItem
- 8.9.x core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/FieldTestItem.php \Drupal\entity_test\Plugin\Field\FieldType\FieldTestItem
- main core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/FieldTestItem.php \Drupal\entity_test\Plugin\Field\FieldType\FieldTestItem
Defines the 'field_test' entity field type.
Plugin annotation
@FieldType(
id = "field_test",
label = @Translation("Test field item"),
description = @Translation("A field containing a plain string value."),
category = @Translation("Field"),
)
Hierarchy
- class \Drupal\Core\TypedData\TypedData implements \Drupal\Core\TypedData\TypedDataInterface, \Drupal\Component\Plugin\PluginInspectionInterface uses \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\TypedData\TypedDataTrait
- class \Drupal\Core\TypedData\Plugin\DataType\Map implements \Drupal\Core\TypedData\Plugin\DataType\IteratorAggregate, \Drupal\Core\TypedData\ComplexDataInterface extends \Drupal\Core\TypedData\TypedData
- class \Drupal\Core\Field\FieldItemBase implements \Drupal\Core\Field\FieldItemInterface extends \Drupal\Core\TypedData\Plugin\DataType\Map
- class \Drupal\entity_test\Plugin\Field\FieldType\FieldTestItem extends \Drupal\Core\Field\FieldItemBase
- class \Drupal\Core\Field\FieldItemBase implements \Drupal\Core\Field\FieldItemInterface extends \Drupal\Core\TypedData\Plugin\DataType\Map
- class \Drupal\Core\TypedData\Plugin\DataType\Map implements \Drupal\Core\TypedData\Plugin\DataType\IteratorAggregate, \Drupal\Core\TypedData\ComplexDataInterface extends \Drupal\Core\TypedData\TypedData
Expanded class hierarchy of FieldTestItem
File
-
core/
modules/ system/ tests/ modules/ entity_test/ src/ Plugin/ Field/ FieldType/ FieldTestItem.php, line 22
Namespace
Drupal\entity_test\Plugin\Field\FieldTypeView source
class FieldTestItem extends FieldItemBase {
/**
* Counts how many times all items of this type are saved.
*
* @var int
*/
protected static $counter = [];
/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('string')->setLabel(new TranslatableMarkup('Test value'))
->setRequired(TRUE);
return $properties;
}
/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'varchar',
'length' => 255,
],
],
];
}
/**
* {@inheritdoc}
*/
public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) {
parent::__construct($definition, $name, $parent);
$name = $this->getFieldDefinition()
->getName();
static::$counter[$name] = 0;
}
/**
* {@inheritdoc}
*/
public function preSave() {
$name = $this->getFieldDefinition()
->getName();
static::$counter[$name]++;
// Overwrite the field value unless it is going to be overridden, in which
// case its final value will already be different from the current one.
if (!$this->getEntity()
->isNew() && !$this->mustResave()) {
$this->setValue('overwritten');
}
}
/**
* {@inheritdoc}
*/
public function postSave($update) {
// Determine whether the field value should be rewritten to the storage. We
// always rewrite on create as we need to store a value including the entity
// id.
$resave = !$update || $this->mustResave();
if ($resave) {
$entity = $this->getEntity();
$definition = $this->getFieldDefinition();
$name = $definition->getName();
$value = 'field_test:' . $name . ':' . $entity->id() . ':' . static::$counter[$name];
$this->setValue($value);
}
return $resave;
}
/**
* Checks whether the field item value should be resaved.
*
* @return bool
* TRUE if the item should be resaved, FALSE otherwise.
*/
protected function mustResave() {
return $this->getValue()['value'] == 'resave';
}
/**
* {@inheritdoc}
*/
public function delete() {
parent::delete();
$deleted_languages = \Drupal::state()->get('entity_test.delete.' . $this->getFieldDefinition()
->getName(), []);
$deleted_languages[] = $this->getLangcode();
\Drupal::state()->set('entity_test.delete.' . $this->getFieldDefinition()
->getName(), $deleted_languages);
}
}
Members
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.