Description
Published status can normally only be set by users with the "administer content" permission, which should not be given to normal users.
As of time of writing there is no proper solution to this.
Steps
-
create a new boolean field that will be used to set the status
Consider making it
- boolean
- with one single value
-
implement hook_ENTITY_TYPE_presave(EntityInterface $entity)
Check in some way that the received request comes from where you need it to. Consider using get queries, the current path or the current user. Do not call save() here.
/** * Implements hook_ENTITY_TYPE_presave() */ function MODULE_NAME_node_presave(EntityInterface $entity) { if ( Drupal::request()->get('_format') == 'xml' && $entity instanceof Node && $entity->bundle() == 'your_bundle' ) { if ($entity->FIELD_FROM_STEP_1->value) { $entity->setPublished(); } else { $entity->setUnpublished(); } } return; }