drupal - 当用户在 webform 中提交值并单击提交时如何创建节点?

标签 drupal drupal-6

我的网站(D6)有“快速查询表单”(使用 webform)& 当用户单击 webform 中的提交按钮时,我需要同时创建一个新节点。

当用户单击提交按钮时,我如何从 webform 获取值并将值插入节点。

请建议我如何做到这一点!

最佳答案

您可以通过创建自定义模块来完成此操作。该模块将具有两个功能:

  • HOOK_form_alter - 向现有网络表单添加额外的提交功能。
  • 自定义函数 - 自定义函数(从 HOOK_form_alter 调用)从提交的 web 表单中获取值并创建节点。

  • 在创建模块之前,您应该使用核心节点模块和 CCK 创建一个内容类型,其中包含所有与 Web 表单相同的字段。

    HOOK_form_alter

    在下面的示例中,将模块名称替换为 MODULENAME,并将您的网络表单的 ID 替换为 switch case 中的 XXX。此函数将 MODULENAME_create_node 添加到您的网络表单的提交函数数组中。我们将在下面定义 MODULENAME_create_node。
    <?php
    function MODULENAME_form_alter(&$form, $form_state, $form_id) {
      switch ($form_id) {
    
        case 'webform_client_form_XXX' :
        $first = array_shift($form['#submit']);
        array_unshift($form['#submit'], $first, 'MODULENAME_create_node');
    
        break;
    
      }
    }
    

    MODULENAME_create_node

    这是将创建节点的主要功能。
    <?php
    function MODULENAME_create_node() {
      // Load all of the data submitted via the webform into a keyed array ($data)
      $data = array();
      foreach ($form_state['values']['submitted_tree'] as $key => $value) {
        $data[$key] = $value;
      }
    
      // The node_save() function (called later in this function) call
      // node_access_check, which means that anonymous calls to this function will
      // not be successful. Top get around this, we load user1 while executing this
      // function, then restore the user back to the original state at the end of
      // the function.
      global $user;
      $original_user = $user;
      $user = user_load(1);
    
      // Initialize the new node with default stuff
      $node = new stdClass();
      $node->type = 'YOUR_CONTENT_TYPE';
      $node->created = time();
      $node->changed = $node->created;
      $node->status = 1;
      $node->promote = 0;
      $node->sticky = 0;
      $node->format = 1;
      $node->uid = $user->uid;
    
      // You'll need to customize this based on what you named your webform and CCK fields.
      // Remember that all of the webform data is available and stored in the $data array.
      $node->title = $data['title'];
      $node->field_myfield1[0]['value'] = $data['myfield1'];
      $node->field_myfield2[0]['value'] = $data['myfield2'];
    
      //Save the node
      node_save($node);
    
      //Set the user state back to the original
      $user = $original_user;
    
    }
    

    您还需要为您的模块创建一个信息文件。参见 Drupal 的文档 writing info files如果你不熟悉。

    关于drupal - 当用户在 webform 中提交值并单击提交时如何创建节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14089131/

    相关文章:

    drupal - 如何修改 Drupal 6 中的图书复制模块,以便在制作副本后将用户转发到不同的起始选项卡?

    java - 有没有办法将数据 drupal 站点传递到 java web 应用程序

    drupal - 我可以使用 Drupal 开发移动网站吗?

    MySQL 性能 - LEFT JOIN/HAVING 与子查询

    Drupal 分类 block 、词汇表

    drupal - 如何仅在某些页面 View 显示上显示 block

    PHP:获取自己的脚本刚刚通过关闭功能发送的http状态代码

    drupal - 如何从页面节点 Drupal 隐藏标题

    php - "paragraph"实体类型不存在。在 Drupal\Core\Entity\EntityTypeManager->getDefinition()

    drupal - 自定义 Drupal 模块不创建任何表