मैं हुक के अंदर config से एक नया क्षेत्र कैसे जोड़ सकता हूं_update_N? [डुप्लिकेट]

Nov 23 2020

मुझे php कोड द्वारा टैक्सोनॉमी जोड़ना होगा। टैक्सोनॉमी को 2 फील्ड मिलने हैं। मैंने test_taxonomy.install में नया फ़ंक्शन जोड़ा, और यह काम करने लगता है:

function taxonomy_update_8805() {

    $config_path = 'modules/feature/test_taxonomy/config/update/'; $source = new FileStorage($config_path); \Drupal::entityManager()->getStorage('taxonomy_vocabulary') ->create($source->read('taxonomy.vocabulary.regulation'))
        ->save();
}

नई शब्दावली को वर्गीकरण में जोड़ा गया।

फ़ाइल की सामग्री:

langcode: pl
status: true
dependencies:
  module:
    - test_remote_vocabulary
third_party_settings:
  test_remote_vocabulary:
    is_remote: 0
name: 'Test'
vid: regulation
description: 'Test desc'
hierarchy: 0
weight: 0
 

समस्याग्रस्त हिस्सा वह है जब मैं शब्दावली में फ़ील्ड जोड़ने की कोशिश करता हूं। मैंने 2 फाइलें बनाईं

field.storage.taxonomy_term.field_regulation_test.yml

langcode: pl
status: true
dependencies:
  module:
    - taxonomy
    - text
id: taxonomy_term.regulation.test
field_name: test_field
entity_type: taxonomy_term
type: text_long
settings: {  }
module: text
locked: false
cardinality: 1
translatable: true
indexes: {  }
persist_with_no_fields: false
custom_storage: false

field.field.taxonomy_term.field_regulation_test.yml

langcode: pl
status: true
dependencies:
  config:
    - field.storage.taxonomy_term.field_regulation_test
    - taxonomy.vocabulary.regulation
id: taxonomy_term.regulation.test
field_name: test_content
entity_type: taxonomy_term
bundle: regulation
label: TEST
description: 'Tekst test'
required: true
translatable: false
default_value: {  }
default_value_callback: ''
settings: {  }
field_type: text_long

लेकिन मुझे यकीन नहीं है कि मैं उन्हें update_xxx फ़ंक्शन में कैसे लोड कर सकता हूं।

संपादित करें: मैंने कोशिश की

    \Drupal::entityManager()->getStorage('taxonomy_term')
        ->create($source->read('field.storage.taxonomy_term.field_regulation_abo')) ->save(); \Drupal::entityManager()->getStorage('taxonomy_term') ->create($source->read('field.field.taxonomy_term.field_regulation_abo'))
        ->save();

लेकिन मैं मॉड्यूल अद्यतन करने की कोशिश करते हुए इकाई प्रकार taxonomy_term के लिए गुम बंडल प्राप्त कर रहा हूं

जवाब

3 leymannx Nov 23 2020 at 15:36

हाँ, आप उस सामग्री को नहीं बना सकते हैं जो निर्भरता को आयात करने से पहले किया गया हो। यह एक क्लासिक ड्रुपल 8 विरोधाभास है जो हाल ही में एक नए हुक द्वारा हल किया गया है hook_deploy_NAME(&$sandbox):।

नवीनतम ड्रश को अपडेट करें और अब drush deployसे अपनी तैनाती दिनचर्या के हिस्से के रूप में चलाएं , जो पुरानी drush updb && drush cimदिनचर्या को बदल देता है। फिर अपने मॉड्यूल में एक नई फ़ाइल बनाएं MYMODULE.deploy.php:। वहाँ लागू करने में MYMODULE_deploy_NAME(&$sandbox)जहां NAMEकोई अनूठा नाम (या एक वृद्धि संख्या) भी हो सकता है। और इस हुक का उपयोग शब्दों को बनाने के लिए करें, जैसे कि hook_update_N(&$sandbox)आपके प्रश्न में।

drush deploy यह सुनिश्चित करेगा कि विन्यास हुक आयात विन्यास से निर्मित होने के बाद, इस हुक को परिनियोजन दिनचर्या में सबसे ऊपर उठाया जाएगा।