0

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

error:

The website encountered an unexpected error. Please try again later.
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 my baseFieldDefinitions() function:

 public static function baseFieldDefinitions(EntityTypeInterface $entity_type){

        // Standard field, used as unique if primary index.
        $fields['id'] = BaseFieldDefinition::create('integer')
            ->setLabel(t('ID'))
            ->setDescription(t('The ID of the redirect entity.'))
            ->setReadOnly(TRUE);

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

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


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

Problem 2: Also I do not see the lang select form element anylonger on the edit/add form.

0

I had to set the fieldWidget to hidden.

 //langcode
        $fields['langcode'] = BaseFieldDefinition::create('language')
            ->setLabel(t('Language'))
            ->setTranslatable(true)
            ->setDescription(t('The redirect language code.'))
            ->setDisplayOptions('form', array(
                'type' => 'language_select',
                'weight' => 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.

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.