अपने कंटेंट टाइप में कस्टम फ़ील्ड्स को प्रोग्राम करना
कभी-कभी, जब आप कस्टम मॉड्यूल से कंटेंट टाइप निकालते हैं, तो आप उस कंटेंट टाइप से संबंधित फ़ील्ड्स भी जोड़ सकते हैं। फ़ील्ड्स को ऑटोमैटिकली बनाने की सुविधा आपको कई साइटों पर मॉड्यूल को हटाने और फिर से इंस्टॉल करने की अनुमति देती है, बिना अतिरिक्त फ़ील्ड छोड़े और यह सुनिश्चित करते हुए कि आप उन्हें जोड़ना भूलेंगे नहीं। इन फ़ील्ड्स को अपने कोडबेस में जोड़ने के दो तरीके हैं, जिन्हें हम यहाँ देखेंगे।
पूर्वापेक्षाएँ
- Drupal 8.0.x इंस्टॉल हो।
- आपके पास एक कस्टम मॉड्यूल हो (इस उदाहरण में मॉड्यूल का नाम foobar है)।
- आपके पास मॉड्यूल में एक कस्टम कंटेंट टाइप हो (कृपया parent पेज पर दिए गए निर्देशों का पालन करें। कंटेंट टाइप का नाम Car Brand होगा)।
हमारे कंटेंट टाइप में अतिरिक्त फ़ील्ड्स जोड़ना
इस स्टेप को करने के दो तरीके हैं। आप UI का उपयोग करके फ़ील्ड्स बना सकते हैं और कॉन्फ़िगरेशन को फाइलों में एक्सपोर्ट कर सकते हैं, या आप सीधे फाइलें लिख सकते हैं। मुझे पहला विकल्प पसंद है, ताकि आप सुनिश्चित हो सकें कि Drupal द्वारा अपेक्षित कोई भी चीज़ छूटे नहीं। हालाँकि, फ़ील्ड्स को “field_” प्रिफ़िक्स के साथ एक्सपोर्ट किया जाता है, न कि “foobar_car_brand_” के साथ, ताकि वे हमारे कस्टम कंटेंट टाइप से लिंक हों। इस पेज पर हम आपके कस्टम मॉड्यूल में फ़ील्ड्स को manually कोड करना देखेंगे।
अपने मॉड्यूल में फ़ील्ड्स को manually कोड करना
जैसा कि पहले बताया गया है, इस विधि के लिए थोड़ी और जानकारी और यह स्पष्ट समझ होना ज़रूरी है कि आप फ़ील्ड से क्या चाहते हैं और यह कैसा दिखे। हर फ़ील्ड के लिए दो YAML फाइलें ज़रूरी हैं, जो कॉन्फ़िगरेशन जानकारी रखती हैं। उनके नाम होंगे: field.field.node.car_brand.field_brand_information.yml और field.storage.node.field_brand_information.yml।
कस्टम कंटेंट टाइप हटाएँ
अगर आपने पिछले पेज पर foobar मॉड्यूल पहले से सक्षम कर रखा है, तो अभी उसे हटा दें।
foobar/config/install/field.storage.node.field_brand_information.yml
यह फ़ाइल Drupal को बताएगी कि उसे हमारा फ़ील्ड बनाना है।
# 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
यह फ़ाइल Drupal को बताएगी कि उसे हमारे फ़ील्ड को हमारे कंटेंट टाइप से अटैच करना है।
# 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
इन दोनों फाइलों को पिछले पेज से अपडेट करना ज़रूरी है, ताकि वे आपके द्वारा जोड़े गए नए फ़ील्ड्स को शामिल करें। मैंने यहाँ दो अतिरिक्त फ़ील्ड्स “Additional_field_1” और “Additional_field_2” जोड़े हैं, ताकि आप देख सकें कि यह कई फ़ील्ड्स के साथ कैसा दिखता है।
नोट: अगर आप अतिरिक्त फ़ील्ड्स नहीं बनाना चाहते हैं, तो अगले कोड से उन फ़ील्ड्स की dependencies और जानकारी हटा दें।
# 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: { }
कस्टम कंटेंट टाइप सक्षम करें
अब आपको foobar मॉड्यूल सक्षम करना चाहिए। अगर आप अभी “Create content” पेज पर जाते हैं, तो आप देखेंगे कि आप नया “Car Brand” कंटेंट टाइप नोड बना सकते हैं, और इसमें हमारा नया फ़ील्ड “Brand Information” शामिल होगा।
हम इन फ़ील्ड्स को UI के माध्यम से जोड़ना अगले पेज पर देखेंगे।