1

We recently updated our site to Drupal 8.4 and the widget for publishing a node has changed from a dropdown to a checkbox. Now, how do I change the label and help text of the checkbox? It cannot be changed in the field UI.

publishing status checkbox

3

You can use hook_form_alter() to change label and help text Use devel module to debug the label and help text change accordingly.

/**
 * Implements hook_form_alter().
 */

  function hook_form_alter(array &$form, FormStateInterface $form_state, $form_id) { 
  if($form_id == 'node_page_form'){
   kint($form);
   kint($form['status']['widget']['value']['#title']);
   $form['status']['widget']['value']['#title'] = t('Published status label changed');
  }
}

After applying above code for basic page node form i was able to see the change in label of published text to Published status label changed

enter image description here

2

You can try use the hook_form_alter,

function hook_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  //obtain the default value
  $value_status_default= $form['status']['widget']['value']['#default_value'];      

  //we modified the title of the field
  $form['status']['widget']['value']['#title']='Publishing Status';
  //we modified the description of the field
  $form['status']['widget']['value']['#description']='New Description Publishing status'; 
  //we modified the type of the field 
  $form['status']['widget']['value']['#type']='select';
  //we modified the options of the field 
  $form['status']['widget']['value']['#options'] = [
    true => t('Published'),
    false =>t('Not Published'),
  ];
  //we modified the default valueof the field       
  $form['status']['widget']['value']['#default_value'][]=$value_status_default;

}

the result enter image description here

  • Nice. I think $form['status']['widget']['value']['#type']='radios'; (a pair of radio buttons) is quite a good choice for this for inexperienced users. Note if you do that, a default value of false needs to be changed to zero for the 'Not Published' radio button to be checked. – William Turrell May 23 '18 at 21:55

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.