View source
<?php
namespace Drupal\taxonomy_manager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\language\Entity\ContentLanguageSettings;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TaxonomyManagerHelper {
protected $taxonomyTypeManager;
protected $languageManager;
protected $currentUser;
protected $moduleHandler;
public function __construct(EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, AccountInterface $current_user, ModuleHandlerInterface $module_handler) {
$this->taxonomyTypeManager = $entity_type_manager
->getStorage('taxonomy_term');
$this->languageManager = $language_manager;
$this->currentUser = $current_user;
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('language_manager'), $container
->get('current_user'), $container
->get('module_handler'));
}
public function vocabularyIsEmpty($vid) {
$term_storage = $this->taxonomyTypeManager;
return empty($term_storage
->loadTree($vid));
}
public function massAddTerms($input, $vid, $parents, array &$term_names_too_long = []) {
$new_terms = [];
$terms = explode("\n", str_replace("\r", '', $input));
$parents = !empty($parents) ? $parents : 0;
$last_parents = [];
foreach ($terms as $name) {
if (empty($name)) {
continue;
}
$matches = [];
if (preg_match('/^(-){1,}/', $name, $matches)) {
$depth = strlen($matches[0]);
$name = substr($name, $depth);
$current_parents = isset($last_parents[$depth - 1]) ? [
$last_parents[$depth - 1]
->id(),
] : 0;
}
elseif (preg_match('/^\\"(-){1,}.*\\"/', $name, $matches)) {
$name = substr($name, 1, -1);
$depth = 0;
$current_parents = $parents;
}
else {
$depth = 0;
$current_parents = $parents;
}
if (strlen($name) > 255) {
$term_names_too_long[] = $name;
$name = substr($name, 0, 255);
}
$filter_formats = filter_formats();
$format = array_pop($filter_formats);
$langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
if (\Drupal::moduleHandler()
->moduleExists('language')) {
$language_configuration = ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', $vid);
$lang_setting = $language_configuration
->getDefaultLangcode();
if ($lang_setting == 'site_default') {
$langcode = $this->languageManager
->getDefaultLanguage()
->getId();
}
elseif ($lang_setting == 'current_interface') {
$langcode = $this->languageManager
->getCurrentLanguage()
->getId();
}
elseif ($lang_setting == 'authors_default') {
$langcode = $this->currentUser
->getPreferredLangcode();
}
elseif ($lang_setting == 'und' || $lang_setting == 'zxx') {
$langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
}
else {
$langcode = $lang_setting;
}
}
$values = [
'name' => $name,
'format' => $format
->id(),
'vid' => $vid,
'langcode' => $langcode,
];
if (!empty($current_parents)) {
foreach ($current_parents as $p) {
$values['parent'][] = [
'target_id' => $p,
];
}
}
$term = $this->taxonomyTypeManager
->create($values);
$term
->save();
$new_terms[] = $term;
$last_parents[$depth] = $term;
}
return $new_terms;
}
public function deleteTerms(array $tids, $delete_orphans = FALSE) {
$deleted_terms = [];
$remaining_child_terms = [];
while (count($tids) > 0) {
$orphans = [];
foreach ($tids as $tid) {
$children = $this->taxonomyTypeManager
->loadChildren($tid);
if (!empty($children)) {
foreach ($children as $child) {
$parents = $this->taxonomyTypeManager
->loadParents($child
->id());
if ($delete_orphans) {
if (count($parents) == 1) {
$orphans[$child->tid] = $child
->id();
}
else {
$remaining_child_terms[$child
->id()] = $child
->getName();
}
}
else {
$remaining_child_terms[$child
->id()] = $child
->getName();
if ($parents) {
$parents_array = [];
foreach ($parents as $parent) {
if ($parent
->id() != $tid) {
$parent->target_id = $parent
->id();
$parents_array[$parent
->id()] = $parent;
}
}
if (empty($parents_array)) {
$parents_array = [
0,
];
}
$child->parent = $parents_array;
$this->taxonomyTypeManager
->deleteTermHierarchy([
$child
->id(),
]);
$this->taxonomyTypeManager
->updateTermHierarchy($child);
}
}
}
}
$term = $this->taxonomyTypeManager
->load($tid);
if ($term) {
$deleted_terms[] = $term
->getName();
$term
->delete();
}
}
$tids = $orphans;
}
return [
'deleted_terms' => $deleted_terms,
'remaining_child_terms' => $remaining_child_terms,
];
}
}