Hey all,

I've been searching for days for an approach that actually works - its a shame that this isn't a simple option when editing each node type from the regular drupal admin side but...

...I'm looking for a snippet to throw into my template.php which will let me specify a custom title atop a node submission/editing page per content type; so instead of 'Create Tomato' I could specify say 'Throw a tomato at us' when a user is looking @ the node/add/tomato page.

I'd love to know if there's an approach to use for both drupal 5 and 6 - if they aren't the same then I would super appreciate snippets for both... I'm surprised to not have found this info anywhere in the handbook...

Thanks,

q./

Comments

adixon’s picture

as per http://drupal.org/node/68999 [hehe, toot-toot!] you can set the title in your form theme.

so in your template.php file, define (or extend), replacing as appropriate:

function phptemplate_node_form($form) {
  switch($form['type']['#value']) {
    case 'my_node_type':
      $title = empty($form['nid']['#value']) ? 'Add a new custom node' : 'Edit a new custom node';
      drupal_set_title($title);
      break;
  }
  return drupal_render($form);
}

This will allow you to redefine all node edit form titles actually. You'll have to modify slightly if you don't want to modify the title when you're editing existing nodes.

Thanks to Andre Molnar for setting me on the one and true drupal way for this solution.

ahkiam’s picture

Using this method in Drupal 6, my title is correctly changed by my submit and preview buttons get relocated to the top of my form. I'm not sure why that's happening, but any ideas...

mritz_p’s picture

odd thing... exactly the same happened to me.

adixon’s picture

If you check out the node module, you'll see that there's already a theme_node_form. So the above solution replaces that theme, which causes unexpected side-effects (including the relocating of buttons as observed in other comments).

So, to fix the above solution, replace that last

return drupal_render($form)

line with

return theme_node_form($form)

so that the usual fix ups to the form are done (see comments in the node.page.inc file to see all of what theme_node_form does).

rosetwig’s picture

I was using this solution for customizing the node create page for a forum topic, and I wanted to include the specific forum topic name (which is a taxonomy term) into the title text. After much struggle, I hit on the trick of using arg() to get the term id (in this case the path is node/add/forum/id, so it was arg(3)) and then calling taxonomy_get_term with it. Here's the result:

function phptemplate_node_form($form) {
  switch($form['type']['#value']) {
    case 'forum':
      $forum_term = taxonomy_get_term(arg(3));
      $title = empty($form['nid']['#value']) ? 'Add a new ' . $forum_term->name . ' posting': 'Edit a ' . $forum_term->name . ' posting';
      drupal_set_title($title);
      break;
  }
  return drupal_render($form);
}
seanburlington’s picture

Hi,
I've written a module to address this

http://www.practicalweb.co.uk/blog/09/05/25/module-make-title-nodeadd-pa...

Sponsored by
http://www.consultanddesign.net/

I've applied for a Drupal CVS account to upload this

Sean Burlington
www.practicalweb.co.uk

daveparrish’s picture

Any luck with the Drupal CVS account?

icstars’s picture

i'm quite sure this is not the best approach, but it got me quickly back to a working theme after moving from 5.x to 6.x.

for several content types, we have a parent-child relationship (e.g. school assignment/student submission to that assignment). for these types of content, we display the form side by side with its parent. in 5.x we used this in the template.php file:

function phptemplate_node_form($form) {
  switch ($form['#node']->type) {
    case 'self_essay':
     return _phptemplate_callback('node-add-tasksubmit', array('form' => $form));
    default:
     return theme_node_form($form);
  }
}

for 6.x we have moved to this:

function phptemplate_node_form($form)
{
  switch ($form['#node']->type) {
    case 'self_essay':
    	return theme_render_template(path_to_theme().'/node-add-tasksubmit.tpl.php', array('form' => $form));
    default:
     return theme_node_form($form);
}
}

i tried getting preprocess functions to work, and they were great for displaying nodes, but i could not figure out how to customize the node/add node/edit using that approach.

here's the code for the node-add-tasksubmit.tpl.php the hairy sql is used to figure out what node to display on the left:

<!-- start node-add-tasksubmit.tpl.php -->
	<div id='left5050'>
	<?php 
	if (arg(2) == 'edit') {
		$editnid = arg(1);
 		$query = <<<SQL
select field_gta_task_nid 
		from content_type_task_assignment_group, content_field_ems_assignment 
		where content_field_ems_assignment.nid = '$editnid' and
			content_field_ems_assignment.field_ems_assignment_nid = content_type_task_assignment_group.nid 
SQL;
	}
	else{
		$assignmentnid = arg(3);
		$query = "select field_gta_task_nid from content_type_task_assignment_group where nid = '$assignmentnid'";
	}
	$task = db_result(db_query($query));
	$node = node_load($task);
	?><h3>
	<?php print $node->title; ?>
	</h3>
	<?php
	print node_view($node, false, true, false);
	?>
	</div>
	<?php
	$submitform = "<div id='right5050'><h3>Please complete this form to submit your assignment</h3>";
	$submitform .= drupal_set_title('');
	$submitform .= drupal_render($form);
	$submitform .= "</div>";
	print $submitform;
	?>
