2015-04-15 6 views
10

मैं अपने कस्टम मॉड्यूल का उपयोग कर ड्रूपल 8 में एक इकाई संदर्भ फ़ील्ड बनाना चाहता हूं। जैसे कोई ड्रूपल पेज पर किसी लिंक पर क्लिक करता है, एक इकाई संदर्भ फ़ील्ड स्वचालित रूप से पृष्ठ नोड प्रकार में बनाई जाएगी।प्रोग्रामिंग के रूप में Drupal 8 में एक इकाई संदर्भ फ़ील्ड कैसे बनाएं?

क्या कोई इस से मेरी सहायता कर सकता है?

उत्तर

-1

आप से https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!Plugin!DataType!EntityReference.php/8

मदद मिल या आप नमूना इकाई उदाहरण भी https://www.drupal.org/project/examples

डाउनलोड कर सकते हैं या फिर आप मेरी विधि का उपयोग कर सकते हैं कर सकते हैं भी जो के रूप में नीचे है:

मॉड्यूल content_entity_example सेट करें। info.yml

name: Content Entity Example 
type: module 
description: 'Provides ContentEntityExampleContact entity.' 
package: Example modules 
core: 8.x 
# These modules are required by the tests, must be available at bootstrap time 
dependencies: 
    - options 
    - entity_reference 
    - examples 

ड्रूपल 8

में सामग्री इकाई प्रकार बनाना, हम लोगों (संपर्क) को जोड़ने, संपादित करने और हटाने के लिए 'संपर्क' इकाई बनायेंगे। यह पूरी तरह से fieldable है और Drupal में नई इकाई अवधारणाओं के सबसे उपलब्ध का उपयोग करता है 8.

content_entity_example.routing.yml

# This file brings everything together. Very nifty! 

# Route name can be used in sevaral place (links, redirects, local actions etc.) 
entity.content_entity_example_contact.canonical: 
    path: '/content_entity_example_contact/{content_entity_example_contact}' 
    defaults: 
    # Calls the view controller, defined in the annotation of the contact entity 
    _entity_view: 'content_entity_example_contact' 
    _title: 'Contact Content' 
    requirements: 
    # Calls the access controller of the entity, $operation 'view' 
    _entity_access: 'content_entity_example_contact.view' 

entity.content_entity_example_contact.collection: 
    path: '/content_entity_example_contact/list' 
    defaults: 
    # Calls the list controller, defined in the annotation of the contact entity. 
    _entity_list: 'content_entity_example_contact' 
    _title: 'Contact List' 
    requirements: 
    # Checks for permission directly. 
    _permission: 'view contact entity' 

content_entity_example.contact_add: 
    path: '/content_entity_example_contact/add' 
    defaults: 
    # Calls the form.add controller, defined in the contact entity. 
    _entity_form: content_entity_example_contact.add 
    _title: 'Add Contact' 
    requirements: 
    _entity_create_access: 'content_entity_example_contact' 

entity.content_entity_example_contact.edit_form: 
    path: '/content_entity_example_contact/{content_entity_example_contact}/edit' 
    defaults: 
    # Calls the form.edit controller, defined in the contact entity. 
    _entity_form: content_entity_example_contact.edit 
    _title: 'Edit Contact' 
    requirements: 
    _entity_access: 'content_entity_example_contact.edit' 

entity.content_entity_example_contact.delete_form: 
    path: '/contact/{content_entity_example_contact}/delete' 
    defaults: 
    # Calls the form.delete controller, defined in the contact entity. 
    _entity_form: content_entity_example_contact.delete 
    _title: 'Delete Contact' 
    requirements: 
    _entity_access: 'content_entity_example_contact.delete' 

content_entity_example.contact_settings: 
    path: 'admin/structure/content_entity_example_contact_settings' 
    defaults: 
    _form: '\Drupal\content_entity_example\Form\ContactSettingsForm' 
    _title: 'Contact Settings' 
    requirements: 
    _permission: 'administer contact entity' 

की 'क्लिक' अनुभाग में परिभाषित कार्यों के लिए मार्ग के नाम रूटिंग इकाई एनोटेशन सही पैटर्न का पालन करना चाहिए। विवरण के लिए कृपया नीचे सामग्री इकाई वर्ग देखें।

content_entity_example.links.menu.yml

मार्ग फ़ाइल साथ संयोजन में, इस मॉड्यूल के लिए hook_menu बदल देता है।

