You are here

function shs_term_has_children in Simple hierarchical select 2.0.x

Same name and namespace in other branches
  1. 8 shs.module \shs_term_has_children()
  2. 7 shs.module \shs_term_has_children()

Determine whether a term has children.

Parameters

int $tid: Term ID to check existence of children for.

Return value

bool TRUE if the term has any children, FALSE otherwise.

2 calls to shs_term_has_children()
OptionsShsWidget::validateElement in src/Plugin/Field/FieldWidget/OptionsShsWidget.php
Form validation handler for widget elements.
ShsController::getTermData in src/Controller/ShsController.php
Load term data.

File

./shs.module, line 108
Main functions and methods for the "Simple hierarchical select" module.

Code

function shs_term_has_children($tid) {
  $current_user = \Drupal::currentUser();

  /** @var \Drupal\taxonomy\TermInterface $term */
  $term = \Drupal::entityTypeManager()
    ->getStorage('taxonomy_term')
    ->load($tid);
  $access_unpublished = $current_user
    ->hasPermission('administer taxonomy') || $current_user
    ->hasPermission('view unpublished terms in ' . $term
    ->bundle());
  $query = Database::getConnection()
    ->select('taxonomy_term_field_data', 't');
  $query
    ->innerJoin('taxonomy_term__parent', 'p', 'p.entity_id = t.tid');
  $query
    ->condition('p.parent_target_id', $tid);
  $query
    ->addTag('term_access');
  if (!$access_unpublished) {

    // Filter to get published terms only.
    $query
      ->condition('t.status', 1);
  }
  return $query
    ->countQuery()
    ->execute()
    ->fetchField() > 0;
}