You're trying to combine what should be two steps, into one. Validation handlers are for validation, so skipping the validation happens in the validation handler. Processing, which in this case setting is setting the node to published, happens in the submit handler.
In the validation handler:
if (!in_array('site_manager', $user->getRoles()) || $given_email !== '[email protected]') {
// Form validation for users other than the site manager goes here.
}
}
In the validation handler (assuming this is a node form):
if (in_array('site_manager', $user->getRoles()) && $given_email == '[email protected]') {
$node = $form_state->getFormObject()->getEntity();
$node->setPublished()
->save();
}
}
However, this is not how I would go about this. Hard coding an email address into this makes it difficult to change if the email address changes in the future. Instead, I would:
- Create a new role.
- Create a new permission in [MODULE].permissions.yml (don't forget to clear the registry after):
bypass my form validation:
title: 'Bypass my form validation'
description: 'Bypasses validation of My Form'
restrict access: true
- Assign the new permission to the role created in step one.
- Assign the role to the admin user who is to bypass validation.
- Change the validation code to this:
if (!\Drupal::currentUser()->hasPermission('bypass my form validation')) {
// Form validation for users other than the site manager goes here.
}
}
- Change the submit handler to this:
if (\Drupal::currentUser()->hasPermission('bypass my form validation')) {
$node = $form_state->getFormObject()->getEntity();
$node->setPublished()
->save();
}
}
This gives a more dynamic solution, in that the user can change their email address in the system, without having to update the code for the new email address. It also allows for adding additional users that can be skipped in the future if the need comes up, simply by assigning them the role created in step one.