Option 1 (recommended): custom entity reference selection handler. Further options down below.
After implementing that plugin, you need to activate it on the entity form display for the field you want it.
Option 1 (recommended): custom entity reference selection handler. Further options down below.
<?php
namespace Drupal\my_module\Plugin\EntityReferenceSelection;
use Drupal\Core\Entity\Plugin\EntityReferenceSelection\UserSelction;
use Drupal\Core\StringTranslation\TranslatableMarkup;
#[EntityReferenceSelection(
id: "default:active_user",
label: new TranslatableMarkup("User selection (only active users)"),
entity_types: ["user"],
group: "default",
weight: 1
)]
class ActiveUserSelection extends UserSelection {
/**
* {@inheritdoc}
*/
protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
$query = parent::buildEntityQuery($match, $match_operator);
$query->condition('status', 1);
return $query;
}
}After implementing that plugin, you need to activate it on the entity form display for the field you want it.
/**
* Reassign content from specific users to anonymous and delete them.
*/
function my_module_deploy_reassign_and_delete_users(&$sandbox) {
$uids = [111, 123, 222, 234, 333, 345];
$user_storage = \Drupal::entityTypeManager()->getStorage('user');
$users = $user_storage->loadMultiple($uids);
foreach ($uids as $uid) {
if (!isset($users[$uid])) {
continue;
}
// Handle all entities (including nodes, comments, etc.).
$entity_type_manager = \Drupal::entityTypeManager();
foreach ($entity_type_manager->getDefinitions() as $entity_type_id => $definition) {
if (!$definition->entityClassImplements(ContentEntityInterface::class)) {
continue;
}
$storage = $entity_type_manager->getStorage($entity_type_id);
$uid_key = $definition->getKey('uid') ?: ($definition->getKey('owner') ?: NULL);
if ($uid_key) {
$ids = $storage->getQuery()
->condition($uid_key, $uid)
->accessCheck(FALSE)
->execute();
if (!empty($ids)) {
foreach ($storage->loadMultiple($ids) as $entity) {
if ($entity instanceof EntityOwnerInterface) {
$entity->setOwnerId(0);
} else {
$entity->set($uid_key, 0);
}
try {
$entity->save();
} catch (\Exception $e) {
\Drupal::logger('sn_base')->error('Failed to reassign entity @type @id: @message', [
'@type' => $entity_type_id,
'@id' => $entity->id(),
'@message' => $e->getMessage(),
]);
}
}
}
}
}
// Finally delete the user.
$users[$uid]->delete();
}
}<?php
use Drupal\user\RoleInterface;
/**
* Remove the old roles and replace them with the new ones.
*/
function MY_MODULE_update_9041(): void {
$em = \Drupal::entityTypeManager();
$user_storage = $em->getStorage('user');
// Ensure the new role exists. If not, create it first.
$role_storage = $em->getStorage('user_role');
if ($role_storage->load('new_role') === NULL) {
/** @var \Drupal\user\RoleInterface $role */
$role = $role_storage->create([
'id' => 'new_role',
'label' => 'New role',
]);
$role->save();
}
$uids = \Drupal::entityQuery('user')
->accessCheck(FALSE)
->execute();
if (empty($uids)) {
return;
}
/** @var \Drupal\user\UserInterface[] $users */
$users = $user_storage->loadMultiple($uids);
foreach ($users as $user) {
if ($user->hasRole('old_role')) {
$user->addRole('new_role');
$user->removeRole('old_role');
}
$user->save();
}
}drush uli
https://my-project.dev.erd.fish/user/reset/1/1743658959/GxIzcvUB-Hv7E9WPoxyAIF-_pXD4kTH3GCIXQ2QFZz0/login
copy:
/user/reset/1/1743658959/GxIzcvUB-Hv7E9WPoxyAIF-_pXD4kTH3GCIXQ2QFZz0/login
look up your site domains in the .ddev/.ddev-docker-compose-base.yaml:
external_links:
- "ddev-router:cms.ddev.site"
Add everything after '/' from the drush uli command.
cms.ddev.site/user/reset/1/1743659359/GxIzcvUB-Hv7E9WPoxyAIF-_pXD4kTH3GCIXQ2QFZz0/logindrush user:information --uid=user_id
```
--------- ----------- ------------------- --------------- -------------
User ID User name User mail User roles User status
--------- ----------- ------------------- --------------- -------------
1 admin admin@example.com authenticated 1
--------- ----------- ------------------- --------------- -------------
```<?php
namespace Drupal\scp_base\Routing;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
class RouteSubscriber extends RouteSubscriberBase {
/**
* {@inheritdoc}
*/
protected function alterRoutes(RouteCollection $collection) {
// Make sure profile pages are not admin pages, in order to load the right
// translation.
if ($route = $collection->get('profile.user_page.single')) {
$route->setOption('_admin_route', FALSE);
}
}
}