View source
<?php
namespace Drupal\taxonomy_facets;
class MenuLeaf {
private $vid = NULL;
public $tid = NULL;
public $termName = NULL;
public $urlAlias = NULL;
public $leafClass = NULL;
public $linkUrl = NULL;
public $leafAnchorClass = NULL;
private $filtersObject = NULL;
public function __construct($leaf_term_object, $leafClass = array()) {
$this->filtersObject = taxonomy_facets_get_selected_filters();
$this->termName = $leaf_term_object->name;
$this->tid = $leaf_term_object->tid;
$this->vid = $leaf_term_object->vid;
if ($leafClass) {
$this->leafAnchorClass = $leafClass['leafAnchorClass'];
$this->leafClass = $leafClass['leafClass'];
}
$this
->getTermUrlAlias();
$this
->buildLinkUrl();
}
private function getTermUrlAlias() {
$query = db_select('url_alias', 'u')
->fields('u', array(
'alias',
))
->condition('source', '/taxonomy/term/' . $this->tid)
->execute();
if ($aliases = $query
->fetchAll()) {
$aliases = current($aliases)->alias;
$this->urlAlias = ltrim($aliases, "/");
}
else {
drupal_set_message(t('You do not have any url aliases generate for taxonomy terms, see Taxonomy Facets module readme file.'), 'warning');
}
}
private function buildLinkUrl() {
$url = [];
$noTermFromCurrentVocabularyFound = TRUE;
$filters = null;
if ($this->filtersObject) {
$filters = $this->filtersObject
->getAppliedFilters();
}
if ($filters) {
foreach ($filters as $filter) {
if ($filter
->getVocabularyId() == $this->vid) {
$obj = new \stdClass();
$obj->vid = $this->vid;
$obj->url = $this->urlAlias;
$url[] = $obj;
$noTermFromCurrentVocabularyFound = FALSE;
}
else {
$obj = new \stdClass();
$obj->vid = $filter
->getVocabularyId();
$obj->url = ltrim($filter
->url(), "/");
$url[] = $obj;
}
}
}
if ($noTermFromCurrentVocabularyFound == TRUE) {
$obj = new \stdClass();
$obj->vid = $this->vid;
$obj->url = $this->urlAlias;
$url[] = $obj;
}
$this->linkUrl = $this
->getLanguagePrefix() . '/listings';
usort($url, function ($a, $b) {
if ($a->vid == $b->vid) {
return 0;
}
return $a->vid < $b->vid ? -1 : 1;
});
foreach ($url as $u) {
$this->linkUrl .= '/' . $u->url;
}
}
function getLanguagePrefix() {
if ($prefixes = \Drupal::config('language.negotiation')
->get('url.prefixes')) {
$language = \Drupal::languageManager()
->getCurrentLanguage()
->getId();
if ($prefixes[$language]) {
return "/" . $prefixes[$language];
}
}
return null;
}
}