Select Git revision

Mikael Meulle authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
Source.php 1.08 KiB
<?php
namespace Drupal\ui_patterns_field\Plugin\DataType;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\Attribute\DataType;
use Drupal\Core\TypedData\TypedData;
/**
* Provides a data type wrapping UI Patterns source.
*
* @internal
* Plugin classes are internal.
*/
#[DataType(
id: "ui_patterns_source",
label: new TranslatableMarkup("Source (UI Patterns)"),
description: new TranslatableMarkup("Source datatype for UI Patterns"),
)]
class Source extends TypedData {
/**
* The pattern configuration.
*/
protected ?string $value;
/**
* {@inheritdoc}
*/
public function setValue($value, $notify = TRUE): void {
if (is_array($value)) {
$value = json_encode($value);
}
parent::setValue($value, $notify);
}
/**
* {@inheritdoc}
*/
public function getValue() {
try {
$value = isset($this->value) ? parent::getValue() : NULL;
return empty($value) ? [] : json_decode($value, TRUE, 512, JSON_THROW_ON_ERROR | JSON_OBJECT_AS_ARRAY);
}
catch (\Exception $e) {
return NULL;
}
}
}