web-dev-qa-db-fra.com

8 - Comment activer par programme un champ utilisateur sous gérer l'affichage du formulaire et gérer l'affichage?

J'ai ajouté par programme un champ utilisateur, mais je ne trouve pas de moyen d'activer par programmation le champ sous "gérer l'affichage du formulaire" et "gérer l'affichage".

J'ai essayé d'exporter la configuration 'affichage de l'entité' et 'affichage de la forme de l'entité' comme indiqué ci-dessous, mais cela ne me permettra pas d'installer le module à l'aide de cette méthode

Affichage du formulaire d'entité:

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: {  }

Affichage de l'entité:

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: {  }

Existe-t-il un moyen de le faire dans Drupal 8?

j'ai essayé d'éditer les champs existants à l'aide de ceci mais n'a pas fonctionné:

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();
}
8
Callum

Vous ne pouvez pas installer les modes d'affichage ou les modes de formulaire qui existent déjà. Vous pouvez essayer de les remplacer en analysant le yaml et en stockant le contenu complet dans la configuration, mais une meilleure approche consisterait à n'ajouter que les nouveaux champs pour conserver les modifications que le propriétaire du site aurait pu apporter.

Vous pouvez ajouter des champs avec setComponent () dans la configuration du mode d'affichage:

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

ou mode formulaire:

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

Exemple

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();  

Si vous ne définissez aucun poids, le champ sera ajouté sous le dernier champ.

8
4k4