2

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?

  • DataDefinition::setRequired? Just a guess... – Mołot Sep 16 '15 at 15:01
  • i have that in my code. unless i need to specify DataDefinition::setRequired somehow – BrandenB171 Sep 16 '15 at 15:44
  • Sorry, somehow I missed it reading your question. No excuse. Drupal 8 is still a beta - maybe you found a bug, then? No other thoughts about it. – Mołot Sep 16 '15 at 15:47
  • yeah thats what i am finding. found a couple questions and no answers – BrandenB171 Sep 16 '15 at 15:54
  • 2
    See also drupal.stackexchange.com/questions/174935/… This question could have easily be modified to ask the same thing. – Daniel Sep 20 '15 at 6:17
2

The 'not null' instruction is automatically set to TRUE if that field is set in your entity's (entity_keys) annotations:

ex:

entity_keys = {
 *     "id" = "id",
 *     "uuid" = "uuid",
 *     "uid" = "uid",
 *     "price" = "price",
 *     "langcode" = "langcode",
 *     "complete" = "complete",
 *   },

In this case ->setRequired(FALSE) will be ignored. However, if you really have to, you can force it to FALSE if you provide your custom storage schema handler.

Todo so add the handler in your entity annotations:

handlers = {
 *     "storage_schema" = "Drupal\YOUR_MODULE\YourEntityStorageSchema",
...
}

And the create YourEntityStorageSchema.php file:

<?php

namespace Drupal\YOUR_MODULE;

use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema;
use Drupal\Core\Field\FieldStorageDefinitionInterface;

class YourEntityStorageSchema extends SqlContentEntityStorageSchema {

  protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) {
    $schema = parent::getEntitySchema($entity_type, $reset);

    //then target your annoying field and set the 'not null' key to FALSE!
    if (!empty($schema['rp_payment']['fields']['operator_id']))
      $schema['rp_payment']['fields']['operator_id']['not null'] = FALSE;

    return $schema;
  }
}

Hope it helps! ^^

  • Searching for this for a while. Thanks. Worked properly on my issue. – kwoxer Jun 17 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.