1

我对 Drupal (7) 和 PHP 比较陌生,所以这可能是一个基本的问题,因为到目前为止我还没有设法找出我需要的问题,因此发布了这篇文章。

我有一个 drupal 站点,它将连接到一个简单的 CRUD REST webservice(.net 4) 以检索和发布数据。

我一直发现很难确定如何将从网站获得的数据加载到一个简单的 drupal 表单中。数据将通过 JSON 格式传输,但我不确定如何从响应数组中检索数据并使用从网站获得的数据填充表单的字段。假设返回的 JSON 字段与表单字段名称匹配。

在我的示例中,我在提交函数中有 http_request,实际上它应该在页面加载函数中。

所以问题是:如何从检索到的数据中填充表单的字段?从 json_array 中获取数据的语法是什么?页面加载时需要检索数据,实现这一点的最佳方法是什么?

这是示例代码,我一直在提交。我已经重建了,这是将数据保留在屏幕上的最佳方式吗?我试图将“姓氏”字段重置为新值,但没有任何乐趣..

<?php

/**
* @file
* Test activation form
*/

/**
* Implements hook_menu()
*/
function activation_menu() {
  $items['activation'] = array(
  'title' => 'Registration form',
  'page callback' => 'drupal_get_form',
  'page arguments' => array('activation_nameform'),
  'access callback' => TRUE,
 // 'access_callback' => TRUE,
  'type' => MENU_NORMAL_ITEM
  );
  return $items;
  }

/**
* Define the form
*/
function activation_nameform() {
$form['Firstname'] = array(
'#Firstname' => t('Firstname'),
'#type' => 'textfield',
'#maxlength'=> 50,
'#description' => t('Please enter your First name.'),
);

$form['Surname'] = array(
'#Surname' => t('Surname'),
'#type' => 'textfield',
'#maxlength'=> 50,
'#description' => t('Please enter your Surname.'),
);

$form['Username'] = array(
'#Username' => t('Username'),
'#type' => 'textfield',
'#maxlength'=> 100,
'#description' => t('Please enter your Username.'),
);
$form['Password'] = array(
'#Password' => t('Password'),
'#type' => 'textfield',
'#maxlength'=> 8,
'#description' => t('Please enter your Surname.'),
);

$form['Organisation'] = array(
'#Organisation' => t('Organisation'),
'#type' => 'textfield',
'#maxlength'=> 50,
'#description' => t('Please enter your Organisation.'),
);

$form['Address1'] = array(
'#Address1' => t('Address1'),
'#type' => 'textfield',
'#maxlength'=> 150,
'#description' => t('Please enter your first full line of Address.'),
);

$form['Address2'] = array(
'#Address2' => t('Address'),
'#type' => 'textfield',
'#maxlength'=> 150,
'#description' => t('Please enter your second full line of Address.'),
);

$form['Town_city'] = array(
'#Town_city' => t('Town_city'),
'#type' => 'textfield',
'#maxlength'=> 50,
'#description' => t('Please enter your Town or City.'),
);

$form['Region'] = array(
'#Region' => t('Region'),
'#type' => 'textfield',
'#maxlength'=> 50,
'#description' => t('Please enter your Region.'),
);

$form['PostCode'] = array(
'#PostCode' => t('PostCode'),
'#type' => 'textfield',
'#maxlength'=> 10,
'#description' => t('Please enter your PostCode.'),
);



$form['Postcode'] = array(
'#Postcode' => t('Postcode'),
'#type' => 'textfield',
'#maxlength'=> 15,
'#description' => t('Please enter your Postcode or ZIP code.'),
);

$form['Country_options'] = array(
'#type' => 'value',
'#value'=> array(t('UK'),t('Europe'),t('USA')),
);

$form['Country']['favourite_country'] = array(
'#title' => t('Favourite Country'),
'#type' => 'select',
'#value'=> array(t('UK'),t('Europe'),t('USA')),
'#description' => t('Please enter your Country'),
'#options' => $form['Country_options']['#value']
);

$form['Status'] = array(
'#Status' => t('Status'),
'#type' => 'textfield',
'#maxlength'=> 150,
'#description' => t('Please enter your first full line of Address.'),
);

$form['Add_credits'] = array(
'#Add_credits' => t('Add_credits'),
'#type' => 'textfield',
'#maxlength'=> 15,
'#description' => t('Please enter additional credits'),
);


$form['TandC'] = array(
'#TandC' => t('TandC'),
'#type' => 'textfield',
'#maxlength'=> 5,
'#description' => t('TandC'),
'#value'=> 30
);

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


/**
* Handle post - validation form submission
*/
function activation_nameform_submit($form,&$form_state) {
$name = $form_state['values']['Firstname'];
drupal_set_message(t('Thanks ,%name',
array('%name' => $name)));
$request = drupal_http_request('http://somewebsite.com/Customers/?ID=10');
$jsonarray =drupal_json_decode($request->data);

$form_state['rebuild']=TRUE;
$form['Surname']['#value']='Hello mother';

}

通过使用继续回答

echo '<pre>';var_dump($request); echo '</pre>'

我现在可以看到请求信息:

["data"]=>
  string(346) "?{"CustomerID":10,"FirstName":"Howard Philip","LastName":"Lovecraft","Organisation":"superglue","AddressID":null,"Status":0,"Credits":0,"Type":0,"ExpiryDate":"0001-01-01T00:00:00","UserName":"xxx@hotmail.com","Password":"","CustomerIconURL":"","CountryCode":"","LastUpdated":null,"DateCreated":"2013-04-18T07:43:10.123","ApplicationID":null}"
  ["protocol"]=>
  string(8) "HTTP/1.1"
  ["status_message"]=>
  string(2) "OK"
  ["headers"]=>
  array(6) {
    ["content-length"]=>
    string(3) "346"
    ["content-type"]=>
    string(31) "application/json; charset=utf-8"
    ["server"]=>
    string(17) "Microsoft-IIS/7.5"
    ["x-powered-by"]=>
    string(7) "ASP.NET"
    ["date"]=>
    string(29) "Fri, 14 Jun 2013 12:00:35 GMT"
    ["connection"]=>
    string(5) "close"
  }
  ["code"]=>
  string(3) "200"
}

