I am working on adding taxonomy to the poll module. This is my BaseFieldDefinition:
$fields['category'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Category'))
->setDescription(t('Section this poll is to appear on'))
->setSetting('target_type', 'taxonomy_term')
->setTranslatable(TRUE)
->setDisplayOptions('form', array(
'type' => 'entity_reference_autocomplete',
'weight' => -10,
'settings' => array(
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
'options_list_callback' => 'my_callback',
),
))
->setDisplayConfigurable('form', TRUE)
->setRequired(FALSE);
However, I would like the category to be optional, but when creating a poll if it is left blank it returns an SQL error and this error appears on screen.
The website encountered an unexpected error. Please try again later.
Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'category' cannot be null: INSERT INTO {poll_field_data} (id, langcode, uid, question, category, runtime, anonymous_vote_allow, cancel_vote_allow, result_vote_allow, status, created, default_langcode) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9, :db_insert_placeholder_10, :db_insert_placeholder_11); Array ( [:db_insert_placeholder_0] => 3 [:db_insert_placeholder_1] => en [:db_insert_placeholder_2] => 1 [:db_insert_placeholder_3] => TEST poll [:db_insert_placeholder_4] => [:db_insert_placeholder_5] => 0 [:db_insert_placeholder_6] => 0 [:db_insert_placeholder_7] => 1 [:db_insert_placeholder_8] => 0 [:db_insert_placeholder_9] => 1 [:db_insert_placeholder_10] => 1442410281 [:db_insert_placeholder_11] => 1 ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 755 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).
If ->setRequired(FALSE)
doesn't allow null columns in the database what does?