TaxonomyViewsIntegratorManager.php in Taxonomy Views Integrator 8
File
src/Service/TaxonomyViewsIntegratorManager.php
View source
<?php
namespace Drupal\tvi\Service;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Url;
use Drupal\taxonomy\TermInterface;
use Drupal\views\Views;
use Drupal\Core\Config\ConfigFactory;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class TaxonomyViewsIntegratorManager implements TaxonomyViewsIntegratorManagerInterface, ContainerAwareInterface {
use ContainerAwareTrait;
private $config;
private $entityTypeManager;
private $requestStack;
public function __construct(ConfigFactory $config, EntityTypeManagerInterface $entity_type_manager, RequestStack $requestStack) {
$this->config = $config;
$this->entityTypeManager = $entity_type_manager;
$this->requestStack = $requestStack;
}
public function getTermConfigSettings(TermInterface $taxonomy_term) {
return $this->config
->get('tvi.taxonomy_term.' . $taxonomy_term
->id());
}
public function getVocabularyConfigSettings(TermInterface $taxonomy_term) {
return $this->config
->get('tvi.taxonomy_vocabulary.' . $taxonomy_term
->bundle());
}
public function getDefaultConfigSettings() {
return $this->config
->get('tvi.global_settings');
}
public function getTermParents(TermInterface $taxonomy_term) {
return $this->entityTypeManager
->getStorage('taxonomy_term')
->loadAllParents($taxonomy_term
->id());
}
public function getRequestUriArguments() {
return array_slice(explode('/', $this->requestStack
->getCurrentRequest()
->getRequestUri()), 3);
}
public function getTaxonomyTermView(TermInterface $taxonomy_term) {
$view_info = $this
->getTaxonomyTermViewAndDisplayId($taxonomy_term);
$config = $this
->getTermConfigSettings($taxonomy_term);
$view_arguments = [
$taxonomy_term
->id(),
];
if ($config
->get('pass_arguments')) {
$view_arguments += $this
->getRequestUriArguments();
}
$build = NULL;
if ($view_info['display_id']) {
$view = Views::getView($view_info['view_id']);
if ($view && $view->storage
->status()) {
$view->override_url = Url::fromUri($this->requestStack
->getCurrentRequest()
->getUri());
$build = $view
->executeDisplay($view_info['display_id'], $view_arguments);
}
}
if (!$build) {
$build = $this->entityTypeManager
->getViewBuilder('taxonomy_term')
->view($taxonomy_term, 'full');
}
return $build;
}
public function getTaxonomyTermViewAndDisplayId(TermInterface $taxonomy_term) {
$view_name = 'taxonomy_term';
$view_id = 'page_1';
$global_config = $this
->getDefaultConfigSettings();
if ($global_config
->get('disable_default_view')) {
$view_name = NULL;
$view_id = NULL;
}
elseif ($global_config
->get('enable_override')) {
$view_name = $global_config
->get('view');
$view_id = $global_config
->get('view_display');
}
$term_config = $this
->getTermConfigSettings($taxonomy_term);
if ($term_config
->get('enable_override')) {
$view_name = $term_config
->get('view');
$view_id = $term_config
->get('view_display');
}
else {
$vocab_config = $this
->getVocabularyConfigSettings($taxonomy_term);
if ($vocab_config
->get('enable_override') && $vocab_config
->get('inherit_settings')) {
$view_name = $vocab_config
->get('view');
$view_id = $vocab_config
->get('view_display');
}
$parents = $this
->getTermParents($taxonomy_term);
unset($parents[$taxonomy_term
->id()]);
foreach ($parents as $parent) {
$parent_config = $this
->getTermConfigSettings($parent);
if ($parent_config
->get('enable_override') && $parent_config
->get('inherit_settings')) {
$view_name = $parent_config
->get('view');
$view_id = $parent_config
->get('view_display');
break;
}
}
}
return [
'view_id' => $view_name,
'display_id' => $view_id,
];
}
}