2017-04-18 109 views
0

我很新的Drupal的,和我試圖從形式到我的druple安裝後的數據。投遞到一個節點的Drupal 7

我已經建立了我的服務模塊設置和建立了一個基本的節點

我的節點

網站/所有/模塊/自定義/ bd_profile/bd_profile.module

bd_profile.module

<?php 

function bd_profile_services_resources() { 
    return array(
     'bd_profile' => array(       // My new resource 
      'create' => array(
       'callback' => '_bd_profile_create_node', 
       'access callback' => '_bd_profile_create_access', 
       'args' => array(
        array(
         'name' => 'node', 
         'optional' => FALSE, 
         'source' => 'data',       // Setting the source to 'data' in your args means that any data in the POST will be passed to the callback function 
         'description' => 'The node data to create', 
         'type' => 'array', 
        ), 
       ), 
      ), 
     ), 
    ); 
} 

/** 
* Access callback 
*/ 
function _bd_profile_create_access() { 
    return TRUE; 
} 

/** 
* Callback function that creates the node 
*/ 
function _bd_profile_create_node($arg) { 
    // Minimally there needs to be something submitted 
    if($arg) { 
     // Create the node 
     $node = new stdClass(); 
     $node->type = 'bio'; 
     $node->title = $arg['name']; 
     $node->language = LANGUAGE_NONE; 
     $node->uid = 0; 
//  $node->uid = $user->uid; 
//  $node->status = 1; //(1 or 0): published or not 
//  $node->promote = 0; //(1 or 0): promoted to front page 
//  $node->comment = 1; // 0 = comments disabled, 1 = read only, 2 = read/write 
     node_object_prepare($node); 

     // Create a map of predefined POST args to Drupal fields 
     $map = array(
      'job_title' => 'field_title', 
      'message' => 'body', 
     ); 

     // Array to store both mapped and unmapped fields 
     $node_fields = array(); 

     // What predefined args have been passed? 
     $arr1 = array_intersect_key($arg, $map); 
     // Build an array associating Drupal fieldnames to arg values 
     foreach($arr1 as $key => $value) { 
      $field = $map[$key];     // Get the drupal field that matches the form field 
      $node_fields[$field] = $value; 
     } 

     // What undefined (ie. unknown) args have been passed? 
     $arr2 = array_diff_key($arg, $map); 
     // Associate unknown arg values with the 'general info' field on our bio/profile pages 
     foreach($arr2 as $key => $value) { 
      if(isset($node_fields['field_general_info'])) { 
       $node_fields['field_general_info'] .= $key . " | " . $value . "\n"; 
      } else { 
       $node_fields['field_general_info'] = $key . " | " . $value . "\n"; 
      } 
     } 

     // Save all arg values as Drupal fields 
     foreach($node_fields AS $key => $value) { 
      $node->{$key}[$node->language][0]['value'] = $value; 
     } 

     // Save the node 
     $node = node_submit($node); 
     node_save($node); 
    } else { 
     // Error if no args were passed 
     return services_error(t('No data submitted!'), 406, t('No data submitted!')); 
    } 
} 

這是現在我的服務資源爲bd_profile積極創建檢查

我的路徑端點:bensapi

而接下來我有一個簡單的表格,POST

myform.html

<form action="http://localhost/myapp/bensapi/node" method="post" enctype="multipart/form-data"> 
<table width="50%"> 
<tr> 
    <td>Your name:</td><td><input name="name" type="text" /></td> 
</tr> 
<tr> 
    <td>Job title:</td><td><input name="job_title" type="text" /></td> 
</tr> 
<tr> 
    <td>Age:</td><td><input name="age" type="text" /></td> 
</tr> 
<tr> 
    <td>Hometown:</td><td><input name="hometown" type="text" /></td> 
</tr> 
<tr> 
    <td>Hometown state:</td><td><input name="state" type="text" /></td> 
</tr> 
<tr> 
    <td>Bio/message:</td><td><textarea name="message"></textarea></td> 
</tr> 
<tr> 
    <td colspan="2">&nbsp;</td> 
</tr> 
<tr> 
    <td colspan="2"><input type="submit" value="Save">&nbsp; 
    <input type="reset" value="Clear"></td> 
</tr> 
</table> 
</form> 

終於我的新內容類型設置好像這樣

enter image description here

一旦我後我只是得到

<result>Node bd_profile could not be found</result> 

我是新來的Drupal,不知道我在哪裏出了問題,任何幫助,得到這個工作將是一件好事。

謝謝。

回答

0

我沒有使用服務,但用於創建端點的頁面,你可以使用hook_menu:

https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_menu/7.x

這應該工作:

function bd_profile_menu() { 
    $items['add_node'] = array(
     'title' => 'Adding a node', 
     'page callback' => '_bd_profile_create_node', 
     'access arguments' => array('access content'), 
    ); 
} 

..其中 「add_node」 是你的路徑終點。如果你喜歡,你可以放置其他的價值。