Skip to content

Commit

Permalink
Naive implementation of increment to test atomicity #2
Browse files Browse the repository at this point in the history
  • Loading branch information
colorfield committed Sep 24, 2016
1 parent 52ee72c commit a920ab4
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/Plugin/Field/FieldType/SerialItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/**
* Plugin implementation of the 'serial' field type.
* @todo should not be translatable, by default
*
* @FieldType(
* id = "serial",
Expand Down Expand Up @@ -74,8 +75,24 @@ public function preSave() {
* @return int
*/
private function getSerial() {
$serial = 1;
// @todo implement
// @todo use as a service
$serial = 0;
$entity = $this->getEntity();
// Does not applies if the node is not new.
if($entity->isNew()) {
// Let's start a first naive implementation
// by querying the amount of entities from this entity type + bundle.
$entity_type_id = $entity->getEntityTypeId(); // e.g. node
$entity_bundle = $entity->bundle(); // e.g. article
// @todo use DI / container
$query = \Drupal::entityQuery($entity_type_id);
$query->condition('type', $entity_bundle);
$result = $query->execute();
$serial = count($result) + 1;
// If we continue on the previous atomicity model, we will need the field instance
// @see https://github.com/r-daneelolivaw/serial/issues/2
//$field_definition = $this->getFieldDefinition();
}
return $serial;
}

Expand Down

0 comments on commit a920ab4

Please sign in to comment.