TermPreDump.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace Drupal\reinstall\EventSubscriber;
  3. use Drupal\reinstall\DumperEvent;
  4. use Drupal\reinstall\ReinstallEvents;
  5. use Drupal\taxonomy\TermInterface;
  6. use Drupal\taxonomy\TermStorageInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. /**
  9. * Class TermPreDump adds the parents target_id values on a term.
  10. */
  11. class TermPreDump implements EventSubscriberInterface {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public static function getSubscribedEvents() {
  16. return [
  17. ReinstallEvents::PRE_DUMP => 'onDumpPre',
  18. ];
  19. }
  20. /**
  21. * Set the parents on the term.
  22. *
  23. * @param \Drupal\taxonomy\TermInterface $term
  24. * The term to complete.
  25. * @param int $tid
  26. * The term id.
  27. * @param \Drupal\taxonomy\TermStorageInterface $storage
  28. * The term storage.
  29. */
  30. protected static function setParents(TermInterface $term, $tid, TermStorageInterface $storage) {
  31. $parents = $storage->loadParents($term->id());
  32. if (!empty($parents)) {
  33. $term->set('parent', array_keys($parents));
  34. }
  35. }
  36. /**
  37. * Callback for PRE_DUMP event.
  38. *
  39. * @param \Drupal\reinstall\DumperEvent $event
  40. * The pre-dump event.
  41. */
  42. public static function onDumpPre(DumperEvent $event) {
  43. $storage = $event->storage;
  44. if ($storage->getEntityTypeId() !== 'taxonomy_term') {
  45. return;
  46. }
  47. array_walk($event->entities, [__CLASS__, 'setParents'], $storage);
  48. }
  49. }