Nasty error after saving custom entity with customized duration field

Created on 26 April 2018, over 6 years ago
Updated 22 November 2023, about 1 year ago

Code used for field creation

    $fields['duration'] = BaseFieldDefinition::create('duration')
      ->setLabel(t('Duration'))
      ->setDescription(t('Length of the video.'))
      ->setSetting('granularity', [
          'year' => FALSE,
          'month'=> FALSE,
          'day' => FALSE,
          'hour' => FALSE,
          'minute' => TRUE,
          'second' => TRUE,
      ])
      ->setDisplayOptions('form', [
        'type' => 'duration',
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

Error after saving the entity.

Error: Cannot unset string offsets in Drupal\Core\Field\WidgetBase->extractFormValues() (line 367 of /var/www/html/web/core/lib/Drupal/Core/Field/WidgetBase.php) #0 /var/www/html/web/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php(225): Drupal\Core\Field\WidgetBase->extractFormValues(Object(Drupal\Core\Field\FieldItemList), Array, Object(Drupal\Core\Form\FormState)) #1 /var/www/html/web/core/lib/Drupal/Core/Entity/ContentEntityForm.php(335): Drupal\Core\Entity\Entity\EntityFormDisplay->extractFormValues(Object(Drupal\ed_academy\Entity\Module), Array, Object(Drupal\Core\Form\FormState)) #2 /var/www/html/web/core/lib/Drupal/Core/Entity/EntityForm.php(293): Drupal\Core\Entity\ContentEntityForm->copyFormValuesToEntity(Object(Drupal\ed_academy\Entity\Module), Array, Object(Drupal\Core\Form\FormState)) #3 /var/www/html/web/core/lib/Drupal/Core/Entity/ContentEntityForm.php(156): Drupal\Core\Entity\EntityForm->buildEntity(Array, Object(Drupal\Core\Form\FormState)) #4 /var/www/html/web/core/lib/Drupal/Core/Entity/ContentEntityForm.php(187): Drupal\Core\Entity\ContentEntityForm->buildEntity(Array, Object(Drupal\Core\Form\FormState)) #5 [internal function]: Drupal\Core\Entity\ContentEntityForm->validateForm(Array, Object(Drupal\Core\Form\FormState)) #6 /var/www/html/web/core/lib/Drupal/Core/Form/FormValidator.php(83): call_user_func_array(Array, Array) #7 /var/www/html/web/core/lib/Drupal/Core/Form/FormValidator.php(276): Drupal\Core\Form\FormValidator->executeValidateHandlers(Array, Object(Drupal\Core\Form\FormState)) #8 /var/www/html/web/core/lib/Drupal/Core/Form/FormValidator.php(119): Drupal\Core\Form\FormValidator->doValidateForm(Array, Object(Drupal\Core\Form\FormState), 'module_edit_for...') #9 /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php(571): Drupal\Core\Form\FormValidator->validateForm('module_edit_for...', Array, Object(Drupal\Core\Form\FormState)) #10 /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php(314): Drupal\Core\Form\FormBuilder->processForm('module_edit_for...', Array, Object(Drupal\Core\Form\FormState)) #11 /var/www/html/web/core/lib/Drupal/Core/Controller/FormController.php(74): Drupal\Core\Form\FormBuilder->buildForm('module_edit_for...', Object(Drupal\Core\Form\FormState)) #12 [internal function]: Drupal\Core\Controller\FormController->getContentResult(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\RouteMatch)) #13 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array) #14 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(582): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() #15 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure)) #16 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) #17 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(151): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() #18 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(68): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1) #19 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #20 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #21 /var/www/html/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(99): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #22 /var/www/html/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(78): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true) #23 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #24 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(50): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #25 /var/www/html/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #26 /var/www/html/web/core/lib/Drupal/Core/DrupalKernel.php(664): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #27 /var/www/html/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request)) #28 {main}.

🐛 Bug report
Status

Closed: outdated

Component

Code

Created by

🇪🇪Estonia kaido.toomingas

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • #10 works for me setting a moderation state to review on Drupal 9.5.9.

    $form_state->setValue('moderation_state', ['0' => [ 'value' => 'review' ] ] ); //Works!
    

    Additionally, I started off with getting the entity and setting the value with:

    $entity = $form_state->getFormObject()->getEntity();
    $entity->set('moderation_state', 'review');
    

    This didn't even throw out an error. It just didn't work. It wasn't until I tried:

    $form_state->setValue('moderation_state', 'review');
    

    and finally the above error and I ended up here.

  • #10 works for me saving a textfield value

Production build 0.71.5 2024