0

I have a custom entity type with custom fields, one of them is an entity_reference type , I saw this question How to create entity reference field from custom entity to specific content type , and according to it some references I googled , try the below code but it not works .

 $fields['book_cat'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Book Category'))
    ->setDescription(t('Book Category Name'))
    ->setSetting('target_type','taxonomy' )
    ->setSetting('handler','default' )
    ->setTranslatable(TRUE)
    ->setDisplayOptions('form', array(
        'type' => 'options_select',
        'weight' => -1,
        'handler_settings' => array(
            'target_bundles'=>['book_cat_agency'=>'book_cat_agency'],
            'size' => '60',
            'placeholder' => '',
        ),
    ))
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);

But not working.

my vocab name is : book_cat_agency

0

I see two mistake in your code, first ->setSetting('target_type','taxonomy' ) is mistake and try ->setSetting('target_type','taxonomy_term' ) and try to set handler_settings with setSetting I mean ->setSetting('handler_settings',['target_bundles'=>['book_cat_agency'=>'book_cat_agency']] ) (although there is a way to set handler_settings in setDisplayOptions )

and finally try following

$fields['book_cat'] = BaseFieldDefinition::create('entity_reference')
   ->setLabel(t('Book Category'))
   ->setDescription(t('Book Category Name'))
   ->setSetting('target_type','taxonomy_term' )
   ->setSetting('handler','default' )
   ->setSetting('handler_settings',['target_bundles'=>['book_cat_agency'=>'book_cat_agency']] )
   ->setTranslatable(TRUE)
   ->setDisplayOptions('form', array(
       'type' => 'options_select',
       'weight' => -1,
       'settings' => array(
           'size' => '60',
           'placeholder' => '',
       ),
   ))
   ->setDisplayConfigurable('form', TRUE)
   ->setDisplayConfigurable('view', TRUE);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy