2017-04-18 2 views
0

Ich bin sehr neu zu Drupal, und versuche, Daten aus einem Formular zu meiner Druple-Installation zu posten.Beitrag zu einem Knoten Drupal 7

Ich habe meine Dienste Modul eingerichtet und verfügen über integrierte Einrichtung eines Basisknoten

Mein Knoten

sites/all/modules/custom/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!')); 
    } 
} 

dies ist nun aktiv in meiner Services Resource als bd_profile und create geprüft

mein Weg zum Endpunkt: bensapi

Und nächstes habe ich eine einfache Form haben, dass 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> 

endlich mein neuer Inhaltstyp ist wie so

01 einrichten

enter image description here

Sobald ich poste ich nur

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

Ich bin neu in Drupal und weiß nicht, wo ich falsch gegangen sind, jede Hilfe diese Arbeit wäre gut zu bekommen.

Danke.

Antwort

0

ich keine Dienste verwendet haben, aber für die Erstellung von Endpoint-Seiten können Sie verwenden hook_menu:

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

Diese Arbeit sollte:

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

.. wo "add_node" ist Pfad der Endpunkt. Sie können anderen Wert legen, wenn Sie natürlich möchten.

Verwandte Themen