You are here

public function TermMerger::mergeIntoTerm in Term Merge 8

Merges one or more terms into an existing term.

Parameters

array $termsToMerge: The terms to merge.

\Drupal\taxonomy\TermInterface $targetTerm: The term to merge them into.

Overrides TermMergerInterface::mergeIntoTerm

1 call to TermMerger::mergeIntoTerm()
TermMerger::mergeIntoNewTerm in src/TermMerger.php
Merges two or more terms into a new term.

File

src/TermMerger.php, line 89

Class

TermMerger
Implements TermMergerInterface to provide a term merger service.

Namespace

Drupal\term_merge

Code

public function mergeIntoTerm(array $termsToMerge, TermInterface $targetTerm) {
  $this
    ->validateTerms($termsToMerge);

  // We have to save the term to make sure we've got an id to reference.
  if ($targetTerm
    ->isNew()) {
    $targetTerm
      ->save();
  }
  $firstTerm = reset($termsToMerge);
  if ($firstTerm
    ->bundle() !== $targetTerm
    ->bundle()) {
    throw new \RuntimeException('The target term must be in the same vocabulary as the terms being merged');
  }
  $this
    ->migrateReferences($termsToMerge, $targetTerm);
  $event = new TermsMergedEvent($termsToMerge, $targetTerm);
  $event = $this->dispatcher
    ->dispatch(TermMergeEventNames::TERMS_MERGED, $event);
  $this->termStorage
    ->delete($termsToMerge);
}