3

I'm prepopulating the title of the node/add/content_type using the URL. I would like this form to be hidden or locked so that the user can't accidentally edit it. I've been looking though the Drupal documentation and haven't found a way to hide fields at all. Any help would be appreciated.

2 Answers 2

4

I'd probably just hide it with CSS to be honest.

Either that, or I'd use Automatic Nodetitles to populate the title with tokens (path token in your case) as it has the option to hide the title field AND auto-generate it with minimal effort.

"auto_nodetitle" is a small and efficient module that allows hiding of the content title field in the form. To prevent empty content title fields one can configure it to generate the title by a given pattern.

When the token module is installed it's possible to use various node data for the auto generated title - e.g. use the text of a CCK field (since 5.x).

Another option would be to use hook_form_alter() and set #disabled to TRUE to disable user input.

1
  • 1
    This works for me: $form['title']['#attributes'] = array('value' => 'Test'); $form['title']['#disabled'] = TRUE; Commented Nov 3, 2014 at 4:24
0

You could use css to simply hide the title element in your node edit form. I also think you should be able to achive this by using the hook_node_presave(), something like this would work:

function YOURMODULE_node_presave($node) {
  // you may also wan't to check the 'status' of the node
  if($node->nid && $node->type == 'your-custom-content-type') {
     // check if title has changed
     if($node->title != 'your-prepopulated-value') {
        // notify user that the update couldn't be performed
        drupal_set_message('You may not change the title of this kind of nodes!','warning');
        // set node to null to force making no changes
        $node = null;
     }
  }
  return $node;
}
1
  • You could also just re-assign the changed $node->title to your desired node title in this hook.
    – Chapabu
    Commented May 17, 2012 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.