Error when translatable When I save my Custom Entity Type I get an error. I tried several different things. The code looks valid to me.


  Error: Cannot unset string offsets in Drupal\Core\Field\WidgetBase->extractFormValues() (line 367 of core/lib/Drupal/Core/Field/WidgetBase.php).


This is my annotation.

/**
 * @ContentEntityType(
 *   id = "email_confirm_redirect",
 *   label = @Translation("Redirect entity"),
 *   admin_permission = "administer email confirm redirect entity",
 *   base_table = "email_confirm_redirect",
 *   translatable = TRUE,
 *   handlers = {
 *     "list_builder" = "Drupal\email_confirm_redirect\Entity\Controller\RedirectListBuilder",
 *     "form" = {
 *       "add" = "Drupal\email_confirm_redirect\Form\RedirectForm",
 *       "edit" = "Drupal\email_confirm_redirect\Form\RedirectForm",
 *       "delete" = "Drupal\email_confirm_redirect\Form\RedirectDeleteForm",
 *     },
 *     "access" = "Drupal\email_confirm_redirect\RedirectAccessControlHandler",
 *   },
 *   list_cache_contexts = { "user" },
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "title",
 *     "uuid" = "uuid",
 *     "langcode" = "langcode",
 *     "admin_name" = "admin_name",
 *   },
 *   links = {
 *     "edit-form" = "/admin/email_confirm_redirect/{email_confirm_redirect_entity}/edit",
 *     "delete-form" = "/admin/email_confirm_redirect/{email_confirm_redirect_entity}/delete",
 *     "collection" = "/admin/structure/email_confirm_redirect/list"
 *   },
 * )
 */


This is the code I am using.

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  // Standard field, used as unique if primary index.
  $fields['id'] = BaseFieldDefinition::create('integer')
    -&gt;setLabel(t('ID'))
    -&gt;setDescription(t('The ID of the redirect entity.'))
    -&gt;setReadOnly(TRUE);

  // Standard field, unique outside of the scope of the current project.
  $fields['uuid'] = BaseFieldDefinition::create('uuid')
    -&gt;setLabel(t('UUID'))
    -&gt;setDescription(t('The UUID of the redirect entity.'))
    -&gt;setReadOnly(TRUE);

  $fields['langcode'] = BaseFieldDefinition::create('language')
    -&gt;setLabel(t('Language'))
    -&gt;setTranslatable(true)
    -&gt;setDescription(t('The redirect language code.'))
    -&gt;setDisplayOptions('form', array('type' =&gt; 'language_select', 'weight' =&gt; 2));

  $fields['admin_name'] = BaseFieldDefinition::create('string')
    -&gt;setLabel(t('Administrative Name'))
    -&gt;setDescription(t('The name for the redirect (shown just in admin)'))
    -&gt;setRequired(TRUE)
    -&gt;setSettings(['max_length' =&gt; 255, 'text_processing' =&gt; 0])
    -&gt;setDefaultValue(NULL)
    -&gt;setDisplayOptions('form', ['type' =&gt; 'string_textfield', 'weight' =&gt; -5])
    -&gt;setDisplayConfigurable('form', FALSE)
    -&gt;setDisplayConfigurable('view', FALSE);
}


I also don't see the language selector on the edit/add form.
I had to set the fieldWidget to hidden.

 //langcode
        $fields['langcode'] = BaseFieldDefinition::create('language')
            -&gt;setLabel(t('Language'))
            -&gt;setTranslatable(true)
            -&gt;setDescription(t('The redirect language code.'))
            -&gt;setDisplayOptions('form', array(
                'type' =&gt; 'language_select',
                'weight' =&gt; 2,
            ));


Also I used Drupal's language configuration settings to enable the multilanguage feature for the entity.

The only missing bit is the language translation interface that you can use with nodes.