【问题标题】:How to display node/add/sometype form on another page?如何在另一个页面上显示节点/添加/某种形式?
【发布时间】:2011-07-17 17:10:53
【问题描述】:

整个问题如下:

假设我们有项目,项目可以有投标,项目可以有问题,问题可以有答案。

当一个项目被显示时,与这个项目相关的所有内容也应该被显示。此外,根据角色的不同,应显示出价、提问和重播答案的某些表格。

如何做到这一点?我应该为每种类型设置单独的节点类型吗?或者我应该将问题和答案等一些子类型视为 cmets?我应该为此使用一些知名模块吗?

我正在使用 Drupal 7,我尝试编写一个自定义模块,但我没有让它正常工作。

【问题讨论】:

    标签: drupal drupal-7


    【解决方案1】:

    这就是我解决问题的方法:

    在我的 hook_menu 中

    $items['add-visiteur'] = array(
      'title' => 'Add',
      'page callback' => 'add_visiteur',
      'access arguments' => array('access content'),
      'type' => MENU_CALLBACK,
    );
    

    还有我的回调函数

    function add_visiteur() {
      module_load_include('inc', 'node', 'node.pages'); 
      // which nodeform you want
      $node_type = 'visiteur';
      $form_id = $node_type . '_node_form';
      // maybe add current users info
      global $user;
      // create a blank node
      $node = new stdClass;
      $node->uid = $user->uid;
      $node->name = (isset($user->name) ? $user->name : '');
      $node->type = $node_type;
    
      // Invoke hook_nodapi and hook_node
      node_object_prepare($node);
    
      $output = drupal_get_form($form_id, $node);
      return $output;
    }
    

    【讨论】:

    【解决方案2】:

    我正在复制适用于我的案例的解决方案。它可以作为 Drupal.org 上的答案,它可以帮助其他人解决我遇到的同样问题。

    答案在这里:https://www.drupal.org/node/1336212#comment-6192404

    我在下面复制粘贴:

    在您的自定义回调或 hook_form_alter 中,调用...

    <?php
    form_load_include($form_state, 'inc', 'node', 'node.pages');
    ?>
    

    ...或...

    <?php
    form_load_include($form_state, 'inc', 'user', 'user.pages');
    ?>
    

    ...取决于您正在加载的核心 Drupal 表单是节点表单还是用户表单。

    【讨论】:

      【解决方案3】:

      模块Form Block 是在页面上嵌入节点表单的最简单方法。然后我会使用带有块显示和参数的视图来显示这些相关节点的表格列表。

      虽然 Drupal 7 评论模块是建立在字段上的,但它对于非评论之类的东西确实不够灵活。如果您希望您的子类型具有标题和正文,那么 cmets 可能是要走的路。如果您只想要自定义字段,则可以使用节点,并且可能使用 Automatic Nodetitles 之类的东西。

      2014 年更新:如果任何人都喜欢添加不带编码的块,您可能希望查看 Advanced Form Block 模块,它为您的标准块添加了一些功能(您可以添加任意数量的块,全部制作)通过 AJAX 提交,甚至选择您想要的字段)。与 Form Block 模块不同,它仍然为 Drupal 7 积极维护。

      【讨论】:

      【解决方案4】:
      // Drupal 7    
      // Embed node creation form on a custom page inside module.
      module_load_include('inc', 'node', 'node.pages');
      $form = node_add('node_machine_name'); 
      return drupal_render($form);
      

      【讨论】:

      • 在 Drupal7 上完美运行!谢谢
      • 刚刚复制了我的答案:|
      【解决方案5】:

      要让 dobeerman 的示例(已接受的答案)在 d7 中工作,请添加 'language' => LANGUAGE_NONE 并将 $node 数组转换为对象。即:

      $node = (object)array(
        'uid' => $user->uid,
        'name' => (isset($user->name) ? $user->name : ''),
        'type' => $node_type,
        'language' => LANGUAGE_NONE
      );
      

      【讨论】:

      【解决方案6】:
      module_load_include('inc', 'node', 'node.pages');
      
      $form = node_add('nodetype');
      $output = drupal_render($form);
      

      如果您的节点表单有文件上传小部件,您应该将以下行添加到菜单数组:

      'file path' => drupal_get_path('module', 'node'),
      'file' => 'node.pages.inc',
      

      【讨论】:

      • 你能添加一个关于如何“将以下行添加到菜单数组”的示例吗?
      • function yourmodule_menu_alter(&$items) { $node_path = drupal_get_path('module', 'node'); $items['system/ajax']['文件路径'] = $node_path; $items['system/ajax']['file'] = 'node.pages.inc'; }
      • 谢谢你的回答对我帮助很大 :D about file upload widget
      • @MariusIlie 我是drupal 的新手,所以我也输入了这段代码。我已经在这个钩子的 hook_node_view 中尝试过我已经添加了 $output 像这样 $node->content['article'] = $output;但什么也没发生。那么渲染后如何将其附加到节点视图?
      • 最好在你的代码中加入这一行 if (!function_exists('node_add')) { module_load_include('inc', 'node', 'node.pages'); }
      【解决方案7】:

      Thomas 的回答对我来说看起来不错:Formblocks 和自动节点标题。我认为您可以使用 Nodereference URL Widget 对此进行扩展——使用节点引用而不是 cmets,并让该模块完成保持子节点与其父节点连接的工作。

      【讨论】:

        【解决方案8】:

        在 Drupal 7 中,需要将空白节点创建为对象(而不是数组)。

          $node->uid = $user->uid;
          $node->name = (isset($user->name) ? $user->name : '');
          $node->type = $node_type;
          $node->language = '';
        

        【讨论】:

          【解决方案9】:

          要获取节点编辑表单,您需要包含node.pages.inc

          <?php
            // required for Drupal 6
            module_load_include('inc', 'node', 'node.pages'); 
            // which nodeform you want
            $node_type = 'YOURNODETYPE';
            $form_id = $node_type . '_node_form';
            // maybe add current users info
            global $user;
            // create a blank node
            $node = array(
              'uid' => $user->uid,
              'name' => (isset($user->name) ? $user->name : ''),
              'type' => $node_type,
            );
            // Invoke hook_nodapi and hook_node
            node_object_prepare($node);
            // Or you can also use an exiting node, for example
            // $node = node_load(123);
            // and the display the form:
            $output = drupal_get_form($form_id, $node);
          ?>
          

          【讨论】:

          • “创建空白节点”部分不适用于 drupal 7,但我找到了解决方案。谢谢
          • 上面的代码对于 d7 是错误的。正如下面@user462645 所说, $node 变量必须是一个对象 module_load_include('inc', 'node', 'node.pages'); $node_type = '你的节点类型'; $form_id = $node_type 。 '_node_form';全局$用户; $node = new stdClass(); $node->uid = $user->uid; $node->name = (isset($user->name) ? $user->name : ''); $node->type = $node_type; $节点->语言='';节点对象准备($节点);返回 drupal_get_form($form_id, $node);
          • @**user506259** 你的解决方案是什么?
          • 此方法在 drupal 7 中返回错误,但 Marius Ilie 解决方案工作正常。
          • 要让 dobeerman 的示例在 d7 中工作,添加 'language' => LANGUAGE_NONE 并将 $node 数组转换为对象。即:
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-04-22
          • 1970-01-01
          • 2011-09-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多