I have a custom entity type "enquiry" which has a custom field "product_enquiry" added to it.
On the edit form at /enquiry/123/edit I save and get "this value should be of the correct primitive type".
In the database "product_enquiries_quote_lead_time" is NULL, as expected - product enquiries are initially created without a lead time.
If I "UPDATE enquiry__product_enquiries SET product_enquiries_quote_lead_time = 0;", flush cache and then save the form again, the error no longer occurs. If I manually UPDATE the column back to NULL again the error comes back.
In PrimitiveTypeConstraintValidator I can see that $typed_data is an instance of IntegerData (as expected), and $value is an empty string. Surely $value should be undef in this case?
Currently it seems like it's impossible for a custom field to support an integer column that may be null.
Might be a duplicate of https://www.drupal.org/node/2220381 but I'm not sure.
ProductEnquiryItem.php:
/**
* @FieldType(
* id = "product_enquiry",
*...
* )
*/
class ProductEnquiryItem extends FieldItemBase implements FieldItemInterface {
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
...
'quote_lead_time' => [
'description' => 'The quoted lead time (in weeks).',
'type' => 'int',
'not null' => FALSE, // null until we set a lead time
],
],
'indexes' => [
...
'quote_lead_time' => [ 'quote_lead_time' ],
],
'foreign keys' => [
...
],
];
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['quote_lead_time'] = DataDefinition::create('integer')->setLabel(t('Quoted lead time'));
return $properties;
}
DESCRIBE enquiry__product_enquiries:
+--------------------------------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------------------------------+------------------+------+-----+---------+-------+
| bundle | varchar(128) | NO | MUL | | |
| deleted | tinyint(4) | NO | PRI | 0 | |
| entity_id | int(10) unsigned | NO | PRI | NULL | |
| revision_id | int(10) unsigned | NO | MUL | NULL | |
| langcode | varchar(32) | NO | PRI | | |
| delta | int(10) unsigned | NO | PRI | NULL | |
...
| product_enquiries_quote_lead_time | int(11) | YES | MUL | NULL | |
+--------------------------------------+------------------+------+-----+---------+-------+