Hello,

I'm defining a new node type in a custom module. I'd like the node title be hidden on it's node add form and then populated based on values from the node type itself when it is saved. Is hook_node_presave the best place to do this? Also, is changing the access of the title field using hook_form_alter the best way, as this feels a little hackish to me, but I'm not sure of a better alternative.

Thanks!

function applicator_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == 'applicator_node_form') {
        $form['title']['#access'] = 0;
        $form['title']['#required'] = FALSE;
    }
}

function applicator_node_presave($node) {
    if ($node->type == 'applicator') {

        $fname = field_get_items('node', $node, 'applicator_module_first_name');
        $fname = $fname[0]['value'];

        $surname = field_get_items('node', $node, 'applicator_module_last_name');
        $surname = $surname[0]['value'];

        $title = $fname . ' ' . $surname;
        $node->title = $title;
    }
}

Comments

richard.french’s picture

Category: Feature request » Support request
cameron tod’s picture

Status: Active » Needs review

There's a contrib module which seems to solve a similar use case: https://drupal.org/project/auto_nodetitle

I used it on D6 many times with much success.

cameron tod’s picture

Title: hook_node_presave question » Hiding titles during node creation, then populating on save
richard.french’s picture

Thanks. I've looked at the module, but it seems using this module you can only configure the node title through the node UI and ideally I'd rather have this set through code. I've looked and there don't seem to be any obvious functions I can use to do this. @cam8001 when you used this for D6 did you have it working through a module?

Thanks

Version: 7.24 » 7.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.