[drupal-devel] [feature] Enable customizing of "Title" field on
node forms
fago
drupal-devel at drupal.org
Fri Sep 16 13:22:28 UTC 2005
Issue status update for
http://drupal.org/node/10056
Post a follow up:
http://drupal.org/project/comments/add/10056
Project: Drupal
Version: cvs
Component: node system
Category: feature requests
Priority: normal
Assigned to: Anonymous
Reported by: Anonymous
Updated by: fago
Status: patch (ready to be committed)
Attachment: http://drupal.org/files/issues/nodetitle-updated.patch (7.05 KB)
i've updated node_validate_title() as thox suggested and added a title
"question" to the poll.module, as there was no title field for it.
fago
Previous comments:
------------------------------------------------------------------------
Sat, 14 Aug 2004 18:17:11 +0000 : Anonymous
Attachment: http://drupal.org/files/issues/node.set-title-field.patch (2.02 KB)
The default "title" field in a node object is useful but not always
appropriate. For some node types it would be preferable to rename the
field for display to users (e.g., from "title" to "name" for a record
of an organization or business), while in others the title might be
best drawn from a combination of other fields (e.g., first_name and
last_name fields,
for a record of an individual).
This patch enables two new alternatives for node titles:
renamingThe title field can be renamed, e.g., from "title" to "name".
substutingOther fields can be substituted for the title field.
The functionality is implemented through a new proposed node hook,
_title_field($op). There are two possible values for $op: "action"
(either "rename" or "substitute") and "names" (a list of field names to
be used in the title field).
Sample uses:
[?PHP
function example_title_field($node, $op) {
switch ($op) {
case 'action':
$return = 'rename';
break;
case 'names':
$return = array('Name');
}
return $return;
}
?]
In this case, since the requested action is "rename", the title field
would be labelled "Name" (instead of "Title") in node add and edit
forms for nodes of type "example".
[?PHP
function example_title_field($node, $op) {
switch ($op) {
case 'action':
$return = 'substitute';
break;
case 'names':
$return = array('first_name', 'last_name');
}
return $return;
}
?]
In this case, no "Title" field would be displayed in node add or node
edit forms of node type "example". Instead, the fields "first_name"
and "last_name" would be concatenated to form a title.
Implementing this patch would significantly increase the flexibility of
the node system, making it feasible to store information of many types
not currently supported because they don't have a "title" attribute.
------------------------------------------------------------------------
Sun, 15 Aug 2004 16:39:37 +0000 : Dries
This is best accomplished through the existing nodeapi hook, using a
separate module.
------------------------------------------------------------------------
Sun, 15 Aug 2004 23:02:13 +0000 : nedjo
Thanks for the suggestion. I did look for a way to accomplish this
through _nodeapi (and other existing hooks), but couldn't see how. The
"title" field is hard-coded into node.module. The _nodeapi hook allows
various types of manipulations, but not, so far as I could see, changes
to elements that have been generated by, e.g., form_textfield() calls.
Are there possibilities I'm missing? Other approaches? Any specific
suggestions as to means we could use to change the hard-coded "title"
field?
------------------------------------------------------------------------
Thu, 19 Aug 2004 18:27:15 +0000 : nedjo
Attachment: http://drupal.org/files/issues/node.set-title-field_0.patch (741 bytes)
Drawing on Dries's suggestion, I've substantially revised this patch to
enable node title field customization with only three additional lines
of code in node.module.
In place of the existing line in function node_form()
<?php
$output .= form_textfield(t('Title'), 'title', $edit->title, 60, 128, NULL, NULL, TRUE);
?>
the patch substitutes a the following:
<?php
$title = variable_get('node_titlefield_'. $edit->type, 'Title');
if(!($title == 'none')) {
$output .= form_textfield(t($title), 'title', $edit->title, 60, 128, NULL, NULL, TRUE);
}
?>
With this change in place, node modules will be able to override the
standard node title label by setting a variable,
node_titlefield_nodetype, where nodetype is the type of node. This
could be done, e.g., in a _settings hook, so that the first time
settings were accessed the module would set the variable:
<?php
if (variable_get('node_titlefield_example', '') == '') {
// since none is already set, set this variable
variable_set('node_titlefield_example', 'Name');
drupal_set_message('Example module initialization completed.');
}
?>
The special value 'none' would suppress the title field altogether in
node forms, used as follows:
<?php
if (variable_get('node_titlefield_example', '') == '') {
// since none is already set, set this variable
variable_set('node_titlefield_example', 'none');
drupal_set_message('Example module initialization completed.');
}
?>
In this case, a _validate hook would be used to set the title field
value. Assuming a module where a first name-last name combination was
to be used for a title:
<?php
function example_validate(&$node) {
if(!(isset($node->title))) {
$node->title = $node->first_name . ' ' . node->last_name;
}
}
?>
In short, this three-line addition to the node.module would
significantly increase the flexibility of the node system by removing
the hard-coded 'Title' field in node add and edit forms and instead
enabling custom labelling or concatenation of the field from other
sources.
------------------------------------------------------------------------
Thu, 19 Aug 2004 18:50:42 +0000 : moshe weitzman
I like this functionality. But the implementation still seems unclean. I
would prefer to simply remove the 'title' form field from node_form()
and require that modules present this field in whatever way they
please. in some cases, they will use a hidden form field because no
customer interaction is required. This causes a break with backward
compatibility, but cleanliness is facored over compatibilityness (when
you must choose).
I'm moving this back to 'Active', since I don't think this patch is
committable as is.
------------------------------------------------------------------------
Fri, 27 Aug 2004 23:46:45 +0000 : nedjo
Attachment: http://drupal.org/files/issues/node.set-title-field_1.patch (830 bytes)
Thanks for looking at this. The suggestion - dropping the title field
altogether and leaving this for module authors - has the advantage of
simplicity. However, this change would imply significant new work as it
would would require all or nearly all existing modules - core and
contributed - be modified. While I think enabling title field
customization is important for Drupal as a whole, and while this is a
change I need for two modules I'm working on, I'm not immediately
convinced that the benefits of this change justify this cost. Also, in
most cases, the present title field works fine, so keeping it as a
default option would I feel be desirable.
Hence the following patch. In place of my (poorly conceived) initial
hook proposals, above, this patch introduces a single very
straightforward new node hook, _title_field. By setting this hook to
return FALSE, node modules authors will suppress the default title
field, and be free therefore to substitute what they wish or need.
Returning TRUE, or not using the hook, will result in the default title
field being used.
Sample usage:
<?php
function example_title_field() {
return FALSE;
}
?>
This is, I feel, a much better solution than the previous two I
suggested--proof, if it's true, that the Drupal issue system works!
------------------------------------------------------------------------
Mon, 06 Sep 2004 15:50:38 +0000 : nedjo
Are there any objections to or issues raised by this small patch
(introducing a new hook to make the title field optional in node
forms)? If not, can it be applied?
------------------------------------------------------------------------
Tue, 07 Sep 2004 13:46:29 +0000 : Anonymous
I think I agree with Moshe's opinion that this should be accomplished by
having node modules display the title field. In any event, this is a new
feature and shouldn't go in during the feature freeze for 4.5.
------------------------------------------------------------------------
Tue, 07 Sep 2004 13:58:34 +0000 : ccourtne
+1 for just moving the title field to be a responsibility of the
implementing module instead of node_form. There is already to some
extent "hook overload", in addition there may be several modules which
don't want to even display a title module. Why add that only fixes
some of the problems just to have to remove it later when you make a
fix that address the whole problem.
------------------------------------------------------------------------
Tue, 07 Sep 2004 21:09:53 +0000 : nedjo
Attachment: http://drupal.org/files/issues/node.remove-title-field.patch (636 bytes)
Thanks for comments. As per feedback, the attached patch removes the
title field from node forms, leaving this instead to node modules.
Understood that possible approval of this patch this will need to wait
until code unfrozen.
------------------------------------------------------------------------
Sun, 24 Oct 2004 12:06:46 +0000 : moshe weitzman
please patch all core node modules as well.
------------------------------------------------------------------------
Sun, 28 Nov 2004 01:00:31 +0000 : nedjo
Attachment: http://drupal.org/files/issues/node-move-form-title-field.patch (4.29 KB)
Good point Moshe. This patch of node.module plus all core node modules
simply moves the title field out of node.module and into the node
modules, enabling (if desired) customization of the title field by a
module. Patches to contributed modules will in most cases be as simple
as adding the line
<?php
$output = form_textfield(t('Subject'), 'title', $node->title, 60, 128, NULL, NULL, TRUE);
?>
at the beginning of a typename_form() function (and, if necessary
changing an existing $output = to $output .=). In the attached patch,
I've customized the title field for only one of the node types, calling
the title of a forum topic the "Subject". We could consider calling the
title of a blog a "Subject" or "Topic" as well.
I feel this is a good solution (thanks for the feedback and discussion)
and, for minimal work (minor updates to contributed modules), will have
the significant benefit of enabling flexibility in content types beyond
those that have "title" attributes.
------------------------------------------------------------------------
Sun, 28 Nov 2004 17:45:35 +0000 : nedjo
Ability to customize title as provided in this patch was requested here
[1]:
"When adding/editing, the 'title' field is manditory, which is fine.
Except that its label is hardcoded to 'title'. A custom node modelling,
say, a 'Company' would much rather this field be presented to the user
as 'Company Name'.
"
[1] http://drupal.org/node/13596
------------------------------------------------------------------------
Fri, 04 Mar 2005 03:44:55 +0000 : nedjo
Here's another patch that's sat for months without being either applied
or turned down, although there was expressed support and, after several
iterations, it addressed all issues that had been raised. The point is
to remove the hard-coded "Title" field - inapprioriate for many node
types, e.g., companies - from node forms, instead allowing node modules
to treat this as appropriate (e.g., a "Subject", "Name" or "Topic", or
nothing at all).
Are there problems with this approach that were not raised? Is there a
reason this wasn't accepted?
------------------------------------------------------------------------
Fri, 04 Mar 2005 03:54:02 +0000 : Steven
nedjo: if you read the backlog you can see that this feature was delayed
for after the 4.6 release.
"I've customized the title field for only one of the node types,
calling the title of a forum topic the "Subject".
"
This is really inconsistent, as what you call the title of a blog,
story or forum node is purely subjective. "Subject" "Topic" and "Title"
are pretty much interchageable and any subtle differences will surely be
eroded by translation. The only thing I can agree on so far is naming a
polls' title "Question".
------------------------------------------------------------------------
Fri, 04 Mar 2005 05:15:03 +0000 : nedjo
Thanks for the note. I'm not sure what you mean by the "backlog".
Where would I find this? I agree that a "Question" field would make
sense for polls.
------------------------------------------------------------------------
Fri, 04 Mar 2005 14:12:56 +0000 : JonBob
Actually UnConeD, it was postponed until after the 4.5 release, not 4.6,
and looks like everyone forgot about it when the release happened. And
no, Nedjo, I don't take this as proof that Drupal needs more
bureaucracy. :-)
Marking this active again; I'm sure the patch will have to be updated
once the code branches. We had rolled this function into the needs for
CCK, but in our discussions split it off again.
------------------------------------------------------------------------
Sun, 03 Apr 2005 05:28:08 +0000 : chx
Attachment: http://drupal.org/files/issues/node-move-form-title-field_0.patch (4.59 KB)
The patch applies to ucrrent HEAD with some offset. I have rerolled it
so it applies cleanly.
------------------------------------------------------------------------
Fri, 20 May 2005 10:09:50 +0000 : chx
Patch still applies (yes, with some offset, but applies). ANd yes, we
need this very badly.
------------------------------------------------------------------------
Fri, 20 May 2005 10:18:49 +0000 : Thox
I'd benefit greatly from having a feature like this. The example already
given is one of the cases I need it: I'd like the title field to be a
concatenation of two fields: "First name" and "Last name".
------------------------------------------------------------------------
Tue, 24 May 2005 03:39:40 +0000 : gsperk
I wonder if someone could help me with the following problem.
After applying the patch I made the output for 'title' in my
own_node.module this:
$output .= form_hidden('title', $node->firstname . ' ' .
$node->surname);
When the form is first submitted I get the error 'You have to specify a
title'. When I submit again the firstname/surname variables are
obviously in $node, and the form submits with the desired title.
So, I suppose my question is, how can get the values for firstname and
surname into $node without submitting twice? (If it's not obvious I can
confirm that I dont have much experience with this kind of stuff. Any
suggestions would be a big help.)
------------------------------------------------------------------------
Wed, 25 May 2005 04:23:41 +0000 : Steven
To make this patch work you also need to move the title validation out
of node.module and into the node modules. I don't think this is that
much of a problem, it is code that is not going to be touched much.
Also, in light of the upcoming CCK it makes sense to do it like this.
------------------------------------------------------------------------
Sat, 02 Jul 2005 17:02:26 +0000 : hanoii
Nice thread.
I have come up with a slightly different approach I think that would
need less patching/updating of other modules.
Drupal 4.6.2
node.module:1275
<?php
// Get the node-specific bits.
// We can't use node_invoke() because $param must be passed by reference.
$function = node_get_module_name($edit) .'_form';
$param = array();
if (function_exists($function)) {
$form .= $function($edit, $param, $title);
}
?>
Notice the $title param on the hook_form invoke. This was what I added.
As that hook is called like this an not with the node_invoke() I can
define on my custom node module, or patch any module I need to modify
the title description with the following hook:
<?php
function budget_form(&$node, &$params, &$title) {
?>
and modify the $title argument there, as it is by reference, It will me
modified on node.module.
Then again on
node.module:1319
I added/changed the following:
<?php
if ( ! $title ) {
$title = t('Title') ;
}
$output .= form_textfield($title, 'title', $edit->title, 60, 128, NULL, NULL, TRUE);
?>
So, if the module modify the title var, it uses that, if not, it uses
the default one.
Worked for me so far, nice to post on drupal.
Greetings,
a.=
------------------------------------------------------------------------
Sat, 02 Jul 2005 18:10:28 +0000 : hanoii
I posted too soon the thing before... :)
I did further patches because of the validation keep telling that I
have tu put a "title".
I was happy with the previous patch, not so much with this one, but I
let you know what I did...
on the node.module, node_validate() function I move the title
validation block just after the
<?php
// Do node-type-specific validation checks.
node_invoke($node, 'validate');
node_invoke_nodeapi($node, 'validate');
?>
and added another parameter to the form_set_error()
<?php
// Validate the title field.
if (isset($node->title)) {
if (trim($node->title) == '') {
form_set_error('title', t('You have to specify a title.'), TRUE);
}
}
?>
then, on the form_set_error function in common.inc I did the following:
<?php
/**
* File an error against the form element with the specified name.
*/
function form_set_error($name, $message, $checkexistance = FALSE ) {
if ( !$checkexistance || $checkexistance && !isset($GLOBALS['form'][$name]) ) {
$GLOBALS['form'][$name] = $message;
drupal_set_message($message, 'error');
}
}
?>
So now, if any previous title error was added, and the $checkexistance
flag is TRUE, no error will be added to the message queue.
The moving place of the validation block was to let the modules
validate first than the node module.
Errrr.. I don't like it, but again, it worked :)
a.=
------------------------------------------------------------------------
Mon, 01 Aug 2005 00:54:22 +0000 : killes at www.drop.org
Attachment: http://drupal.org/files/issues/title_0.patch (4.56 KB)
I've updated the patch to apply to current cvs. As progress on the CCK
has been rather slow, I'd appreciate if somebody could address Steven's
concerns and the patch could be applied.
------------------------------------------------------------------------
Tue, 16 Aug 2005 19:19:23 +0000 : Thox
Attached is a patch with an attempt to move the title validation code
into the node modules (from node.module).
------------------------------------------------------------------------
Tue, 16 Aug 2005 19:20:21 +0000 : Thox
Attachment: http://drupal.org/files/issues/title-validation.patch (6.22 KB)
attached again (issue preview seems busted?)
------------------------------------------------------------------------
Wed, 24 Aug 2005 18:57:26 +0000 : nedjo
Thanks chx, killes, and Thox for updates and fixes on the patch.
I've applied and tested it and Thox's changes seem to address the title
validation issue.
It's a couple of releases later--it would be great to see this fix
applied!
------------------------------------------------------------------------
Wed, 24 Aug 2005 23:17:25 +0000 : mathias
Attachment: http://drupal.org/files/issues/title-validation_0.patch (6.04 KB)
+1
I've needed to change the name of the title field or even altogether
hide it for custom modules.
Issue preview was working for me.
We should document for module developers the implications title-less
nodes (e.g., less informative watchdog entries, harder to edit nodes,
node title lists become buggy, etc).
I couldn't get the patch to apply cleanly to HEAD so here's a new one.
------------------------------------------------------------------------
Mon, 12 Sep 2005 15:42:30 +0000 : Robrecht Jacques
Attachment: http://drupal.org/files/issues/title-validation_1.patch (6.23 KB)
+1 I need this too: I want to be able to set the title of a node
automatically.
Patch still applies with offset (rerolled patch attached).
------------------------------------------------------------------------
Tue, 13 Sep 2005 13:59:39 +0000 : fago
+1 for this functionality
i've tested the patch and it looks nice, however node_validate_title()
produces an validation error, where the field is called title, which
isn't suitable if the modules changes the name, as the forum.module
does.
------------------------------------------------------------------------
Tue, 13 Sep 2005 15:50:15 +0000 : Thox
Perhaps node_validate_title() should be passed the name of the field?
'Title' by default.
More information about the drupal-devel
mailing list