Skip to content
Snippets Groups Projects
Commit 482efab2 authored by Randal V's avatar Randal V Committed by Mingsong
Browse files

Issue #3479567 by randalv, mingsong: TermStorage::loadTree is not language aware

parent a1e5c067
No related branches found
No related tags found
1 merge request!13Issue #3479567: TermStorage::loadTree is not language aware
Pipeline #408784 passed with warnings
......@@ -5,6 +5,7 @@ namespace Drupal\tooltip_taxonomy\Services;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Component\Plugin\Factory\FactoryInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\node\Entity\Node;
......@@ -43,6 +44,13 @@ class TooltipManager {
*/
protected $entityTypeManager;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* The term storage.
*
......@@ -76,11 +84,12 @@ class TooltipManager {
* @param \Drupal\Core\Render\RendererInterface $renderer
* Render service instance.
*/
public function __construct(FactoryInterface $plugin_factory, FieldTypeManager $field_type_manager, EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer) {
public function __construct(FactoryInterface $plugin_factory, FieldTypeManager $field_type_manager, EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository, RendererInterface $renderer) {
$this->pathCondition = $plugin_factory->createInstance('request_path');
$this->contentTypeCondition = $plugin_factory->createInstance('entity_bundle:node');
$this->fieldTypeManager = $field_type_manager;
$this->entityTypeManager = $entity_type_manager;
$this->entityRepository = $entity_repository;
$this->renderer = $renderer;
$this->termStorage = $this->entityTypeManager->getStorage('taxonomy_term');
}
......@@ -307,45 +316,41 @@ class TooltipManager {
}
foreach ($vids as $vid) {
$term_tree = $this->loadAllTaxonomyTerms($vid);
if (!empty($term_tree)) {
foreach ($term_tree as $term) {
if ($term instanceof TermInterface) {
$access = $term->access('view');
}
else {
$access = $term->status ?? FALSE;
}
if (!$access) {
continue;
}
$term_ids = $this->loadAllTaxonomyTermIds($vid);
foreach ($term_ids as $tid) {
$term = $this->termStorage->load($tid);
if (!$term instanceof TermInterface || !$term->access('view')) {
continue;
}
$des = strip_tags((string) $term->description__value, $allowed_html_tags);
if (empty($des)) {
continue;
}
$tooltip_render = [
'#theme' => 'tooltip_taxonomy',
'#tooltip_id' => $term->vid . '-' . $term->tid,
'#term_name' => $term->name,
'#description' => $des,
];
$name_pattern = '/\b' . preg_quote($term->name, '/') . '\b/';
// Check if there is a same term name.
$name_key = array_search($name_pattern, $pattern_map['search']);
if ($name_key === FALSE) {
// New term.
$pattern_map['search'][] = $name_pattern;
$pattern_map['replace'][] = $this->renderer->renderPlain($tooltip_render);
}
else {
// Overwrite existing term.
// Conditions that has bigger weight value
// will overwrite the same term name.
$pattern_map['replace'][$name_key] = $this->renderer->renderPlain($tooltip_render);
}
$count++;
/** @var \Drupal\taxonomy\TermInterface $term */
$term = $this->entityRepository->getTranslationFromContext($term);
$des = strip_tags($term->get('description')->value, $allowed_html_tags);
if (empty($des)) {
continue;
}
$tooltip_render = [
'#theme' => 'tooltip_taxonomy',
'#tooltip_id' => $term->bundle() . '-' . $term->id(),
'#term_name' => $term->getName(),
'#description' => $des,
];
$name_pattern = '/\b' . preg_quote($term->getName(), '/') . '\b/';
// Check if there is a same term name.
$name_key = array_search($name_pattern, $pattern_map['search']);
if ($name_key === FALSE) {
// New term.
$pattern_map['search'][] = $name_pattern;
$pattern_map['replace'][] = $this->renderer->renderInIsolation($tooltip_render);
}
else {
// Overwrite existing term.
// Conditions that has bigger weight value
// will overwrite the same term name.
$pattern_map['replace'][$name_key] = $this->renderer->renderInIsolation($tooltip_render);
}
$count++;
}
}
......@@ -361,8 +366,11 @@ class TooltipManager {
* @return array
* All entities in the entity bundle.
*/
protected function loadAllTaxonomyTerms(string $vid) {
return $this->termStorage->loadTree($vid);
protected function loadAllTaxonomyTermIds(string $vid) {
return $this->termStorage->getQuery()
->accessCheck()
->condition('vid', $vid)
->execute();
}
/**
......
......@@ -7,6 +7,6 @@ services:
# Tooltip filter condition manager
tooltip_taxonomy.tooltip_manager:
class: \Drupal\tooltip_taxonomy\Services\TooltipManager
arguments: ['@plugin.manager.condition', '@tooltip_taxonomy.field_type_manager', '@entity_type.manager', '@renderer']
arguments: ['@plugin.manager.condition', '@tooltip_taxonomy.field_type_manager', '@entity_type.manager', '@entity.repository', '@renderer']
tags:
- { name: tooltip_taxonomy.tooltip_manager, priority: 1000 }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment