/** * Implements \Drupal\block\BlockBase::blockBuild(). */ public function build() { static $vocabularies, $terms; $items = array(); $faq_settings = \Drupal::config('faq.settings'); if (!$faq_settings->get('use_categories')) { return; } $moduleHandler = \Drupal::moduleHandler(); if ($moduleHandler->moduleExists('taxonomy')) { if (!isset($terms)) { $terms = array(); $vocabularies = Vocabulary::loadMultiple(); $vocab_omit = array_flip($faq_settings->get('omit_vocabulary')); $vocabularies = array_diff_key($vocabularies, $vocab_omit); foreach ($vocabularies as $vocab) { foreach (taxonomy_get_tree($vocab->vid) as $term) { if (FaqHelper::taxonomyTermCountNodes($term->tid)) { $terms[$term->name] = $term->tid; } } } } if (count($terms) > 0) { foreach ($terms as $name => $tid) { $items[] = l($name, 'faq-page/' . $tid); } } } return array('#theme' => 'item_list', '#items' => $items, '#list_type' => $faq_settings->get('category_listing')); }
/** * Return a structured array that consists a list of terms indented according to the term depth. * * @param $vid * Vocabulary id. * @param $tid * Term id. * @return * Return an array of a list of terms indented according to the term depth. */ private function _getIndentedFaqTerms($vid, $tid) { //if ($this->moduleHandler()->moduleExists('pathauto')) { // pathauto does't exists in D8 yet //} $faq_settings = \Drupal::config('faq.settings'); $display_faq_count = $faq_settings->get('count'); $hide_child_terms = $faq_settings->get('hide_child_terms'); $items = array(); $tree = taxonomy_get_tree($vid, $tid, 1, TRUE); foreach ($tree as $term) { $term_id = $term->id(); $tree_count = FaqHelper::taxonomyTermCountNodes($term_id); if ($tree_count) { // Get term description. $desc = ''; $term_description = $term->getDescription(); if (!empty($term_description)) { $desc = '<div class="faq-qa-description">'; $desc .= $term_description . "</div>"; } $query = db_select('node', 'n'); $query->join('node_field_data', 'd', 'n.nid = d.nid'); $query->innerJoin('taxonomy_index', 'ti', 'n.nid = ti.nid'); $term_node_count = $query->condition('d.status', 1)->condition('n.type', 'faq')->condition("ti.tid", $term_id)->addTag('node_access')->countQuery()->execute()->fetchField(); if ($term_node_count > 0) { $path = "faq-page/{$term_id}"; // pathauto is not exists in D8 yet //if (!\Drupal::service('path.alias_manager.cached')->getPathAlias(arg(0) . '/' . $tid) && $this->moduleHandler()->moduleExists('pathauto')) { //} if ($display_faq_count) { $count = $term_node_count; if ($hide_child_terms) { $count = $tree_count; } $cur_item = l($this->t($term->getName()), $path) . " ({$count}) " . $desc; } else { $cur_item = l($this->t($term->getName()), $path) . $desc; } } else { $cur_item = $this->t($term->getName()) . $desc; } if (!empty($term_image)) { $cur_item .= '<div class="clear-block"></div>'; } $term_items = array(); if (!$hide_child_terms) { $term_items = $this->_getIndentedFaqTerms($vid, $term_id); } $items[] = array("item" => $cur_item, "children" => $term_items); } } return $items; }
/** * Helper function to setup the list of sub-categories for the header. * * @param $term * The term to setup the list of child terms for. * @return * An array of sub-categories. */ public static function viewChildCategoryHeaders($term) { $child_categories = array(); $list = taxonomy_term_load_children($term->id()); foreach ($list as $tid => $child_term) { $term_node_count = FaqHelper::taxonomyTermCountNodes($child_term->id()); if ($term_node_count) { // Get taxonomy image. $term_image = ''; //taxonomy_image does not exists in D8 yet //if (module_exists('taxonomy_image')) { // $term_image = taxonomy_image_display($child_term->tid, array('class' => 'faq-tax-image')); //} $child_term_id = $child_term->id(); $term_vars['link'] = l(t($child_term->getName()), "faq-page/{$child_term_id}"); $term_vars['description'] = t($child_term->getDescription()); $term_vars['count'] = $term_node_count; $term_vars['term_image'] = $term_image; $child_categories[] = $term_vars; } } return $child_categories; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, $category = NULL) { //get category id from route values if (is_numeric(FaqHelper::arg(1))) { $category = FaqHelper::arg(1); } $order = $date_order = ''; $faq_settings = $this->config('faq.settings'); $use_categories = $faq_settings->get('use_categories'); if (!$use_categories) { $step = "order"; } elseif (!isset($form_state['values']) && empty($category)) { $step = "categories"; } else { $step = "order"; } $form['step'] = array('#type' => 'value', '#value' => $step); // Categorized q/a. if ($step == "categories") { // Get list of categories. $vocabularies = Vocabulary::loadMultiple(); $options = array(); foreach ($vocabularies as $vid => $vobj) { $tree = taxonomy_get_tree($vid); foreach ($tree as $term) { if (!FaqHelper::taxonomyTermCountNodes($term->tid)) { continue; } $options[$term->tid] = $this->t($term->name); $form['choose_cat']['faq_category'] = array('#type' => 'select', '#title' => t('Choose a category'), '#description' => t('Choose a category that you wish to order the questions for.'), '#options' => $options, '#multiple' => FALSE); $form['choose_cat']['search'] = array('#type' => 'submit', '#value' => t('Search'), '#submit' => array('faq_order_settings_choose_cat_form_submit')); } } } else { $default_sorting = $faq_settings->get('default_sorting'); $default_weight = 0; if ($default_sorting != 'DESC') { $default_weight = 1000000; } $options = array(); if (!empty($form_state['values']['faq_category'])) { $category = $form_state['values']['faq_category']; } // Uncategorized ordering. $query = db_select('node', 'n'); $query->join('node_field_data', 'd', 'n.nid = d.nid'); $query->fields('n', array('nid'))->fields('d', array('title'))->addTag('node_access')->condition('n.type', 'faq')->condition('d.status', 1); // Works, but involves variable concatenation - safe though, since // $default_weight is an integer. $query->addExpression("COALESCE(w.weight, {$default_weight})", 'effective_weight'); // Doesn't work in Postgres. //$query->addExpression('COALESCE(w.weight, CAST(:default_weight as SIGNED))', 'effective_weight', array(':default_weight' => $default_weight)); if (empty($category)) { $category = 0; $w_alias = $query->leftJoin('faq_weights', 'w', 'n.nid = %alias.nid AND %alias.tid = :category', array(':category' => $category)); $query->orderBy('effective_weight', 'ASC')->orderBy('d.sticky', 'DESC')->orderBy('d.created', $default_sorting == 'DESC' ? 'DESC' : 'ASC'); } else { $ti_alias = $query->innerJoin('taxonomy_index', 'ti', '(n.nid = %alias.nid)'); $w_alias = $query->leftJoin('faq_weights', 'w', 'n.nid = %alias.nid AND %alias.tid = :category', array(':category' => $category)); $query->condition('ti.tid', $category); $query->orderBy('effective_weight', 'ASC')->orderBy('d.sticky', 'DESC')->orderBy('d.created', $default_sorting == 'DESC' ? 'DESC' : 'ASC'); } $options = $query->execute()->fetchAll(); $form['weight']['faq_category'] = array('#type' => 'value', '#value' => $category); // Show table ordering form. $form['order_no_cats']['#tree'] = TRUE; $form['order_no_cats']['#theme'] = 'faq_draggable_question_order_table'; foreach ($options as $i => $record) { $form['order_no_cats'][$i]['nid'] = array('#type' => 'hidden', '#value' => $record->nid); $form['order_no_cats'][$i]['title'] = array('#markup' => String::checkPlain($record->title)); $form['order_no_cats'][$i]['sort'] = array('#type' => 'weight', '#delta' => count($options), '#default_value' => $i); } $form['actions']['#type'] = 'actions'; $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Save order'), '#button_type' => 'primary'); } return $form; }