# Define the menu links for this module 

entity.content_entity_example_contact.collection: 
    title: 'Content Entity Example: Contacts Listing' 
    route_name: entity.content_entity_example_contact.collection 
    description: 'List Contacts' 
    weight: 10 
content_entity_example_contact.admin.structure.settings: 
    title: Contact Settings 
    description: 'Configure Contact entity' 
    route_name: content_entity_example.contact_settings 
    parent: system.admin_structure 

content_entity_example.links.action.yml

# All action links for this module 

content_entity_example.contact_add: 
    # Which route will be called by the link 
    route_name: content_entity_example.contact_add 
    title: 'Add Contact' 

    # Where will the link appear, defined by route name. 
    appears_on: 
    - entity.content_entity_example_contact.collection 
    - entity.content_entity_example_contact.canonical 
+0

इस बनाने के लिए कोड हो रहा है एक कस्टम इकाई प्रकार? लेकिन, सवाल एक इकाई संदर्भ क्षेत्र बनाने के बारे में है। – Christian

0
$node = new stdClass(); 
$node->title = "YOUR TITLE"; 
$node->type = "YOUR_NODE_TYPE"; 
.... 
$node->field_customer_nid[$node->language][]['target_id'] = $form_state['values']['entity id']; 


... 
node_submit($node); 
node_save($node); 
+0

आप इसमें कुछ साथ टेक्स्ट जोड़ना चाहेंगे। यह स्पष्ट रूप से स्पष्ट नहीं है कि आप क्या दिखा रहे हैं। – Christian

0

यह article बंडल के साथ node सामग्री प्रकार के लिए एक इकाई के संदर्भ क्षेत्र का निर्माण करेगा।

$form['node_id'] = array(
    '#type' => 'entity_autocomplete', 
    '#title' => $this->t('Node'), 
    '#target_type' => 'node', 
    '#selection_settings' => ['target_bundles' => ['article']], 
    '#tags' => TRUE, 
    '#size' => 30, 
    '#maxlength' => 1024, 
); 

नोट आप taxonomy_term जैसे अन्य संस्थाओं के लिए target_type बदल सकते हैं।

0

तो मैं उम्मीद है कि इस तेजी से प्राप्त कर सकते हैं अगर मैं फिर से करने की जरूरत है ...

क्षेत्र भंडारण बनाएँ:

if (!$field_storage = FieldStorageConfig::loadByName($entity_type, $field_name)) { 
    FieldStorageConfig::create([ 
    'field_name' => $field_name, 
    'entity_type' => $entity_type, 
    'type' => $type, 
    'cardinality' => -1, 

    // Optional to target entity types. 
    'settings' => [ 
     'target_type' => $entity_target_type, // Ex: node, taxonomy_term. 
    ], 
    ])->save(); 
} 

बनाएं क्षेत्र:

FieldConfig::create([ 
    'field_name' => $field_name, 
    'entity_type' => $entity_type, 
    'bundle' => $bundle, 
    'label' => $label, 
    'cardinality' => -1, 

    // Optional to target bundles. 
    'settings' => [ 
    'handler' => 'default', 
    'handler_settings' => [ 
     'target_bundles' => [ 
     $vid1, 
     $vid2, 
     ], 
    ], 
    ], 
])->save(); 
+0

क्या आपको उपरोक्त कोड पर कोई आधिकारिक दस्तावेज मिला है? प्रमुख मूल्य दिखाना? यह ध्यान दिया जाना चाहिए कि "सेटिंग्स" सेटिंग्स पृष्ठ पर पाए गए फॉर्म फ़ील्ड नामों को मैप करेगा (कुछ हद तक)। तो किसी दिए गए सामग्री प्रकार के लिए चेकबॉक्स चेक करने के लिए; 'पृष्ठ' => 'पृष्ठ''। – zkolnik

+0

मैं ऑनलाइन अधिक दस्तावेज नहीं ढूंढ पाया, जो यहां पोस्ट किया गया एक प्रमुख कारण है। हालांकि कोर फील्ड मॉड्यूल का 'EntityReferenceAutoCreateTest' क्लास' में इसका एक अच्छा उदाहरण है। उदाहरण 'target_bundles' और 'target_type' का भी उपयोग करता है। साथ ही, यदि आप सेटिंग पृष्ठ देखते हैं तो चेकबॉक्स भी करते हैं। – sareed

संबंधित मुद्दे