Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2903261 Add a normalizer so that the value of the serial field appea… #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions serial.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ services:
serial.sql_storage:
class: Drupal\serial\SerialSQLStorage
arguments: ['@entity.query', '@entity_type.manager']
serializer.normalizer.serial_field_item:
class: \Drupal\serial\Normalizer\SerialItemFieldItemNormalizer
tags:
- {name: normalizer, priority: 50 }
30 changes: 30 additions & 0 deletions src/Normalizer/SerialItemFieldItemNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Drupal\serial\Normalizer;

use Drupal\serial\Plugin\Field\FieldType\SerialItem;
use Drupal\serialization\Normalizer\ComplexDataNormalizer;

/**
* Class SerialItemFieldItemNormalizer.
*
* @package Drupal\serial\Normalizer
*/
class SerialItemFieldItemNormalizer extends ComplexDataNormalizer {

/**
* The interface or class that this Normalizer supports.
*
* @var string
*/
protected $supportedInterfaceOrClass = SerialItem::class;

/**
* {@inheritdoc}
*/
public function normalize($object, $format = NULL, array $context = []) {
/** @var \Drupal\serial\Plugin\Field\FieldType\SerialItem $object */
return $object->getValue();
}

}
15 changes: 15 additions & 0 deletions src/Plugin/Field/FieldType/SerialItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ public function preSave() {
}
}

/**
* {@inheritdoc}
*/
public function getValue() {
// Update the values and return them.
foreach ($this->properties as $name => $property) {
$value = $property->getValue();
// Only write NULL values if the whole map is not NULL.
if (isset($this->values) || isset($value)) {
$this->values[$name] = $value;
}
}
return $this->values;
}

/**
* Gets the serial for this entity type, bundle, field instance.
*
Expand Down