所以现在将实施以下建议......

4

2 回答 2

1

..所以这个问题的完整答案是,填充下面显示的字段的代码/**

* Define the form
*/
function activation_nameform() {
$request = drupal_http_request('http://somewebsite.com/ML5/MLSvrSvc.svc/Customers/?ID=10');
$jsonarray = $request->data; 
//$jsonarray =json_decode('{"CustomerID":10,"FirstName":"Howard Philip","LastName":"Lovecraft","Organisation":"superglue","AddressID":null,"Status":0,"Credits":0,"Type":0,"ExpiryDate":"0001-01-01T00:00:00","UserName":"xxx@hotmail.com","Password":"","CustomerIconURL":"","CountryCode":"","LastUpdated":null,"DateCreated":"2013-04-18T07:43:10.123","ApplicationID":null}',TRUE);
echo '<pre2>';var_dump($jsonarray); echo '</pre2>';

$form['Firstname'] = array(
'#Firstname' => t('FirstName'),
'#type' => 'textfield',
'#maxlength'=> 50,
'#description' => t('Please enter your First name.'),
'#default_value' => $jsonarray['FirstName'],
);
.
.
.etc

然而,来自 windows 7 /.net 64 位系统的 json 存在解析问题,因此测试 json 字符串......已经尝试了大多数 utf8 编码函数和 preg_replace 示例,但今天仍然没有进展......也许另一个问题,我们会看到。

于 2013-06-14T16:01:00.730 回答
0

在我的示例中,我在提交函数中有 http_request,实际上它应该在页面加载函数中。

我会这样做

1)在定义表单时获取值,即activation_nameform() 2)使用键默认值来分配它。

'#default_value' => // your variable here

3) 执行 printr 函数执行 xdebug session 以确保 json 对象正在被转换。我rem我之前有一些问题。

如果上述方法不起作用,那么我会研究 hook_form_alter 函数。

4) 返回类型是一个数组,所以你可以访问它 $var[1]

$request = drupal_http_request('https://api.twitter.com/1/users/show.json? screen_name=TwitterAPI&include_entities=true');

$var= drupal_json_decode($request->data);
print $var[1]; // this should contain your value and then you can place for the default value.

参考 - http://webwash.net/articles/getting-started-json-drupal-7 https://api.drupal.org/api/drupal/includes!common.inc/function/drupal_json_decode/7 http:// in3.php.net/json_decode

于 2013-06-13T18:43:47.170 回答