3

My goal is to make the "revision log message" required if "Create new revision" is checked on create/edit node.

I do able to add the visible and required mark based on the "create new revision" field.

function hook_form_node_form_alter(&$form, &$form_state, $form_id) {

  $form['revision_information']['log']['#states'] = [
    'required' => [
      ':input[name="revision"]' => ['checked' => TRUE],
    ],
    'visible'  => [
      ':input[name="revision"]' => ['checked' => TRUE],
    ],
  ];
}

I also able to make the "revision log message" as required.

function hook_form_node_form_alter(&$form, &$form_state, $form_id) {

  $form['revision_information']['log']['#required'] = [
    'required' => [
      ':input[name="revision"]' => ['checked' => TRUE],
    ],
  ];
}

But I don't know how to make the field required based on the "create new revision" checkbox.

PS: I want to make this simple and don't want to use the "Enforce revision log message" module.

// update 1: I found it require a custom hook validate and Im searching for it

3 Answers 3

2

I found an answer after hours of searching. Basically, I just need to add a custom hook validation for revision log. See below for my code.

function MYMODULENAME_form_node_form_alter(&$form, &$form_state, $form_id) {
    // change revision log status based on revision checkbox
    $form['revision_information']['log']['#states'] = array(
        'required' => array(
          ':input[name="revision"]' => array('checked' => TRUE),
        ),
        'visible' => array(
          ':input[name="revision"]' => array('checked' => TRUE),
        ),
    );
}
function MYMODULENAME_node_validate($node, $form, &$form_state) {
    // add revision log validation
    if (empty($form_state['values']['log']) && ($form_state['values']['revision'])) {
        form_set_error('log', 'Revision log message cannot be empty!');
    }
}

I hope this helps someone who needs revision log message required.

2
  • This is the great job, could you please tell me how this script paste into running drupal project? Create module or where should I place this src?
    – Roman
    Mar 12, 2021 at 17:41
  • @Roman This is a hook function on a custom module. This code should be in the "module-name.module" file.
    – CocoSkin
    Mar 15, 2021 at 15:43
0

In Drupal 8 and 9 i just do this sort of thing.

$form['revision_log']['#required'] = TRUE;
$form['revision_log']['widget']['#required'] = TRUE;
$form['revision_log']['widget'][0]['#required'] = TRUE;
$form['revision_log']['widget'][0]['value']['#required'] = TRUE;
-1

I created a simple module that does this. Note, it will require the log message on any revision of a node, but not on initial creation. Also, it currently affects all node types without regard to if the create new revision is clicked on the content type configuration.

https://www.drupal.org/project/require_revision_log_message/

Your Answer

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

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