8

I've programmatically added a user field but i can't find a way to programmatically enable the field under 'manage form display' and 'manage display'.

I tried exporting the 'entity view display' and 'entity form display' configuration as seen below but it will not allow me to install the module using this method

Entity Form display:

uuid: ecd2bd45-26ec-4fee-835a-eedc20a1dbf9
langcode: en
status: true
dependencies:
  config:
    - field.field.user.user.field_birthdate_pixelpin
    - field.field.user.user.field_country_pixelpin
    - field.field.user.user.field_family_name_pixelpin
    - field.field.user.user.field_gender_pixelpin
    - field.field.user.user.field_given_name_pixelpin
    - field.field.user.user.field_nickname_pixelpin
    - field.field.user.user.field_phone_number_pixelpin
    - field.field.user.user.field_postal_code_pixelpin
    - field.field.user.user.field_region_pixelpin
    - field.field.user.user.field_street_address_pixelpin
    - field.field.user.user.field_town_city_pixelpin
    - field.field.user.user.user_picture
    - image.style.thumbnail
  module:
    - image
    - user
_core:
  default_config_hash: LLAieeozVsoZDb-2PbFxRJpQqnKmpR7-4OoRJnduz-U
id: user.user.default
targetEntityType: user
bundle: user
mode: default
content:
  account:
    weight: 0
    settings: {  }
    third_party_settings: {  }
  contact:
    weight: 3
    settings: {  }
    third_party_settings: {  }
  field_birthdate_pixelpin:
    type: string_textfield
    weight: 5
    settings:
      size: 60
      placeholder: ''
    third_party_settings: {  }
  field_country_pixelpin:
    type: string_textfield
    weight: 6
    settings:
      size: 60
      placeholder: ''
    third_party_settings: {  }
  field_family_name_pixelpin:
    type: string_textfield
    weight: 7
    settings:
      size: 60
      placeholder: ''
    third_party_settings: {  }
  field_gender_pixelpin:
    type: string_textfield
    weight: 8
    settings:
      size: 60
      placeholder: ''
    third_party_settings: {  }
  field_given_name_pixelpin:
    type: string_textfield
    weight: 9
    settings:
      size: 60
      placeholder: ''
    third_party_settings: {  }
  field_nickname_pixelpin:
    type: string_textfield
    weight: 10
    settings:
      size: 60
      placeholder: ''
    third_party_settings: {  }
  field_phone_number_pixelpin:
    type: string_textfield
    weight: 11
    settings:
      size: 60
      placeholder: ''
    third_party_settings: {  }
  field_postal_code_pixelpin:
    type: string_textfield
    weight: 12
    settings:
      size: 60
      placeholder: ''
    third_party_settings: {  }
  field_region_pixelpin:
    type: string_textfield
    weight: 13
    settings:
      size: 60
      placeholder: ''
    third_party_settings: {  }
  field_street_address_pixelpin:
    type: string_textfield
    weight: 14
    settings:
      size: 60
      placeholder: ''
    third_party_settings: {  }
  field_town_city_pixelpin:
    type: string_textfield
    weight: 15
    settings:
      size: 60
      placeholder: ''
    third_party_settings: {  }
  language:
    weight: 2
    settings: {  }
    third_party_settings: {  }
  timezone:
    weight: 4
    settings: {  }
    third_party_settings: {  }
  user_picture:
    type: image_image
    settings:
      progress_indicator: throbber
      preview_image_style: thumbnail
    third_party_settings: {  }
    weight: 1
hidden: {  }

Entity View display:

uuid: 7ae755fd-5988-446f-beb5-2c9725b10c30
langcode: en
status: true
dependencies:
  config:
    - field.field.user.user.field_birthdate_pixelpin
    - field.field.user.user.field_country_pixelpin
    - field.field.user.user.field_family_name_pixelpin
    - field.field.user.user.field_gender_pixelpin
    - field.field.user.user.field_given_name_pixelpin
    - field.field.user.user.field_nickname_pixelpin
    - field.field.user.user.field_phone_number_pixelpin
    - field.field.user.user.field_postal_code_pixelpin
    - field.field.user.user.field_region_pixelpin
    - field.field.user.user.field_street_address_pixelpin
    - field.field.user.user.field_town_city_pixelpin
    - field.field.user.user.user_picture
    - image.style.thumbnail
  module:
    - image
    - user
_core:
  default_config_hash: L2mtwGWH_7wDRCMIR4r_Iu_jmvQ10DV1L8ht8iNZ5qY
