0

Here is basically what I'm trying to do. If the draft field is present I would like to make the name field not required.

if($form_state['values']['field_draft']['und'][0]['value']){
  $form['field_name']['und']['#required'] = FALSE; }

Is this even possible? I tried after_build but it doesn't work. If not, is there a better way to add draft functionality through hook_form_alter.

Thanks!

2 Answers 2

2

In Drupal 7, I would use the following code.

/*
 * Implements hook_form_alter().
 */
function test_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'your_form_id':
      $lang = $form['name_field']['#language'];//get field language
      if(isset($form_state['values']['field_draft'][$lang][0]['value'])){
        $form['name_field'][$lang][0]['value']['#required'] = FALSE;
      }
      break;
  }
}

Note: Never use und try to get the language for multilingual sites. If site is not multilingual, then best to use the constant LANGUAGE_NONE and not the string 'und'.

6
  • I think "never" is a bit strong. If I'm building a website that I know will never need internationalization and I'm writing code that will only ever be used on that specific site then it's perfectly acceptable to use 'und' and save some lines of code.
    – Felix Eve
    Commented Jan 29, 2014 at 11:06
  • 2
    ofcourse if the site is multilingual sites then need to get the language. If site is not multilingual then you can use the constant LANGUAGE_NONE and not the string 'und'.
    – Inizio
    Commented Jan 29, 2014 at 12:03
  • Language none is defined as: define('LANGUAGE_NONE', 'und'). I'm happy to assume that this definition will not change so it's less typing to put 'und' than LANGUAGE_NONE.
    – Felix Eve
    Commented Jan 29, 2014 at 12:51
  • 2
    Still very much not advisable to use 'und' instead of LANGUAGE_NONE, even though it is unlikely to change. It doesn't save you many keystrokes (about half a second worth of typing) to future proof yourself. Laziness can sometimes come back to bite you on the butt.
    – rooby
    Commented May 6, 2014 at 8:19
  • Your IDE will suggest the constant name in a quater second, so LANGUAGE_NONE is for the laziest one.
    – AKS
    Commented Jul 19, 2014 at 14:20
0

please try it in hook_form_alter

$form['field_name']['#required'] = isset($form_state['values']['field_draft'][LANGUAGE_NONE][0]['value']) ? FALSE : TRUE;
2
  • 1
    Welcome to Drupal Answers! The OP is trying to set the property of a form element for a field basing on the value assigned to another field. The node title is not handled by the field API.
    – apaderno
    Commented Jul 20, 2014 at 6:58
  • Whilst this may theoretically answer the question, it would be preferable to include the essential explanation with the code. Now the answer seems bad (an answer, true, just bad one, as @kiamlaluno said), but maybe the logic behind it was decent, and could be usable?
    – Mołot
    Commented Jul 23, 2014 at 6:33

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.