Wednesday, December 13, 2017 02:23 am EST

How to Auto Node Title Nodes of a Certain Content Type in Drupal 7

Bri's picture

Sometimes when you are creating a piece of content, the title field does not make sense to be filled out by the user. In these cases, its best to hide it and have it filled in automatically. Depending on your needs, the Auto Nodetitle may be able to give you everything you need.

After enabling the module, navigate back to basic editing of the content type you wish to configure, or add a new one. You will see a new option for "automatic title generation" with three options:

1. Disabled
2. Automatically generate the title and hide the title field
3. Automatically generate the title if the title field is left empty

Below is the option to define a pattern that can be fixed, or include the use of tokens. The module works great and may do exactly what you need it to do and you will be all set.

However, at times I have come across a situation where a client wanted a very distinct pattern that included fields that were not always required. For example, they may want a pattern such as "Observation - [Species Node Name] - [Preserve Node Name] - [Date of submission]." In this example, the middle two parameters are node references that are not required fields, so they may or may not be present. When you use auto_nodetitle to define a token based pattern, and the content is not present, you end up with something like "Observation - - - 12/12/2013" which isn't ideal.

That is when I began to investigate exactly how the auto_nodetitle module handles the titling so that I could define my own routines and incorporate the logic to only add in the " - [Species name]" part when it was needed. Below is what I came up with and might be useful to you if you have a similar situation.

First I put everything in a helper module. You can put it in whatever custom module you want. If your content type is derived from a feature, that would be ideal.

Define a hook_form_alter to alter the node form of the particular content type in question.

function [MY_MODULE]_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == '[CONTENT_TYPE_MACHINE_NAME]_node_form') {
    
    //un-require the title and hide it, we will auto title the content on submit
    $form['title']['#required'] = FALSE;
    $form['title']['#type'] = 'value';
    $form['#submit'][] = 'my_content_node_form_submit';
    
  }  
  
}

That should handle the removing of the title field from display, un-requiring it, and defining a submit handler for you to add your titling logic. Next you need to create the form submit handler

/**
 * Custom submit handler for observation nodes to alter the title 
 */
function [MY_MODULE]_node_form_submit(&$form, &$form_state) {

  //if you want any of the current users info in the title routine...
  $current_user = user_load($form_state['values']['uid']);
  $title = $current_user->name;
  
  //an example of getting a node reference field if its not empty
  // this only adds to the title if its not empty
  if(!empty($form_state['values']['field_observation_preserve_ref'][LANGUAGE_NONE][0]['nid'])) {
    $preserve = node_load($form_state['values']['field_observation_preserve_ref'][LANGUAGE_NONE][0]['nid']);
    $title .= ' - '.$preserve->title;
  }

  
  //add in the current timestamp
  $title .= ' - '.date('Y-m-d H:i', $form_state['values']['created']);
  
  //add the new title back to the form_state
  $form_state['values']['title'] = $title;
}

That should pretty much do it! Don't forget to change [MY_MODULE] to whatever your decide to name your module and to change [CONTENT_TYPE_MACHINE_NAME] to the name of your content types machine name.

Comments

Anonymous's picture

Can anybody write my research

Can anybody write my research paper for me? http://familyessay.org/research-papers/ - I usually use only this service, maybe you also can help me? I see here a lot of professionals!

Anonymous's picture

Thanks for the CustomWritings

Thanks for the CustomWritings review info. It was the exact issue for which I was looking for a solution and you know what before visiting your site I have a feeling that I would definitely find something at your site. So here you are always on time and always helpful for us. Thanks for being there.

Anonymous's picture

Form submit function

Maybe the problem was in the $form['#submit'] function.

I think you should have been written it as: '[MY_MODULE]_node_form_submit'

Anonymous's picture

Works great

Oh sorry, forgot to mention works great! Thanks for the info!

Anonymous's picture

Not working for me?

this code is not working for me in drupal 7

Bri's picture

Post the code you are using?

Show me the code that you are using. That code is just a sample of how you might accomplish something similar. It can't be directly copied.