-2

I find that my instance of drupal is always "creating" new anonymous users.

I have an even on Create New User that simply logs the name to a file, and it is filled with entries like

2023-10-20T12:07:48-04:00,Anonymous <>,Drupal\nyc_custom_events\Event\UserCreateEvent,user created.
2023-10-20T12:07:49-04:00,Anonymous <>,Drupal\nyc_custom_events\Event\UserCreateEvent,user created.
2023-10-20T12:07:50-04:00,Anonymous <>,Drupal\nyc_custom_events\Event\UserCreateEvent,user created.
2023-10-20T12:07:50-04:00,Anonymous <>,Drupal\nyc_custom_events\Event\UserCreateEvent,user created.
2023-10-20T12:07:51-04:00,Anonymous <>,Drupal\nyc_custom_events\Event\UserCreateEvent,user created.

Thousands, as you can see one or two a second. The site is not even that heavily trafficked, though maybe it is robot traffic.

Is that normal?

The logging Event is simply fired during the function hook_user_create.

/**
 * Implements hook_user_login().
 * @see https://drupal.stackexchange.com/a/223380
 */
function nyc_custom_events_user_create(UserInterface $user) {
  $event = new UserCreateEvent($user);
  $event_dispatcher = \Drupal::service('event_dispatcher');
  $event_dispatcher->dispatch($event, UserCreateEvent::EVENT_NAME);
}
  /**
   * On user create.
   *  - log event
   *  - rebuild router
   *
   * @param \Drupal\Component\EventDispatcher\Event $event
   *   Generic event.
   */
  public function userCreate(UserCreateEvent $event) {
    $user = $event->user->getDisplayName().' <'.$event->user->getEmail().'>';
    $this->logger->write($user, get_class($event), 'user created.');
    // rebuild router so that user endpoints are created
    \Drupal::service('router.builder')->rebuild();
  }
6
  • If you’re asking to have your code debugged, we must see the code. This question is unanswerable as written.
    – cilefen
    Oct 20, 2023 at 17:14
  • @cilefen I've added some code, but the question is simply whether Drupal is known to trigger the create user hook every time an anonymous user visits the site.
    – dmgig
    Oct 20, 2023 at 17:46
  • The hook says it implements hook_user_login but it is called nyc_custom_events_user_create. That makes no sense. It is unclear whether you are showing us the actual code. What is that hook actually implementing?
    – cilefen
    Oct 20, 2023 at 17:58
  • 1
    In the end registration is simply left open. 😅
    – leymannx
    Oct 20, 2023 at 18:36
  • @cilefen All it does is write to the file the fact that a user is created. Simple enough. I'm trying to log events like this to a file. Why is it constantly calling it for Anonymous users is the question I have.
    – dmgig
    Oct 20, 2023 at 20:47

1 Answer 1

1

You are misinterpreting the debugging results. This hook is for entity objects initialized in memory, for example, to render entity forms. A lot of entity forms rendered are never submitted. It's also quite common for other code to initialize entity objects and never intend to save them to the database.

You probably want to rebuild the router table when a new user is actually inserted into the database:

/**
 * Implements hook_ENTITY_TYPE_insert().
 */
function mymodule_user_insert(UserInterface $entity) {
  \Drupal::service('router.builder')->setRebuildNeeded();
}

See https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/group/entity_crud/10

BTW I've also replaced the router rebuild method with a different method which delays the rebuild until the Drupal kernel is shut down. The rebuild process is very time-consuming and you don't want to have it in the middle of the request.

Or most times not at all when saving entities. You might want to reconsider your routing scheme not to depend on single entities. Like it is in Drupal out of the box.

1
  • Ah interesting, I didn't understand the different between _create hook and the _insert hook. This is super helpful, thanks.
    – dmgig
    Oct 21, 2023 at 14:55

Your Answer

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

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