You are here

class MixitupFunc in MixItUp Views 8

Same name and namespace in other branches
  1. 8.2 src/MixitupFunc.php \Drupal\mixitup_views\MixitupFunc

Performs assistance functionality.

@package Drupal\mixitup_views

Hierarchy

Expanded class hierarchy of MixitupFunc

3 files declare their use of MixitupFunc
MixitupFiltersForm.php in src/Form/MixitupFiltersForm.php
MixitUpFiltersFormTest.php in tests/src/Kernel/MixitUpFiltersFormTest.php
MixItUpFuncTest.php in tests/src/Kernel/MixItUpFuncTest.php
1 string reference to 'MixitupFunc'
mixitup_views.services.yml in ./mixitup_views.services.yml
mixitup_views.services.yml
1 service uses MixitupFunc
mixitup_views.func_service in ./mixitup_views.services.yml
Drupal\mixitup_views\MixitupFunc

File

src/MixitupFunc.php, line 14

Namespace

Drupal\mixitup_views
View source
class MixitupFunc {

  /**
   * Static array for store active nodes terms.
   *
   * @var array
   */
  protected static $populatedFilters = [];

  /**
   * Static array for store information about which nodes have a specific tid.
   *
   * @var array
   */
  protected static $nodeFilters = [];

  /**
   * Default options service.
   *
   * @var null
   */
  protected $defaultOptionsService;

  /**
   * EntityTypeManager service.
   *
   * @var object
   */
  protected $entityTypeManager;

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * Constructor.
   *
   * @param \Drupal\mixitup_views\MixitupViewsDefaultOptionsService $defaultOptionsService
   *   MixitupViewsDefaultOptionsService service.
   * @param \Drupal\Core\Entity\EntityTypeManager $entityTypeManagerService
   *   EntityTypeManager service.
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection.
   */
  public function __construct(MixitupViewsDefaultOptionsService $defaultOptionsService, EntityTypeManager $entityTypeManagerService, Connection $connection) {
    $this->defaultOptionsService = $defaultOptionsService;
    $this->entityTypeManager = $entityTypeManagerService;
    $this->connection = $connection;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('mixitup_views.default_options_service'), $container
      ->get('entity_type.manager'), $container
      ->get('database'));
  }

  /**
   * Get classes string for node.
   *
   * @param int $nid
   *   Node id.
   *
   * @return string
   *   Classes string.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function getRowClasses($nid) {
    $tids = $this
      ->getNodeTids($nid);
    $classes = [];
    if (!empty($tids)) {
      foreach ($tids as $tid) {
        $classes[] = 'tid_' . $tid;
        $this
          ->populateFilters($tid, $nid);
      }
    }
    $classes = implode(' ', $classes);
    return $classes;
  }

  /**
   * Get all node's taxonomy ids.
   *
   * @param int $nid
   *   Node id.
   *
   * @return array
   *   Array of tids.
   */
  public function getNodeTids($nid) {
    $tids = $this->connection
      ->select('taxonomy_index', 'ti')
      ->fields('ti', [
      'tid',
      'nid',
    ])
      ->condition('ti.nid', $nid)
      ->execute()
      ->fetchAllKeyed();
    return array_keys($tids);
  }

  /**
   * Populates structured array of used taxonomy terms.
   *
   * @param int $tid
   *   Taxonomy id.
   * @param int $nid
   *   Node id.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function populateFilters($tid, $nid) {
    $term = $this->entityTypeManager
      ->getStorage('taxonomy_term')
      ->load($tid);

    /* @var \Drupal\taxonomy\Entity\Term $term */
    if (!empty($term)) {
      $vid = $term
        ->bundle();
      self::$populatedFilters[$vid]['.tid_' . $tid] = $term
        ->getName();
      $this
        ->populateNodeFilters($nid, $tid);
    }
  }

  /**
   * Collects information regarding wich nodes have a specific tid.
   *
   * @param int $nid
   *   Node id.
   * @param int $tid
   *   Taxonomy id.
   */
  public function populateNodeFilters($nid, $tid) {
    self::$nodeFilters[$tid][] = $nid;
  }

  /**
   * Gets populated filters.
   *
   * @return array
   *   Array with structure item[vid]['tid_{tid}'] = term_name.
   */
  public function getPopulatedFilters() {
    return self::$populatedFilters;
  }

  /**
   * Gets populated node filters.
   *
   * @return array
   *   Array with structure item[tid] => array(nids).
   */
  public function getPopulatedNodeFilters() {
    return self::$nodeFilters;
  }

  /**
   * Get default mixitup options.
   *
   * @param bool $convert
   *   Convert check.
   *
   * @return mixed
   *   Array of default options.
   */
  public function getDefaultOptions($convert = NULL) {
    return $this->defaultOptionsService
      ->defaultOptions($convert);
  }

  /**
   * Checks is mixitup js file exists.
   *
   * @return bool
   *   True or False.
   */
  public function isMixitupInstalled() {
    if (is_file('libraries/mixitup/dist/mixitup.min.js')) {
      return TRUE;
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MixitupFunc::$connection protected property The database connection.
MixitupFunc::$defaultOptionsService protected property Default options service.
MixitupFunc::$entityTypeManager protected property EntityTypeManager service.
MixitupFunc::$nodeFilters protected static property Static array for store information about which nodes have a specific tid.
MixitupFunc::$populatedFilters protected static property Static array for store active nodes terms.
MixitupFunc::create public static function
MixitupFunc::getDefaultOptions public function Get default mixitup options.
MixitupFunc::getNodeTids public function Get all node's taxonomy ids.
MixitupFunc::getPopulatedFilters public function Gets populated filters.
MixitupFunc::getPopulatedNodeFilters public function Gets populated node filters.
MixitupFunc::getRowClasses public function Get classes string for node.
MixitupFunc::isMixitupInstalled public function Checks is mixitup js file exists.
MixitupFunc::populateFilters public function Populates structured array of used taxonomy terms.
MixitupFunc::populateNodeFilters public function Collects information regarding wich nodes have a specific tid.
MixitupFunc::__construct public function Constructor.