You are here

public function TermMerger::mergeIntoNewTerm in Term Merge 8

Merges two or more terms into a new term.

Parameters

\Drupal\taxonomy\TermInterface[] $termsToMerge: The terms to merge.

string $newTermLabel: The label of the new term.

Return value

\Drupal\taxonomy\TermInterface The newly created term.

Overrides TermMergerInterface::mergeIntoNewTerm

File

src/TermMerger.php, line 69

Class

TermMerger
Implements TermMergerInterface to provide a term merger service.

Namespace

Drupal\term_merge

Code

public function mergeIntoNewTerm(array $termsToMerge, $newTermLabel) {
  $this
    ->validateTerms($termsToMerge);
  $firstTerm = reset($termsToMerge);
  $values = [
    'name' => $newTermLabel,
    'vid' => $firstTerm
      ->bundle(),
  ];

  /** @var \Drupal\taxonomy\TermInterface $newTerm */
  $newTerm = $this->termStorage
    ->create($values);
  $this
    ->mergeIntoTerm($termsToMerge, $newTerm);
  return $newTerm;
}