ChartConfigItem.php in Charts 5.0.x
File
src/Plugin/Field/FieldType/ChartConfigItem.php
View source
<?php
namespace Drupal\charts\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\DataDefinition;
class ChartConfigItem extends FieldItemBase {
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['config'] = DataDefinition::create('chart_config')
->setLabel(new TranslatableMarkup('Chart configuration'))
->setRequired(TRUE);
$properties['library'] = DataDefinition::create('string')
->setLabel(t('Chart library'));
$properties['type'] = DataDefinition::create('string')
->setLabel(t('Chart type'));
return $properties;
}
public static function mainPropertyName() {
return 'config';
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
$schema = [
'columns' => [
'config' => [
'type' => 'blob',
'size' => 'big',
'not null' => TRUE,
'serialize' => TRUE,
],
'library' => [
'description' => 'The chart library.',
'type' => 'varchar_ascii',
'length' => 255,
'not null' => TRUE,
],
'type' => [
'description' => 'The chart type.',
'type' => 'varchar_ascii',
'length' => 255,
'not null' => TRUE,
],
],
];
return $schema;
}
public function isEmpty() {
return empty($this->config);
}
public function setValue($values, $notify = TRUE) {
if (isset($values) && isset($values['config'])) {
$values['library'] = $values['config']['library'];
$values['type'] = $values['config']['type'];
}
parent::setValue($values, $notify);
}
}