-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from openeuropa/OPENEUROPA-1905
OPENEUROPA-1905: Create field type plugin for entity revisions.
- Loading branch information
Showing
9 changed files
with
297 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
/** | ||
* @file | ||
* Entity versions module. | ||
* Entity version module. | ||
*/ | ||
|
||
declare(strict_types = 1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Drupal\entity_version\Plugin\Field\FieldType; | ||
|
||
use Drupal\Core\Field\FieldItemBase; | ||
use Drupal\Core\Field\FieldStorageDefinitionInterface; | ||
use Drupal\Core\TypedData\DataDefinition; | ||
|
||
/** | ||
* Plugin implementation of the 'entity_version' field type. | ||
* | ||
* @FieldType( | ||
* id = "entity_version", | ||
* label = @Translation("Entity version"), | ||
* module = "entity_version", | ||
* description = @Translation("Stores the version of the entity.") | ||
* ) | ||
*/ | ||
class EntityVersionItem extends FieldItemBase implements EntityVersionItemInterface { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function schema(FieldStorageDefinitionInterface $field_definition) { | ||
return [ | ||
'columns' => [ | ||
'major' => [ | ||
'type' => 'int', | ||
], | ||
'minor' => [ | ||
'type' => 'int', | ||
], | ||
'patch' => [ | ||
'type' => 'int', | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isEmpty() { | ||
// We consider the field empty if either of these properties left empty. | ||
$major = $this->get('major')->getValue(); | ||
$minor = $this->get('minor')->getValue(); | ||
$patch = $this->get('patch')->getValue(); | ||
|
||
return $major === NULL || $major === '' || $minor === NULL || $minor === '' || $patch === NULL || $patch === ''; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { | ||
$properties['major'] = DataDefinition::create('integer') | ||
->setLabel(t('Major number')); | ||
$properties['minor'] = DataDefinition::create('integer') | ||
->setLabel(t('Minor number')); | ||
$properties['patch'] = DataDefinition::create('integer') | ||
->setLabel(t('Patch number')); | ||
|
||
return $properties; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function applyDefaultValue($notify = TRUE) { | ||
parent::applyDefaultValue($notify); | ||
// Created fields default to zero. | ||
$this->setValue( | ||
[ | ||
'major' => 0, | ||
'minor' => 0, | ||
'patch' => 0, | ||
], | ||
$notify | ||
); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function increase(string $category): void { | ||
$value = $this->get($category)->getValue(); | ||
$this->set($category, ($value + 1)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function decrease(string $category): void { | ||
$value = $this->get($category)->getValue(); | ||
$this->set($category, empty($value) ? 0 : ($value - 1)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function reset(string $category): void { | ||
$this->set($category, 0); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Drupal\entity_version\Plugin\Field\FieldType; | ||
|
||
use Drupal\Core\Field\FieldItemInterface; | ||
|
||
/** | ||
* Defines an interface for the entity_version field item. | ||
*/ | ||
interface EntityVersionItemInterface extends FieldItemInterface { | ||
|
||
/** | ||
* Increase the given version number category. | ||
* | ||
* @param string $category | ||
* The version number category. | ||
*/ | ||
public function increase(string $category); | ||
|
||
/** | ||
* Decrease the given version number category. | ||
* | ||
* @param string $category | ||
* The version number category. | ||
*/ | ||
public function decrease(string $category); | ||
|
||
/** | ||
* Reset the given version number category to zero. | ||
* | ||
* @param string $category | ||
* The version number category. | ||
*/ | ||
public function reset(string $category); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?php | ||
|
||
namespace Drupal\Tests\entity_version\Kernel; | ||
|
||
use Drupal\entity_test\Entity\EntityTest; | ||
use Drupal\field\Entity\FieldConfig; | ||
use Drupal\field\Entity\FieldStorageConfig; | ||
use Drupal\Tests\field\Kernel\FieldKernelTestBase; | ||
|
||
/** | ||
* Tests the new entity API for the entity version field type. | ||
*/ | ||
class EntityVersionItemTest extends FieldKernelTestBase { | ||
|
||
/** | ||
* Modules to enable. | ||
* | ||
* @var array | ||
*/ | ||
public static $modules = ['entity_version']; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function setUp() { | ||
parent::setUp(); | ||
|
||
// Create a generic field for validation. | ||
FieldStorageConfig::create([ | ||
'field_name' => 'version', | ||
'entity_type' => 'entity_test', | ||
'type' => 'entity_version', | ||
])->save(); | ||
FieldConfig::create([ | ||
'entity_type' => 'entity_test', | ||
'field_name' => 'version', | ||
'bundle' => 'entity_test', | ||
])->save(); | ||
} | ||
|
||
/** | ||
* Tests using entity fields of the entity version field type. | ||
*/ | ||
public function testEntityVersionItem() { | ||
// Create entity. | ||
$entity = EntityTest::create(); | ||
$entity->save(); | ||
|
||
// Verify that the field default value is zero. | ||
$id = $entity->id(); | ||
$entity = EntityTest::load($id); | ||
$this->assertEqual($entity->version->major, 0); | ||
$this->assertEqual($entity->version->minor, 0); | ||
$this->assertEqual($entity->version->patch, 0); | ||
|
||
// Use the field type method to increase the values. | ||
$entity->version->first()->increase('major'); | ||
$entity->version->first()->increase('minor'); | ||
$entity->version->first()->increase('patch'); | ||
$entity->save(); | ||
|
||
// Verify that the field value is changed. | ||
$id = $entity->id(); | ||
$entity = EntityTest::load($id); | ||
$this->assertEqual($entity->version->major, 1); | ||
$this->assertEqual($entity->version->minor, 1); | ||
$this->assertEqual($entity->version->patch, 1); | ||
|
||
// Use the field type method to decrease the major number. | ||
$entity->version->first()->decrease('major'); | ||
$entity->save(); | ||
|
||
// Verify that the field value is changed. | ||
$id = $entity->id(); | ||
$entity = EntityTest::load($id); | ||
$this->assertEqual($entity->version->major, 0); | ||
$this->assertEqual($entity->version->minor, 1); | ||
$this->assertEqual($entity->version->patch, 1); | ||
|
||
// Use the field type method to decrease values. | ||
$entity->version->first()->decrease('minor'); | ||
$entity->version->first()->decrease('patch'); | ||
$entity->save(); | ||
|
||
// Verify that the field value is changed. | ||
$id = $entity->id(); | ||
$entity = EntityTest::load($id); | ||
$this->assertEqual($entity->version->major, 0); | ||
$this->assertEqual($entity->version->minor, 0); | ||
$this->assertEqual($entity->version->patch, 0); | ||
|
||
// Use the field type method to increase the values. | ||
$entity->version->first()->increase('major'); | ||
$entity->version->first()->increase('minor'); | ||
$entity->version->first()->increase('patch'); | ||
$entity->save(); | ||
|
||
// Use the field type method to reset values to zero. | ||
$entity->version->first()->reset('major'); | ||
$entity->version->first()->reset('minor'); | ||
$entity->version->first()->reset('patch'); | ||
$entity->save(); | ||
|
||
// Verify that the field value is reset. | ||
$id = $entity->id(); | ||
$entity = EntityTest::load($id); | ||
$this->assertEqual($entity->version->major, 0); | ||
$this->assertEqual($entity->version->minor, 0); | ||
$this->assertEqual($entity->version->patch, 0); | ||
|
||
// Use the field type method to decrease values. | ||
$entity->version->first()->decrease('major'); | ||
$entity->version->first()->decrease('minor'); | ||
$entity->version->first()->decrease('patch'); | ||
$entity->save(); | ||
|
||
// Verify that the field value has not changed. | ||
$id = $entity->id(); | ||
$entity = EntityTest::load($id); | ||
$this->assertEqual($entity->version->major, 0); | ||
$this->assertEqual($entity->version->minor, 0); | ||
$this->assertEqual($entity->version->patch, 0); | ||
|
||
// Use the field type method to increase patch. | ||
$entity->version->first()->increase('patch'); | ||
$entity->save(); | ||
|
||
// Verify that the field value is changed. | ||
$id = $entity->id(); | ||
$entity = EntityTest::load($id); | ||
$this->assertEqual($entity->version->major, 0); | ||
$this->assertEqual($entity->version->minor, 0); | ||
$this->assertEqual($entity->version->patch, 1); | ||
} | ||
|
||
} |