id: user.user.default
targetEntityType: user
bundle: user
mode: default
content:
  field_birthdate_pixelpin:
    type: string
    weight: 2
    label: above
    settings:
      link_to_entity: false
    third_party_settings: {  }
  field_country_pixelpin:
    type: string
    weight: 3
    label: above
    settings:
      link_to_entity: false
    third_party_settings: {  }
  field_family_name_pixelpin:
    type: string
    weight: 4
    label: above
    settings:
      link_to_entity: false
    third_party_settings: {  }
  field_gender_pixelpin:
    type: string
    weight: 5
    label: above
    settings:
      link_to_entity: false
    third_party_settings: {  }
  field_given_name_pixelpin:
    type: string
    weight: 6
    label: above
    settings:
      link_to_entity: false
    third_party_settings: {  }
  field_nickname_pixelpin:
    type: string
    weight: 7
    label: above
    settings:
      link_to_entity: false
    third_party_settings: {  }
  field_phone_number_pixelpin:
    type: string
    weight: 8
    label: above
    settings:
      link_to_entity: false
    third_party_settings: {  }
  field_postal_code_pixelpin:
    type: string
    weight: 9
    label: above
    settings:
      link_to_entity: false
    third_party_settings: {  }
  field_region_pixelpin:
    type: string
    weight: 10
    label: above
    settings:
      link_to_entity: false
    third_party_settings: {  }
  field_street_address_pixelpin:
    type: string
    weight: 11
    label: above
    settings:
      link_to_entity: false
    third_party_settings: {  }
  field_town_city_pixelpin:
    type: string
    weight: 12
    label: above
    settings:
      link_to_entity: false
    third_party_settings: {  }
  member_for:
    weight: 1
    settings: {  }
    third_party_settings: {  }
  user_picture:
    type: image
    weight: 0
    settings:
      image_style: thumbnail
      image_link: content
    third_party_settings: {  }
    label: hidden
hidden: {  }

Is there a way to do this in Drupal 8?

i tried to edit the existing fields using this but didn't work:

function openid_connet_entity_edit(){
  \Drupal::entityTypeManager()
  ->getStorage('entity_view_display')
  ->load('user.user.default')
  ->setComponent('field_birthdate_pixelpin', [
      'format' => 'plain text'
    ])->save();

  \Drupal::entityTypeManager()
  ->getStorage('entity_form_display')
  ->load('user.user.default')
  ->setComponent('field_birthdate_pixelpin', [
      'Widget' => 'textfield'
    ])->save();
}
  • What is the error message you get when trying to install your module? – Stefanos Petrakis Jan 5 '17 at 17:06
8

You can't install view modes or form modes that already exists. You could try to overwrite them by parsing the yaml and store the complete content in the configuration, but a better approach would be to only add the new fields to preserve any changes that the site owner could have made.

You can add fields with setComponent() in the configuration of the view mode:

\Drupal::entityTypeManager()
  ->getStorage('entity_view_display')
  ->load('user.user.default')
  ->setComponent('field_example', [
        'label' => 'hidden',
        'type' => 'string',
        'settings' => ['...' => '...'],
    ])->save();

or form mode:

\Drupal::entityTypeManager()
  ->getStorage('entity_form_display')
  ->load('user.user.default')
  ->setComponent('field_example', [
   ...

Example

core.entity_view_display.user.user.default.yml

content:
  field_birthdate_pixelpin:
    type: string
    weight: 2
    label: above
    settings:
      link_to_entity: false
    third_party_settings: {  }

PHP

\Drupal::entityTypeManager()
  ->getStorage('entity_view_display')
  ->load('user.user.default')
  ->setComponent('field_birthdate_pixelpin', [
      'type' => 'string',
      'label' => 'above',
      'settings' => ['link_to_entity' => 'false'],
  ])->save();  

If you set no weight, the field will be appended under the last field.

  • So does this code create the fields rather than edit their properties? – Callum Jan 6 '17 at 10:33
  • Both. If it does not exist, it creates the component for the field (formatter or widget). If you want to edit you could use get/set to load the component and modify it. removeComponent() is the third option to hide the component again. – 4k4 Jan 6 '17 at 11:25
  • i've made an edit showing what i tried – Callum Jan 6 '17 at 11:32
  • I add an example. It's not difficult if you use the YAML you get as result when you configure the widget/formatter in UI. – 4k4 Jan 6 '17 at 12:38

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.