Following http://drupal.org/node/61048 I realized that when adding a content-type through a module, the default values of title and body have no meaning. These values are only applied to a cck type.
_node_type_set_defaults in node.module defines few form elements as default, like has_title, has_body etc.
A programmer could think that when defining a new content-type (through hook_node_info), the node/add/<content-type> form will already include a title and body input fields, given the defaults were not overidden.
Helas, this is not the case, since those default fields are not rendered, unless it's a cck content-type. This is done through the function node_content_form.
Why won't these fields be available through node_form? This causes simple module provided forms to break.
| Comment | File | Size | Author |
|---|---|---|---|
| #1 | node.pages-inc.patch | 867 bytes | olav |
Comments
Comment #1
olav commentedAs documented in http://api.drupal.org/api/function/hook_node_info and shown in the example in http://api.drupal.org/api/function/node_example_node_info, hook_node_info() allows to return two switches title_label and has_body. Still, these fields are not included in the generated node form. Another - related - effect is that the node type is not listed on admin/content/types.
I can offer a workaround and a patch of node.module for this.
Workaround
In addition to implementing hook_node_info(), also implement a node form through hook_form(). This can either include title and body fields manually as examplified on http://drupal.org/node/132845 or - if you really only want title + body + listing on admin/content/types it can be a simple
Patch
See attached file. I think it is important to include this patch in node.pages.inc as the current behaviour confuses developers. As opposed to the workaround, this patch makes title + body fields show but still does not list your content type on on admin/content/types. Thus its status code needs work.
Comment #2
davidn commentedThanks you olav. Unfortunately your Workaround does not work with drupal 7 since hook_form is not called in drupal 7.Sorry, that was totally wrong! But you have to pay attention to this:
MODULE should NOT be the module name but rather the base attribute defined in hook_node_info(). This might often be the modules name but not in my case...
Comment #3
lateatnight commentedThank you olav, I didn't try the patch, but the workaround as described in #2 works.
When a bugfix in drupal core?
Comment #4
dcam commentedClosing old issues.
This is more of a support request. If anyone else finds this issue in the future, you should know a couple of things.
First, modules that provide a content type must implement hook_form(). Implementing hook_form() is not a "workaround" as suggested in #2; it's a requirement. If there's any problem, it's that I don't see anything in the API docs about this requirement.
Second, the hook_form() documentation explicitly says that you must return the node title field in the $form array. If you don't want to do that yourself, feel free to return node_content_form() as shown in the #2 example code.
Comment #5
KeithC commentedHi,
I have re-opened this issue as I don't agree with the comment made when this was closed.
I acknowledge that the requirement of implementing
hook_form()when a module defines a content type exists and that it is partially documented / hinted at in thehook_form()documentation (if not - more importantly - in thehook_node_info()documentation.However, I don't believe this requirement should exist and that it should instead be optional. If a content type is defined and is configured to have a title using the
has_titleproperty, then I think it should have a default title field added to the node_form as standard with the ability to override this field with your ownhook_form()implementation IF you wish to. I don't think you should be forced to add this field for every content type defined as it's unnecessary work.In fact, if you actually look at the
node_content_form()hook_form implementation innode.module, while the comment on the first line quite clearly informs you about the requirement (// It is impossible to define a content type without implementing hook_form()), the comment on the second line is a todo to remove this requirement (// @todo: remove this requirement.)!The current problem I am experiencing is that I want to implement
hook_view(). However, I can't implementhook_view()in my module unless I change thebasevalue in myhook_node_infofromnode_contentto my module name. But as soon as I do that, the content type disappears from theadmin/structure/typespage and thetitlefield disappears from the node add/edit form. So I then have to implementhook_form()just to return the return value ofnode_content_form().It doesn't sound like much, but you shouldn't have to jump through so many hoops (or hooks!) just to implement
hook_view()! And if you have more than one module that this applies to then it's even more of an issue.I propose that the title field defined in
node_content_form()should be added by default to all content types that have `has_title` = TRUE and that if you wish to remove or alter that field then you do so withhook_form().Thanks
Comment #6
dcam commentedHi @KeithC,
Whether or not your suggestion is a good idea is not the issue. Realistically, there is no way this will ever happen. It would be a major API change and could never be committed. I can't even guess at how many D7 modules - in core, in contrib, and custom-made - would suddenly have extra title fields popping up on their forms.
I'm sorry you've had to do extra work because of this requirement. I'm sure you have some reason why you feel that you need to use hook_view(), but I have to wonder why you're not using hook_node_view().
Comment #7
KeithC commentedHi @dcam,
Thanks for the reply. I tried to use hook_node view but unfortunately, it hooks in after a node's fields are processed where as hook_view is just before so hook_view is the only option I have for what I am trying to achieve (checking field data before it's processed).
It's disappointed that we are stuck with this issue however I understand the reasoning/implications.