I have the following custom field:
/**
* Plugin implementation of the 'price_table' field type.
*
* @FieldType(
* id = "price_table",
* default_widget = "price_table_widget",
* default_formatter = "price_table_formatter",
* )
*/
class PriceTable extends FieldItemBase {
public static function mainPropertyName(): string {
return 'value';
}
public static function schema(FieldStorageDefinitionInterface $field_definition): array {
return [
'columns' => [
'value' => [
'type' => 'blob',
'size' => 'big',
'serialize' => TRUE,
],
],
];
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition): array {
$properties['value'] = MapDataDefinition::create()
->setLabel(t('Table data'));
return $properties;
}
public function setValue($values, $notify = TRUE) {
if (isset($values) && !empty($values['table'])) {
$values['value'] = $values['table'];
unset($values['table']);
parent::setValue($values, $notify);
}
}
public function isEmpty(): bool {
try {
$value = $this->get('value')->getValue();
dpm($value); //<<<
return !is_array($value) || empty($value);
} catch (MissingDataException | InvalidArgumentException $e) {
return FALSE;
}
}
}
with the appropriate widget (using a table form element) and formatter present. The editing goes fine, the serialized data gets stored into the database record of my entity. However, when it comes to the formatter, it displays nothing because isEmpty()
above always gets an empty value and returns FALSE
. The blob gets stored all right (thanks to setValue()
above) but it isn't retrieved by the field.
What am I missing here?