I have a module which limits the options in a views exposed filter to terms which have associated nodes. The code is based on this answer: How can I limit the exposed filters to only display values for which content exists?
Code:
if (isset($form['area-of-law']['#options'])) {
foreach ($form['area-of-law']['#options'] as $key => $value) {
if (is_numeric($key)) {
$query = \Drupal::database()->select('taxonomy_index', 'ti');
$query->fields('ti', ['nid']);
$query->condition('ti.tid', $key);
$nodes = $query->execute()->fetchAssoc();
if (!$nodes) {
unset($form['area-of-law']['#options'][$key]);
}
}
}
}
This works, but all node types are returned. I would like to limit the filter further to only show those of content type X.