You are here

function SelectedFilters::__construct in Taxonomy Facets 8

File

src/SelectedFilters.php, line 23

Class

SelectedFilters

Namespace

Drupal\taxonomy_facets

Code

function __construct($term_names = array()) {

  // Set filters
  foreach ($term_names as $term_name) {
    if ($tid = self::getTermIdFromUrlAlias($term_name)) {
      $this->terms[] = \Drupal\taxonomy\Entity\Term::load($tid);
    }
  }

  // Sort by vocabulary id so that we always get same order of filters, to avoid
  // duplicate urls for the same page.
  if ($this->terms) {
    usort($this->terms, function ($a, $b) {
      if ((int) $a
        ->id() == (int) $b
        ->id()) {
        return 0;
      }
      if ((int) $a
        ->id() < (int) $b
        ->id()) {
        return -1;
      }
      else {
        return 1;
      }
    });
  }
}