Programmeren van custom velden in je inhoudstype
Soms, wanneer je een inhoudstype uit een custom module extraheert, kun je ook velden toevoegen die betrekking hebben op dat inhoudstype. De mogelijkheid om automatisch velden aan te maken stelt je in staat om te verwijderen en opnieuw te installeren op meerdere sites, zonder overbodige velden achter te laten en met de garantie dat je niet vergeet ze toe te voegen. Er zijn twee manieren om deze velden aan je codebasis toe te voegen, die we zullen bekijken.
Voorwaarden
- Drupal 8.0.x is geïnstalleerd.
- Je hebt een custom module (de modulenaam die in dit voorbeeld wordt gebruikt is foobar).
- Je hebt een custom inhoudstype in de module (Volg de instructies op de bovenliggende pagina. De naam van het inhoudstype zal Car Brand zijn).
Extra velden toevoegen aan ons inhoudstype
Er zijn twee manieren om deze stap te doen. Je kunt de gebruikersinterface gebruiken om velden aan te maken en configuratie te exporteren naar bestanden, of je kunt gewoon zelf de bestanden schrijven. Ik gebruik graag de eerste optie, gewoon om zeker te zijn dat je niets mist dat vereist is. Houd er echter rekening mee dat velden worden geëxporteerd met “field_” aan het begin, en niet “foobar_car_brand_”, om ze te koppelen aan ons custom inhoudstype. We zullen hier het handmatig coderen van velden in je custom module bekijken.
Handmatig coderen van velden in je module
Zoals eerder vermeld, vereist deze methode iets meer kennis en een duidelijk beeld van wat je wilt dat het veld doet en hoe het eruitziet. Elk toegevoegd veld moet twee YAML-bestanden hebben die de configuratie-informatie bevatten. Ze heten als volgt: field.field.node.car_brand.field_brand_information.yml en field.storage.node.field_brand_information.yml.
Verwijder het custom inhoudstype
Als je de foobar-module al hebt ingeschakeld aan het einde van de vorige pagina, verwijder deze dan nu.
foobar/config/install/field.storage.node.field_brand_information.yml
Dit bestand vertelt Drupal dat het ons veld moet aanmaken.
# field.storage.node.field_brand_information.yml
langcode: en
status: true
dependencies:
module:
- node
- text
id: node.field_brand_information
field_name: field_brand_information
entity_type: node
type: text_with_summary
settings: { }
module: text
locked: false
cardinality: 1
translatable: true
indexes: { }
persist_with_no_fields: false
custom_storage: false
foobar/config/install/field.field.node.car_brand.field_brand_information.yml
Dit bestand vertelt Drupal dat het ons veld moet koppelen aan ons inhoudstype.
# field.field.node.car_brand.field_brand_information.yml
langcode: en
status: true
dependencies:
config:
- field.storage.node.field_brand_information
- node.type.car_brand
module:
- text
id: node.car_brand.field_brand_information
field_name: field_brand_information
entity_type: node
bundle: car_brand
label: 'Brand Information'
description: 'More specific information about the car brand'
required: false
translatable: false
default_value: { }
default_value_callback: ''
settings:
display_summary: false
field_type: text_with_summary
foobar/config/install/core.entity_form_display.node.car_brand.default.yml | foobar/config/install/core.entity_view_display.node.car_brand.default.yml
Beide bestanden moeten worden bijgewerkt vanaf de vorige pagina, zodat ze alle nieuwe velden bevatten die je toevoegt. Ik heb twee extra velden toegevoegd met de namen “Additional_field_1” en “Additional_field_2”, zodat je kunt zien hoe dit eruitziet met meerdere velden.
Opmerking: als je geen extra velden wilt aanmaken, verwijder dan de dependencies en informatie voor deze velden in de onderstaande code.
# core.entity_view_display.node.car_brand.default.yml
langcode: en
status: true
dependencies:
config:
- field.field.node.car_brand.field_brand_information
- field.field.node.car_brand.field_additional_field_1
- field.field.node.car_brand.field_additional_field_2
- node.type.car_brand
module:
- file
- text
- user
_core:
default_config_hash: Nfnv6VMugBKl6EOqi_U0I_LQ1ZQpbNDN3a9GXHWUBz4
id: node.car_brand.default
targetEntityType: node
bundle: car_brand
mode: default
content:
field_brand_information:
weight: 101
label: above
settings: { }
third_party_settings: { }
type: text_default
field_additional_field_1:
weight: 103
label: above
settings:
link_to_entity: false
third_party_settings: { }
type: string
field_additional_field_2:
weight: 102
label: above
settings: { }
third_party_settings: { }
type: file_default
links:
weight: 100
hidden: { }
# core.entity_form_display.node.car_brand.default.yml
langcode: en
status: true
dependencies:
config:
- field.field.node.car_brand.field_brand_information
- field.field.node.car_brand.field_additional_field_1
- field.field.node.car_brand.field_additional_field_2
- node.type.car_brand
module:
- file
- path
- text
_core:
default_config_hash: qZE-qJ04DTTNggVVQdVOPQmpE_I69GQ_LqB32kXivVg
id: node.car_brand.default
targetEntityType: node
bundle: car_brand
mode: default
content:
created:
type: datetime_timestamp
weight: 2
settings: { }
third_party_settings: { }
field_brand_information:
weight: 7
settings:
rows: 9
summary_rows: 3
placeholder: ''
third_party_settings: { }
type: text_textarea_with_summary
field_additional_field_1:
weight: 6
settings:
size: 60
placeholder: ''
third_party_settings: { }
type: string_textfield
field_additional_field_2:
weight: 8
settings:
progress_indicator: throbber
third_party_settings: { }
type: file_generic
path:
type: path
weight: 5
settings: { }
third_party_settings: { }
promote:
type: boolean_checkbox
settings:
display_label: true
weight: 3
third_party_settings: { }
sticky:
type: boolean_checkbox
settings:
display_label: true
weight: 4
third_party_settings: { }
title:
type: string_textfield
weight: 0
settings:
size: 60
placeholder: ''
third_party_settings: { }
uid:
type: entity_reference_autocomplete
weight: 1
settings:
match_operator: CONTAINS
size: 60
placeholder: ''
third_party_settings: { }
hidden: { }
Activeer het custom inhoudstype
Nu moet je de foobar-module inschakelen. Als je nu naar de pagina “Inhoud aanmaken” gaat, zie je dat je een nieuw node van het inhoudstype “Car Brand” kunt aanmaken, en dat dit ons nieuwe veld met de naam “Brand Information” bevat.
We zullen het toevoegen van deze velden via de gebruikersinterface bekijken op de volgende pagina.