[drupal-devel] [feature] Enable customizing of "Title" field on
node forms
Steven
drupal-devel at drupal.org
Fri Mar 4 03:54:05 UTC 2005
Issue status update for http://drupal.org/node/10056
Project: Drupal
Version: cvs
Component: node system
Category: feature requests
Priority: normal
Assigned to: Anonymous
Reported by: Anonymous
Updated by: Steven
Status: patch
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".
Steven
Previous comments:
------------------------------------------------------------------------
August 14, 2004 - 19:17 : 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
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
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.
------------------------------------------------------------------------
August 15, 2004 - 17:39 : Dries
This is best accomplished through the existing nodeapi hook, using a
separate module.
------------------------------------------------------------------------
August 16, 2004 - 00:02 : 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?
------------------------------------------------------------------------
August 19, 2004 - 19:27 : 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.
------------------------------------------------------------------------
August 19, 2004 - 19:50 : 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.
------------------------------------------------------------------------
August 28, 2004 - 00:46 : 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!
------------------------------------------------------------------------
September 6, 2004 - 16:50 : 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?
------------------------------------------------------------------------
September 7, 2004 - 14:46 : 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.
------------------------------------------------------------------------
September 7, 2004 - 14:58 : 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.
------------------------------------------------------------------------
September 7, 2004 - 22:09 : 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.
------------------------------------------------------------------------
October 24, 2004 - 13:06 : moshe weitzman
please patch all core node modules as well.
------------------------------------------------------------------------
November 28, 2004 - 02:00 : 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.
------------------------------------------------------------------------
November 28, 2004 - 18:45 : 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
------------------------------------------------------------------------
March 4, 2005 - 04:44 : 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?
More information about the drupal-devel
mailing list