3

I'm trying to create a simple custom module to stop the Title of a specific node being edited.

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'nodetitle_node_form') {
    unset($form['title']);
  }
}

Currently it just removes any reference to the Title from the edit pages. Does anyone know how I can set the Title field attribute read only?

3

You can use the #disabled attribute:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'nodetitle_node_form' && $form['nid']['#value']) {
    $form['title']['#disabled'] = TRUE;
  }
}

I made the assumption that you want this when editing nodes only (not when they are being added) which is what the && $form['nid']['#value'] part is for.

  • The problem with #disabled is that it prevents submission of the disabled field as the drupal form api says... – DrCord May 4 '15 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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