Drupal 8 - How to enable custom field under manage form display programmatically?

1.1k views Asked by At

I have created a drupal8 module that programmatically created custom user fields. It display in Manage fields section but not enable/view inside Manage form display and Manage display section.

I have tried to override entiry_form_display but not get success.

core.entity_form_display.user.facebook.default.yml

langcode: en
status: true
dependencies:
  config:
    - field.field.user.user.field_facebook
  module:
    - field_layout
    - layout_discovery
    - user
id: user.field_facebook.default
targetEntityType: user
bundle: field_facebook
mode: default
content:
  field_facebook:
    type: string
    weight: 200
    label: above
    settings:
      link_to_entity: false
    third_party_settings: {  }
hidden: {  }

field.field.user.user.field_facebook.yml

langcode: en
status: true
dependencies:
  config:
    - field.storage.user.field_facebook
  module:
    - user
id: user.user.field_facebook
field_name: field_facebook
entity_type: user
bundle: user
label: 'Facebook'
description: ''
required: false
translatable: false
default_value: {  }
default_value_callback: ''
settings: {  }
field_type: string

field.storage.user.field_fullcontact_facebook.yml

langcode: en
status: true
dependencies:
  module:
    - user
id: user.field_facebook
field_name: field_facebook
entity_type: user
type: string
settings: { 
  max_length: 255
  is_ascii: false
  case_sensitive: false
 }
module: core
locked: false
cardinality: 1
translatable: true
indexes: {  }
persist_with_no_fields: false
custom_storage: false
2

There are 2 answers

1
Sibil On

persist_with_no_fields: true

Give it a try.

0
Alen Simonyan On

For enabling custom field under manage form display programmatically you will need to set hidden property to FALSE for field in EntityFormDisplay configuration, here is how.

$form_display = EntityFormDisplay::load('user.' . 'facebook' . '.default');
if (!$form_display) {
  $form_display = EntityFormDisplay::create([
    'targetEntityType' => 'user',
    'bundle' => 'facebook',
    'mode' => 'default',
    'status' => TRUE,
  ])->setComponent('field_facebook', [
    'region' => 'content',
    'type' => 'string_textfield',
    'weight' => 5,
    'hidden' => FALSE,
  ])
  ->save();
}