Call to a member function isTranslatable() on null in Drupal\Core\Field\FieldConfigBase->isTranslatable()

Created on 27 December 2021, over 3 years ago
Updated 8 April 2024, 12 months ago

Problem/Motivation

When creating a custom bundle computed field, "Content language" configuration page (/admin/config/regional/content-language) throws an exception on save: Error: Call to a member function isTranslatable() on null in Drupal\Core\Field\FieldConfigBase->isTranslatable()

The website encountered an unexpected error. Please try again later.
Error: Call to a member function isTranslatable() on null in Drupal\Core\Field\FieldConfigBase->isTranslatable() (line 336 of core/lib/Drupal/Core/Field/FieldConfigBase.php).
Drupal\Core\Field\FieldConfigBase->isTranslatable() (Line: 369)
content_translation_form_language_content_settings_submit(Array, Object)
call_user_func_array('content_translation_form_language_content_settings_submit', Array) (Line: 113)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 51)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 593)
Drupal\Core\Form\FormBuilder->processForm('language_content_settings_form', Array, Object) (Line: 321)
...

Steps to reproduce



1. Declare a computed bundle field in a custom module:

/web/modules/custom/my_module/my_module.module

...

function my_module_entity_bundle_field_info(EntityTypeInterface $entity_type, string $bundle, array $base_field_definitions): array {

  $fields = [];

  $fields['my_computed_field'] = BaseFieldDefinition::create('string')
    ->setName('my_computed_field')
    ->setLabel(t('My computed field'))
    ->setComputed(TRUE)
    ->setClass('\Drupal\my_module\MyComputedFieldFieldItemList')
    ->setTranslatable(TRUE)

    return $fields;

/web/modules/custom/my_module/src/MyComputedFieldFieldItemList.php

<?php

  namespace Drupal\my_module;

  use Drupal\Core\Field\FieldItemList;
  use Drupal\Core\TypedData\ComputedItemListTrait;

  class MyComputedFieldFieldItemList extends FieldItemList {

    use ComputedItemListTrait;

    /**
     * {@inheritdoc}
     */
    protected function computeValue() {
      $computedValue = "Foo";
      $this->list[0] = $this->createItem(0, $computedValue);
    }

}



2. Go to content language config page (/admin/config/regional/content-language) and save the form

Proposed resolution

Don't assume that a field has always a storage definition when checking its translatability.

πŸ› Bug report
Status

Active

Version

11.0 πŸ”₯

Component
BaseΒ  β†’

Last updated about 8 hours ago

Created by

πŸ‡ͺπŸ‡ΈSpain vidorado LogroΓ±o (La Rioja)

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.

Production build 0.71.5 2024