<!-- end .tpl.php -->
Junro’s picture

Hi, I'm looking for custom node/add page titles.

1. Put the code in template.php

2. node-add-tasksubmit.tpl.php

but where i set custom titles for different content types ?

example:

content-type1 = blablabla[title]

content-type2 = [title]blablabla

Junro’s picture

Actually, i'm using this code display in an active block:

drupal_set_title("".t('Add xxx')." :");

This method works very well, but you have to create one block per node type...

chinita7’s picture

@Junro How can I hide the original title displayed by default?

gagarine’s picture

Hello,

Now, what the best approach for D6?

Thanks

https://interface-network.com - Interface Network is an action and research technology governance agency.

Junro’s picture

Hello, I try this code in my template.php file but it doesn't work

function phptemplate_node_form($form) {
  switch($form['type']['#value']) {
    case 'my_node_type':
      $title = empty($form['nid']['#value']) ? 'Add a new custom node' : 'Edit a new custom node';
      drupal_set_title($title);
      break;
  }
  return drupal_render($form);
}

I had only replace 'my_node_type'

Is this code still working in D6?

markosaurus’s picture

Not sure why, I gave this in my template.php abd it doesn't work either.

function phptemplate_node_form($form) {
      $title = 'TEST TITLE';
      drupal_set_title($title);
return theme_node_form($form)}
jackiboa’s picture

it’s good to see this information in your post, i was looking the same but there was not any proper resource.
assignment | lab report writing help

FranciscoLuz’s picture

here is a snippet of how to achieve that in you custom module

function mymodule_form_alter(&$form, $form_state, $form_id) {
    if($form_id == 'mynodetype_node_form'){
         drupal_set_title('My new title');
    }
}

Drupal in the Amazon Jungle

pubudusj’s picture

Hi,

drupal_set_title('My new title') is working fine when the form loads.

But when form submits and if there are any validation errors, the title is reverted back instead of "My new title".

Please help.

--thanks

jeffam’s picture

Here's my roundabout way of dealing with that. I added an additional validation handler that sets the page title to the same thing as in my form_alter. Not pretty, but it works:

function mymodule_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'mynodetype_node_form_node_form') {
    $form['#validate'][] = 'mymodule_validate';
    drupal_set_title(t('My New Title'));
  }
}

function mymodule_validate($form, &$form_state) {
  drupal_set_title(t('My New Title'));
}
gMaximus’s picture

Thanks for that... Couldn't work out why the title changed after my validation kicked in...

I am always interested in paid work... Contact me through Online Business Builders

bdawg8569’s picture

I think a better way to do this is with the use of an after_build callback

In your form alter you can specify this as

$form['#after_build'][] = 'node_entry_form_after_build';

Then define a callback function of the same name

/**
 * This function is ran after the node/add/[node-type] form is built. The css and js files are added here
 * because if they are added in the form alter, they won't get included when the form throws validation errors.
 *
 * @form
 * the node form at node/add/[node-type]
 */
function node_entry_form_after_build($form, &$form_state) {
  
  //add an css and JS you want to be executed even when you have errors
  drupal_add_css(drupal_get_path('module', '[module_name]') . '/css/whatever.css');
  drupal_add_js(drupal_get_path('module', '[module_name]') . '/js/whatever.js');
  

  //set a drupal title here
  drupal_set_title('my title');  
  
  
  return $form;
}
OptimusPrime23’s picture

jcmartinez’s picture

I had the need for a module like this. After checking this post and others I rolled out the module Node Add Title for Drupal 7. Hope it can help you.

Node Add Title will let you change the title of the node creation pages and the node editing pages.

icf-vpathak’s picture

The contributed Node Add Title module helped me in this type of similar case. Also there are other ways mentioned on Stack exchange which might be helpful http://stackoverflow.com/questions/1392573/changing-the-node-creation-ti....

IRuslan’s picture

I've built a simple module to control any page with a drupal form behind it — Form page title.
For now, it's experimental, but if someone will be interested, I'll promote it.

With best regards,
Ruslan Isay

Alice112’s picture

i'm quite sure this is not the best approach, but it got me quickly back to a working theme after moving from 5.x to 6.x.