2012-09-10 48 views
-1

我想創建一個模塊,在邏輯位置創建commerc_paypal_wps表單的簡單副本,以便用戶不必查看規則表單或元素。從規則元素返回drupal_get_form

我無法拉入表單值然後保存它們。

我的代碼是:

<?php 

//Create the admin page 
function sb_paypal_settings_menu() { 

$items['admin/commerce/config/paypal'] = array(
    'title' => 'Paypal settings', 
    //'description' => 'Set values for your site such as site name.', 
    'page callback' => 'drupal_get_form', 
    'page arguments' => array('site_settings_admin_1'), 
    'access arguments' => array('administer paypal settings'), 
    'file path' => drupal_get_path('module', 'rules_admin'), 
    'file' => 'rules_admin.inc', 
    'type' => MENU_NORMAL_ITEM, 
); 
return $items; 

} 

//Valid permissions for this module 
function sb_paypal_settings_permission() { 
return array(
'administer paypal settings' => array(
    'title' => t('Administer Paypal settings')  
), 
); 
} 

//Create the form 
function site_settings_admin_1() { 

$form = drupal_get_form('commerce_paypal_wps_settings_form'); 


unset($form['ipn_logging']); 

$form['submit'] = array(
'#type' => 'submit', 
'#value' => t('Submit') 
); 

return($form); 

} 

該模塊是代碼爲:http://drupalcontrib.org/api/drupal/contributions!commerce_paypal!modules!wps!commerce_paypal_wps.module/function/commerce_paypal_wps_settings_form/7

窗體需要被存儲在rules_config表中的信息。

+0

簡短的回答:你不能從一個函數本身被'drupal_get_form稱爲()中調用'drupal_get_form()''...它剛剛獲得」工作。你需要實現'hook_form_alter()'來改變形式 – Clive

+0

HI,我們得到了這個購買創建一個新的形式,然後unserialising數組然後使用一個字段來改變我們需要的位,然後保存序列化它。 –

回答

0

解決方案是創建一個模塊來改變w值。爲.module文件中的代碼如下:

<?php 
// $Id$ 

//Create the admin page 
function paypal_settings_menu() { 
$items['admin/commerce/config/paypal_settings'] = array(
    'title' => 'Paypal payment settings', 
    'description' => 'Add your paypal email address', 
    'page callback' => 'drupal_get_form', 
    'page arguments' => array('paypal_settings_admin'), 
    'access arguments' => array('administer paypal payment settings'), 
    'file path' => drupal_get_path('module', 'rules_admin'), 
    'file' => 'rules_admin.inc', 
    'type' => MENU_NORMAL_ITEM, 
); 
return $items; 
} 

//Valid permissions for this module 
function paypal_settings_permission() { 
return array(
'administer paypal payment settings' => array(
    'title' => t('Administer Paypal payment settings')  
), 
); 
} 

//Create the form 
function paypal_settings_admin() { 

$query = db_query("SELECT * FROM rules_config WHERE name='commerce_payment_paypal_wps'")->fetchAll(); 
if (count($query) == 1) { 
$settings = unserialize($query[0]->data); 
} 


$pay_settings; 

$actions = $settings->actions(); 

foreach ($actions as $action) { 
$pay_settings = &$action->settings['payment_method']['settings']; 
break; 
} 

$saveval = serialize($settings); 

$form['business_email'] = array(
'#type' => 'textfield', 
'#title' => t('PayPal e-mail address'), 
'#description' => t('The e-mail address you use for the PayPal account you want to receive payments.'), 
'#default_value' => $pay_settings['business'], 
'#required' => TRUE, 
); 

$form['submit'] = array(
'#type' => 'submit', 
'#value' => t('Submit'), 
); 
$form['#submit'][] = 'paypal_settings_form_submit'; 
return $form; 

} 

function paypal_settings_form_submit(&$form, &$form_state) { 

$query = db_query("SELECT * FROM rules_config WHERE name='commerce_payment_paypal_wps'")->fetchAll(); 

if (count($query) == 1) { 
$settings = unserialize($query[0]->data); 
} 

$pay_settings = null; 

$actions = $settings->actions(); 

foreach ($actions as $action) { 
$pay_settings = &$action->settings['payment_method']['settings']; 
break; 
} 

$pay_settings['business'] = $form_state['values']['business_email']; 

$insertquery = db_update('rules_config') 
->fields(array(
'data' => serialize($settings), 
)) 
->condition('name', 'commerce_payment_paypal_wps') 
->execute(); 

}