2012-11-13 10 views
6

मेरे पास कुछ फ़ील्ड संग्रह हैं जो नोड प्रकार से जुड़े हुए हैं और मैं ड्रूपल के फॉर्म एपीआई का उपयोग करके कस्टम फॉर्म लिखने की प्रक्रिया में हूं।एक ड्रूपल फॉर्म में फ़ील्ड संग्रह को जोड़ने पर एक उदाहरण की आवश्यकता है - ड्रूपल 7

मैं इन फ़ील्ड संग्रह फ़ील्ड को फ़ॉर्म में लोड करने के बारे में कैसे जा सकता हूं? क्या मुझे hook_form, hook_field, या कुछ और उपयोग करना चाहिए?

क्या कोई मुझे एक फॉर्म तत्व लिखने का एक सरल उदाहरण प्रदान कर सकता है जो असीमित कार्डिनिटी के साथ फ़ील्ड संग्रह है?

उत्तर

0

मैं फ़ील्ड संग्रह फ़ॉर्म लोड करने के लिए निम्न कोड का उपयोग करने में सक्षम था। मैंने अतिरिक्त टिप्पणियां जोड़ दी हैं और कस्टम तर्क को हटा दिया है जो $ form_state ['values'] चर से फ़ील्ड मानों के लिए शिकार करता है।

function mymodule_custom_form($form, $form_state, $args){ 
    $form = array(); 
    $type = $args['type']; 
    $id = $args['id']; 
    module_load_include('inc', 'field_collection', 'field_collection.pages'); 
    if(!empty($entity->field_comments[LANGUAGE_NONE][0]['value'])){ 
    $field_collection_item = field_collection_item_load($entity->field_comments[$entity->language][0]['value'], TRUE); 
    $output = drupal_get_form('field_collection_item_form', $field_collection_item); 
    }else{ 
    $output = field_collection_item_add('field_comments', 'node', $entity->nid); 
    } 
    dpm($output); 
    // If you want to use field collection form submit handlers as is, just return the below 
    // In case you want to use your own custom submit handlers then do modify the $output array as seems fit. 
    $form['field_collection_element'] = $output; 
    // You may also attach the $output array to your custom form array. Make sure you handle submit handlers properly 

    return $form; 
} 

एक नमूना हैंडलर प्रस्तुत @see में हैंडलर प्रस्तुत आप उदाहरण में here

function mymodule_custom_form_submit($form, $form_state){ 
    ... 
    if(empty($item_id)){ 
    $field_collection_item = entity_create('field_collection_item', array('field_name' => $field_name)); 
    $field_collection_item->setHostEntity('node', $node); 
    } 
    else { 
    $items = entity_load('field_collection_item', array($item_id)); 
    $field_collection_item = $items[$item_id]; 
    } 
    if(is_object($field_collection_item)){ 
    // for references. 
    $field_collection_item->field1[$node->language][0]['target_id'] = $field1_val; 
    // normal values 
    $field_collection_item->field2[$node->language][0]['value'] = $field2_val; 
    $field_collection_item->field3[$node->language][0]['value'] = $field3_val; 
    if(empty($item_id)){ 
     $field_collection_item->save(FALSE); 
    } 
    else{ 
     $field_collection_item->save(TRUE); 
    } 
    } 
    ... 
} 
0

आप एक फ़ॉर्म का उपयोग करके सामग्री के साथ एक नोड पॉप्युलेट करने के लिए देख रहे हैं दिया उपयोग कर सकते हैं, तो आप हमेशा का पर्दाफाश कर सकता है उपयोगकर्ता पहुंच के आधार पर तत्वों को छिपाने के लिए उपयोगकर्ता और hook_form_alter के लिए नोड फॉर्म।

या यदि आप एक क्षेत्रीय रूप की तलाश में हैं, तो आप entityform का उपयोग कर सकते हैं और फिर अन्य सामग्री को पॉप्युलेट करने के लिए नियमों का उपयोग कर सकते हैं।

0

वहाँ भी

Fieldset

यह फार्म आइटम के एक समूह फ़ॉर्मेट करने देता है कहा जाता Drupal रूपों में वर्गीकृत किया क्षेत्रों में से एक और अवधारणा है। फॉर्म एपीआई पेज पर उदाहरण नीचे दिए गए कोड के साथ संपर्क मॉड्यूल के लिए है।

function contact_form_user_profile_form_alter(&$form, &$form_state) { 
    if ($form['#user_category'] == 'account') { 
    $account = $form['#user']; 
    $form['contact'] = array(
     '#type' => 'fieldset', 
     '#title' => t('Contact settings'), 
     '#weight' => 5, 
     '#collapsible' => TRUE, 
    ); 
    $form['contact']['contact'] = array(
     '#type' => 'checkbox', 
     '#title' => t('Personal contact form'), 
     '#default_value' => !empty($account->data['contact']) ? $account->data['contact'] : FALSE, 
     '#description' => t('Allow other users to contact you via a <a href="@url">personal contact form</a> which keeps your e-mail address hidden. Note that some privileged users such as site administrators are still able to contact you even if you choose to disable this feature.', array('@url' => url("user/$account->uid/contact"))), 
    ); 
    } 
} 

इस प्रकार अगर आपका इरादा togather क्षेत्रों में से सिर्फ समूह विभिन्न प्रकार के लिए है तो यह भी यह करने के लिए एक तरीका है।

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