2011-11-25 91 views
3

我有我自己的內容類型在我的模塊中。它有文字和圖像字段。添加表單元素到用戶輸入的節點drupal 7

我現在想要添加一個表單字段,比如單選框和一個提交按鈕,這樣當節點呈現時,用戶可以給他們輸入。我無法做到這一點。

1) 我第一次嘗試失敗,當我嘗試創建我的內容類型時添加一個電臺框。

simplequiz_radio11' => array(
'field_name' => 'simplequiz_radio11, 
**'type'  => 'radio',** 

除了文本和圖像,我在類型字段中寫什麼。我想再次提到,當節點不處於編輯模式時,無線電盒需要接受用戶輸入。

2)我試過使用drupal_get_forms,我可以使用drupal_get_forms將節點和元素附加到節點,但是我的節點有很多圖像(字段),我需要每個圖像的表單元素,所以它不太實用就這樣。以下是我的代碼:

function simplequiz_node_view($node, $view_mode, $langcode) 
{ 
$f = drupal_get_form('simplequiz_form', $node); 
$f['#weight'] = 100; 
$node->content['data_collection_form'] = $f; 
} 

這工作正常,但我怎麼能做到這一點說10個圖像字段?看起來第一種選擇更好,但我似乎無法得到正確的字段類型。

//我設法解決它,因爲我的邏輯錯了我需要使用formater而不是drupal中的字段。

function simplequiz_field_formatter_info() { 
return array(

'simplequiz_example_field' => array(
    'label' => t('radio button on node'), 
    'field types' => array('text'), 
), 
'simplequiz_btn_field' => array(
    'label' => t('button on node'), 
    'field types' => array('text'), 
), 

); 


} 

/** 
* Implements hook_field_formatter_view(). 
*/ 


function simplequiz_field_formatter_view($object_type, $object, $field, $instance,  
$langcode, $items, $display) { 
switch ($display['type']) { 
case 'simplequiz_example_field': 
    $element[0]['#type'] = 'markup'; 
    $element[0]['#markup'] = theme('simplequiz_example_field', array('value' => $items[0] 
['safe_value'])); 
    break; 

case 'simplequiz_btn_field': 
    $element[0]['#type'] = 'markup'; 
    $element[0]['#markup'] = theme('simplequiz_btn_field', array('value' => $items[0] 
['safe_value'])); 
    break; 
} 
return $element; 
} 

/** 
* Theme the example field. 
*/ 
function theme_simplequiz_example_field($variables) { 
return '<form><input type="radio" name="type" value="silver">Gold' 
.$variables['value']. ' </br> <input type="radio" name="type" value="Gold"> Silver </br> 
</form>' 

    ; 
} 

function theme_simplequiz_btn_field($variables) { 
return '<input type="submit" class="form-submit" value="Submit" name="op" id="edit- 
submit--2">' 

    ; 
} 

乾杯, 維沙爾

+0

你做這個節點視圖頁面或節點編輯頁面上?如果它是編輯頁面,您可以使用無限基數的字段 – James

+0

我在節點視圖頁面上執行此操作。我解決了它 –

+0

請發佈您的解決方案並接受它作爲您的解決方案,以便其他具有類似問題的人員可以找到解決方案。 – KerrM

回答

0

//我設法解決它作爲我的邏輯是錯誤的,我需要與所述格式化,而不是在Drupal下地幹活。

function simplequiz_field_formatter_info() { 
return array(

'simplequiz_example_field' => array(
    'label' => t('radio button on node'), 
    'field types' => array('text'), 
), 
'simplequiz_btn_field' => array(
    'label' => t('button on node'), 
    'field types' => array('text'), 
), 

); 


} 

/** 
* Implements hook_field_formatter_view(). 
*/ 


function simplequiz_field_formatter_view($object_type, $object, $field, $instance,  
$langcode, $items, $display) { 
switch ($display['type']) { 
case 'simplequiz_example_field': 
    $element[0]['#type'] = 'markup'; 
    $element[0]['#markup'] = theme('simplequiz_example_field', array('value' => $items[0] 
['safe_value'])); 
    break; 

case 'simplequiz_btn_field': 
    $element[0]['#type'] = 'markup'; 
    $element[0]['#markup'] = theme('simplequiz_btn_field', array('value' => $items[0] 
['safe_value'])); 
    break; 
} 
return $element; 
} 

/** 
* Theme the example field. 
*/ 
function theme_simplequiz_example_field($variables) { 
return '<form><input type="radio" name="type" value="silver">Gold' 
.$variables['value']. ' </br> <input type="radio" name="type" value="Gold"> Silver </br> 
</form>' 

    ; 
} 

function theme_simplequiz_btn_field($variables) { 
return '<input type="submit" class="form-submit" value="Submit" name="op" id="edit- 
submit--2">' 

    ; 
} 

乾杯, 維沙爾