my_module.module
use Drupal\Core\Form\FormStateInterface;
function my_module_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Add default values for vapn module so the editor don't need to check this.
if (isset($form['vapn']) && $form_state->getFormObject()->getEntity()->isNew()) {
$form['vapn']['content']['widget']['#default_value'] = ['anonymous', 'authenticated'];
}
}
my_module.deploy.php
use Drupal\node\Entity\Node;
function my_module_deploy_vapn_install() {
$entity_type_manager = \Drupal::service('entity_type.manager');
$role_storage = $entity_type_manager->getStorage('user_role');
$node_storage = $entity_type_manager->getStorage('node');
$nodes = $node_storage->loadByProperties([
'type' => 'my_bundle'
]);
$roles = array_values($role_storage->loadMultiple(['anonymous', 'authenticated']));
foreach ($nodes as $node) {
// First check if already some roles are set. Only needed if you are upgrading vapn.
if ($node->vapn->isEmpty()) {
$node->setSyncing(TRUE)
->set('vapn', $roles)
->save();
}
}
}
22.01.2026 | Michael Ebert