TermPreDump.php 924 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. namespace Drupal\reinstall\EventSubscriber;
  3. use Drupal\reinstall\DumperEvent;
  4. use Drupal\reinstall\DumperEvents;
  5. use Drupal\taxonomy\TermInterface;
  6. use Drupal\taxonomy\TermStorageInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class TermPreDump implements EventSubscriberInterface {
  9. public static function getSubscribedEvents() {
  10. return [
  11. DumperEvents::PRE_DUMP => 'onDumpPre',
  12. ];
  13. }
  14. protected static function setParents(TermInterface $term, $tid, TermStorageInterface $storage) {
  15. $parents = $storage->loadParents($term->id());
  16. if (!empty($parents)) {
  17. $term->set('parent', array_keys($parents));
  18. }
  19. }
  20. public static function onDumpPre(DumperEvent $event) {
  21. $storage = $event->storage;
  22. if ($storage->getEntityTypeId() !== 'taxonomy_term') {
  23. return;
  24. }
  25. array_walk($event->entities, [__CLASS__, 'setParents'], $storage);
  26. }
  27. }