I've created a custom content entity in Drupal 8 and all is well in those regards. I have an issue when I go to create a new record for my entity using the entity form. "This value should not be null.".

The error comes from a widget plugin that I've created that only replicates the functionality of a list of checkboxes that represent taxonomy terms. The field that uses this widget is not a base field, it's added through the UI. I've looked at the plugin documentation on drupal.org and haven't found much help in regards to my issue.

Below, is my code for the widget form element.

namespace Drupal\my_entity\Plugin\Field\FieldWidget;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsWidgetBase;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\taxonomy\Entity\Term;
use Drupal\user\Entity\User;

 * A widget for displaying terms and supplying an AJAX request when a checkbox is clicked
 * @FieldWidget(
 *   id = "my_entity_programme_areas_lookup_directors",
 *   module = "my_entity",
 *   label = @Translation("Programme Areas Lookup Directors Widget"),
 *   field_types = {
 *     "entity_reference"
 *   }
 * )
class ProgrammeAreasLookupDirectorsWidget extends OptionsWidgetBase

   * {@inheritdoc}
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {

    $value = isset($items[$delta]->value) ? $items[$delta]->value : NULL;

    $element = parent::formElement($items, $delta, $element, $form, $form_state);

    $element += [
      '#type'          => 'checkboxes',
      '#default_value' => $this->getSelectedOptions($items),
      '#options'       => $this->getOptions($items->getEntity()),
      '#multiple'      => TRUE,
      '#element_validate' => [
        [static::class, 'validate'],
      '#prefix' => '<div id="programme-areas-directors-lookup">',
      '#suffix' => '</div>',

    return ['value' => $element];

    * @inheritdoc
  public static function validate($element, FormStateInterface $form_state) {

    $value = $element['#value'];
    return true;

For the moment, I just want it to pass validation so then I can work on from there, but right now, it can't seem to retrieve a value, even though I can see it has the expected values in the $form_state object (field_programme_areas field).

enter image description here

Any help and tips would be greatly appreciated. Thanks!


I managed to get it working by extending the OptionsButtonsWidget class instead of OptionsWidgetBase. I've also added the multiple_values annotation which I was missing and also removed the validation callback I was supplying, so now the validateElement method from OptionsWidgetBase is the validation handler used. My widget class just extends OptionsButtonsWidget and works fine. I think the missing annotation was the main reason it wouldn't validate.

  • Glad you figured out that inheritance is good way to go in D8. – Codium Sep 13 '18 at 9:11

I had exactly the same problem with a custom entity extending Datetime (shamelessly adapted from datetime range: in my case, I need a datetime with a comment). I followed your suggestions, adding multiple_values to the annotations of the widget, and extending DateTimeDefaultWidget rather than DateTimeWidgetBase, but it wasn't enough, I also had to add setRequired(FALSE) on the property definitions in FieldType. It seems odd to me, because "required" in such a case should mean "the properties are required if there is an attached entity", yet it apparently means something different, or maybe there is a subtle bug that I don't understand.

  • What was the error message you were getting before adding setRequired? Maybe it requires you to explicitly set that for strictness. – Josh Brown Sep 13 '18 at 16:30
  • It was the usual This value should not be null, with all the fields of my entity set to red. I don't understand, maybe I should set what for strictness? – Francesco Marchetti-Stasi Sep 14 '18 at 7:48
  • As in always setting the setRequired() method for strictness. Maybe field type definitions get a bit funny when it doesn't know for sure if it's a required field or not and so it doesn't assume a default value. – Josh Brown Sep 14 '18 at 9:10

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.