
API FieldConfig pour créer le champ :
// Création du champ
FieldStorageConfig::create([
'field_name' => 'field_sous_titre',
'entity_type' => 'node',
'type' => 'string'
])->save();
// Attribution du champ aux nodes de type article
FieldConfig::create([
'field_name' => 'field_sous_titre',
'entity_type' => 'node',
'bundle' => 'article',
'label' => 'Sous-titre',
'required' => true,
])->save();
On set l'affichage dans la vue et dans le formulaire
// Récupération des view modes des nodes de type article
$viewmodes = \Drupal::service('entity_display.repository')->getViewModeOptionsByBundle('node', 'article');
$viewmode_names = [];
foreach ($viewmodes as $viewmode_sysname => $viewmode) {
$viewmode_names[] = $viewmode_sysname;
}
// On n'affichera ce champ sur aucun des view mods (pour l'exemple)
foreach($viewmode_names as $viewmode_name) {
\Drupal::service('entity_display.repository')->getViewDisplay('node', 'article', $viewmode_name)
->removeComponent('field_id_prestataire')
->save();
}
// On affichera ce champ dans le formulaire
\Drupal::service('entity_display.repository')->getFormDisplay('node', 'article', 'default')
->setComponent('field_id_prestataire', [
'type' => 'string_textfield',
'weight' => 20 // Ordre d'affichage dans le formulaire
])
->save();
;
Types possibles
View type fields :
- string: A simple string.
- boolean: A boolean stored as an integer.
- integer: An integer, with settings for min and max value validation (also- provided for decimal and float)
- decimal: A decimal with configurable precision and scale.
- float: A float number
- language: Contains a language code and the language as a computed property
- timestamp: A Unix timestamp stored as an integer
- created: A timestamp that uses the current time as a default value.
- changed: A timestamp that is automatically updated to the current time if the entity is saved.
- datetime: A date stored as an ISO 8601 string.
- uri: Contains a URI. The link module also provides a link field type that can- include a link title and can point to an internal or external URI/route.
- uuid: A UUID field that generates a new UUID as the default value.
- email: An email, with corresponding validation and widgets and formatters.
- entity_reference: An entity reference with a target_id and a computed entity- field property. entity_reference.module provides widgets and formatters when enabled.
- map: Can contain any number of arbitrary properties, stored as a serialized string
Form type fields :
- datetime_datelist
- datetime_default
- file_generic
- image_focal_point
- image_image
- inline_entity_form_simple
- inline_entity_form_complex
- link_default
- metatag_firehose
- path
- telephone_default
- text_textfield
- text_textarea
- text_textarea_with_summary
- webform_entity_reference_autocomplete
- webform_entity_reference_select
- entity_reference_autocomplete_tags
- email_default
- options_select
- language_select
- boolean_checkbox
- entity_reference_autocomplete
- string_textfield
- string_textarea
- uri
- number
- options_buttons
- datetime_timestamp
Attention, j'ai récupéré cette liste directement depuis un site Drupal avec de nombreux modules installés (Webform par exemple), elle peut donc varier d'un site à l'autre, mais je n'ai rien trouvé dans la doc officielle
source : Remplir le type de la méthode setComponent()
avec une valeur inexistante , il affichera les types existants.