Skip to content
Snippets Groups Projects
Commit 48f35acb authored by Matt Glaman's avatar Matt Glaman Committed by Björn Brala
Browse files

Issue #3439465 by mglaman, amangrover90, e0ipso, bbrala: List field enhancer...

Issue #3439465 by mglaman, amangrover90, e0ipso, bbrala: List field enhancer for displaying key and value
parent a7bc592a
Branches
Tags 8.x-3.25-beta2
1 merge request!46Add ListFieldEnhancer
Pipeline #181511 passed with warnings
<?php
namespace Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer;
use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerBase;
use Drupal\options\Plugin\Field\FieldType\ListItemBase;
use Shaper\Util\Context;
/**
* Perform additional manipulations to list fields.
*
* @ResourceFieldEnhancer(
* id = "list",
* label = @Translation("List Field"),
* description = @Translation("Formats a list field based on labels and values.")
* )
*/
class ListFieldEnhancer extends ResourceFieldEnhancerBase {
/**
* {@inheritDoc}
*/
protected function doTransform($data, Context $context) {
return is_array($data) ? array_column($data, 'value') : $data;
}
/**
* {@inheritDoc}
*/
protected function doUndoTransform($data, Context $context) {
$field_context = $context->offsetGet('field_item_object');
assert($field_context instanceof ListItemBase);
$options = $field_context->getPossibleOptions();
$reformat = static function ($input) use ($options) {
return [
'value' => $input,
'label' => $options[(string) $input] ?? '',
];
};
return is_array($data) ? array_map($reformat, $data) : $reformat($data);
}
/**
* {@inheritDoc}
*/
public function getOutputJsonSchema(): array {
return [
'type' => 'array',
'items' => [
'type' => 'object',
'properties' => [
'value' => [
'anyOf' => [
['type' => 'string'],
['type' => 'number'],
['type' => 'null'],
],
],
'label' => [
'anOf' => [
['type' => 'string'],
['type' => 'null'],
],
],
],
],
];
}
}
<?php
declare(strict_types=1);
namespace Drupal\Tests\jsonapi_extras\Kernel\Plugin\jsonapi\FieldEnhancer;
use Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer\ListFieldEnhancer;
use Drupal\KernelTests\KernelTestBase;
use Drupal\options\Plugin\Field\FieldType\ListIntegerItem;
use Shaper\Util\Context;
/**
* Tests ListFieldEnhancer.
*
* @group jsonapi_extras
*/
final class ListFieldEnhancerTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'file',
'jsonapi_extras',
'jsonapi',
'serialization',
'system',
'user',
'options',
];
/**
* Tests JSON schema output.
*/
public function testGetOutputJsonSchema(): void {
self::assertEquals([
'type' => 'array',
'items' => [
'type' => 'object',
'properties' => [
'value' => [
'anyOf' => [
['type' => 'string'],
['type' => 'number'],
['type' => 'null'],
],
],
'label' => [
'anOf' => [
['type' => 'string'],
['type' => 'null'],
],
],
],
],
], $this->getInstance()->getOutputJsonSchema());
}
/**
* Tests transform.
*/
public function testTransform(): void {
$output = $this->getInstance()->transform([['value' => 1]]);
self::assertEquals([1], $output);
$output = $this->getInstance()->transform('foo');
self::assertEquals('foo', $output);
}
/**
* Tests undo transform.
*/
public function testUndoTransform(): void {
$field_item = $this->createMock(ListIntegerItem::class);
$field_item->expects($this->once())
->method('getPossibleOptions')
->willReturn([1 => 'One', 2 => 'Two', 3 => 'Three']);
$context = new Context();
$context['field_item_object'] = $field_item;
$output = $this->getInstance()->undoTransform([3, 1, 2], $context);
self::assertEquals(
[
['value' => 3, 'label' => 'Three'],
['value' => 1, 'label' => 'One'],
['value' => 2, 'label' => 'Two'],
],
$output
);
}
/**
* Gets a plugin instance for testing.
*
* @return \Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer\ListFieldEnhancer
* The plugin instance.
*/
private function getInstance(): ListFieldEnhancer {
return $this->container->get('plugin.manager.resource_field_enhancer')->createInstance('list');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment