You are here

function _tft_get_group_gid in Taxonomy File Tree 8

Same name and namespace in other branches
  1. 3.x tft.module \_tft_get_group_gid()

Check if the current term is part of a Group and return the Group id.

If no id is found, return FALSE.

Parameters

int $tid: The tid (and its ancestor tree) to check against.

Return value

int|bool The Group id if found, else FALSE.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

6 calls to _tft_get_group_gid()
TFTController::add_content_links in src/Controller/TFTController.php
Render the add file and add folder links.
TFTController::ajaxGetFolder in src/Controller/TFTController.php
Returns folder.
tft_file_access in ./tft.module
Implements hook_ENTITY_TYPE_access().
tft_taxonomy_term_access in ./tft.module
Implements hook_ENTITY_TYPE_access().
tft_taxonomy_term_create_access in ./tft.module
Implements hook_ENTITY_TYPE_create_access().

... See full list

File

./tft.module, line 42
Contains tft.module.

Code

function _tft_get_group_gid($tid) {
  static $cache = [];
  if (is_array($tid)) {
    $tid = reset($tid);
  }
  $tid = (int) $tid;
  if (!$tid) {
    return FALSE;
  }
  if (isset($cache[$tid])) {
    return $cache[$tid];
  }
  $param_tid = $tid;

  // Get $gid for $tid.
  $gids = \Drupal::entityQuery('group')
    ->condition('type', 'learning_path')
    ->condition('field_learning_path_folder.target_id', $tid)
    ->execute();
  $gid = reset($gids);
  while ($tid && !$gid) {

    // Get parent $tid.

    /** @var \Drupal\taxonomy\TermStorage $storage */
    $storage = \Drupal::entityTypeManager()
      ->getStorage('taxonomy_term');
    $result = $storage
      ->loadParents($tid);
    $result = reset($result);
    $tid = empty($result) ? FALSE : $result
      ->id();

    // Get $gid for $tid.
    $gids = \Drupal::entityQuery('group')
      ->condition('type', 'learning_path')
      ->condition('field_learning_path_folder.target_id', $tid)
      ->execute();
    $gid = reset($gids);
  }
  if ($gid) {
    $cache[$param_tid] = (int) $gid;
  }
  else {
    $cache[$param_tid] = FALSE;
  }
  return $cache[$param_tid];
}