简体   繁体   English

Drupal自定义节点表单

[英]Drupal Custom Node Form

I want to create an input form for registered user, separated from admin's content creation form. 我想为注册用户创建一个输入表单,与管理员的内容创建表单分开。 Each submission will create multiple node. 每次提交都将创建多个节点。 To illustrate the case, I will use content type Project , and Review. 为了说明这种情况,我将使用内容类型ProjectReview。

Project : Title , Description , Owner , Rating . 项目标题描述所有者评级

Review : Title , Reviewer , Project , Difficulty 评论标题评论者项目难度

I have setup the content types along with their fields using CCK modules. 我使用CCK模块设置了内容类型及其字段。 I want to create an input form to be displayed for registered members (non-admin). 我想为注册成员创建一个输入表单(非管理员)。 Its field includes Project Name , Description , Owner , Rating , Review , Difficulty . 其字段包括项目名称描述所有者评级评论难度

When the form is submitted, Project Name , Description , Owner , and Rating value goes into new Project node, where the rest goes into new Review node. 提交表单后,“ 项目名称” ,“ 描述” ,“ 所有者 ”和“ 评级”值将进入新的“ 项目”节点,其余部分将进入新的“ 审阅”节点。

Another customization that I would like to do is for Rating and Difficulty input field to use star rating input . 我想做的另一个定制是评级难度输入字段使用星级评分输入

What would be the best way to accomplish this? 实现这一目标的最佳方法是什么? Should I create custom module and custom form (Can anyone point me how to do this)? 我应该创建自定义模块和自定义表单(任何人都可以指出我如何做到这一点)? Or is there any modules I could use? 或者我可以使用任何模块吗?

Thanks 谢谢

There are several ways to do this: 做这件事有很多种方法:

  1. Do everything from scratch: This is what theunravelers suggestion (+1) boils down to - build the form yourself, add your own validation and submit handlers and on submit, build two node objects and save them. 从头开始做一切:这就是theraravelers建议 (+1)归结为 - 自己构建表单,添加自己的验证和提交处理程序,然后提交,构建两个节点对象并保存它们。 You'll have full control/flexibility, but it is quite some work and you need to have a good understanding of Drupals inner workings to get it right. 你将拥有完全的控制/灵活性,但这是相当多的工作,你需要很好地理解Drupals内部工作才能使它正确。

  2. 'Overload' one of your content types with the fields needed by the other and tweak the 'overloaded' content types submit (and partially edit/display) logic to create the other content type from the additional fields, while hiding them on the 'overloaded' one on display and edit. 使用另一个所需的字段“重载”您的一个内容类型并调整“重载”内容类型提交(和部分编辑/显示)逻辑以从其他字段创建其他内容类型,同时将其隐藏在“重载”上'一个展出和编辑。 You can find an article describing this approach here . 您可以在此处找到描述此方法的文章 This is a considerably easier approach than #1, but I'd consider it to be a bit 'hackish', because of the content type definition vs. display mismatch. 这是比#1更容易的方法,但我认为它有点'hackish',因为内容类型定义与显示不匹配。

  3. A less 'hackish' variation of #2 would be to set up your content types normally and just manipulate the edit and submit process via hook_form_alter() . #2的较少“hackish”变体是正常设置内容类型,只需通过hook_form_alter()操作编辑和提交过程。 You'd do more or less the same as with approach #2, but instead of 'overloading' one node with the additional fields upfront, you'd just inject them into the edit form directly on hook_form_alter (either from scratch or by loading the edit form of the other node in the background and copying the relevant field definitions from that). 您可以使用与方法#2相同的方法,但不是“预先加载”一个节点和其他字段,而是直接在hook_form_alter它们插入到编辑表单中(从头开始或通过加载在后台编辑另一个节点的表单并从中复制相关的字段定义。 On form submission, you remove those additional fields while using them to build your other node. 在表单提交时,您在使用它们构建其他节点时删除这些附加字段。 This requires a bit more work and knowledge than #2, but should be a bit more stable and easier to adjust/maintain than that, as you do not have to deal with a content type definition vs. display mismatch. 这需要比#2更多的工作和知识,但应该比这更稳定,更容易调整/维护,因为您不必处理内容类型定义与显示不匹配。

Also, you did not specify how you want to deal with editing of existing nodes - I'd suggest adding a nodereference to one of the nodes to keep track of their association. 此外,您没有指定如何处理现有节点的编辑 - 我建议将节点引用添加到其中一个节点以跟踪它们的关联。 That way, you could also add the logic to edit both nodes from one form, as well as synchronized deletion, if desired. 这样,如果需要,您还可以添加逻辑以从一个表单编辑两个节点,以及同步删除。

That sounds like a custom job to me. 这对我来说听起来像是一份定制工作。 You can use the Form API in your module to make form. 您可以使用模块中的Form API来创建表单。 Look at pretty much any other module to see examples of how Form API works. 查看几乎任何其他模块,以查看Form API如何工作的示例。 Then, you'll want to create a $node object from all of the values of those fields and use node_submit() and node_save() to actually create the different nodes. 然后,您将要从这些字段的所有值创建$ node对象,并使用node_submit()node_save()来实际创建不同的节点。

I'd suggest maybe looking at the Webform module for using the Form API and hijacking the submit process to make it create those two node types. 我建议可能会查看Webform模块以使用Form API并劫持提交过程以使其创建这两种节点类型。

Option #3, using one of the youngish solutions in creating a CCK nodereference field that can point to a non-existing node, and create it on submit. 选项#3,使用其中一个年轻的解决方案创建可以指向不存在的节点的CCK节点参考字段,并在提交时创建它。

Node Reference Create looks like one of the more stable of these projects. 节点参考Create看起来像这些项目中更稳定的一个。

Node Reference Auto-create and Node Reference Field appear to have more value-added for determining values in the new node. 节点参考自动创建节点参考字段似乎具有更多的附加值,用于确定新节点中的值。

This has a secondary advantage in building a nodereference between the modules, which you can use to integrate the nodes when rendered, create Views, and more. 这在构建模块之间的节点引用方面具有次要优势,您可以使用它们在呈现时集成节点,创建视图等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM