UserPreImport.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace Drupal\reinstall\EventSubscriber;
  3. use Drupal\reinstall\ReinstallEvents;
  4. use Drupal\reinstall\SourceEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. /**
  7. * Class UserPreImport is an EventSubscriber filtering user sources.
  8. */
  9. class UserPreImport implements EventSubscriberInterface {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public static function getSubscribedEvents() {
  14. return [
  15. ReinstallEvents::POST_SOURCE_PARSE => 'onPreImport',
  16. ];
  17. }
  18. /**
  19. * Event callback for POST_SOURCE_PARSE.
  20. *
  21. * @param \Drupal\reinstall\SourceEvent $event
  22. * The event.
  23. */
  24. public function onPreImport(SourceEvent $event) {
  25. $source = $event->source;
  26. if ($source->getConfiguration()['type'] !== 'user') {
  27. return;
  28. }
  29. $event->source->records = array_filter($event->source->records, [$this, 'filter01']);
  30. }
  31. /**
  32. * Skip users 0 and 1 in imports, as they are core-provided.
  33. *
  34. * @param array $record
  35. * The description of a user entity.
  36. *
  37. * @return bool
  38. * Include it (1) or filter it (0).
  39. */
  40. public function filter01(array $record) {
  41. $ret = ($record['uid'] ?? 0) > 1;
  42. return $ret;
  43. }
  44. }