/** * Test terms in a single and multiple hierarchy. */ function testTaxonomyTermHierarchy() { // Create two taxonomy terms. $term1 = $this->createTerm($this->vocabulary); $term2 = $this->createTerm($this->vocabulary); // Check that hierarchy is flat. $vocabulary = entity_load('taxonomy_vocabulary', $this->vocabulary->id()); $this->assertEqual(0, $vocabulary->hierarchy, 'Vocabulary is flat.'); // Edit $term2, setting $term1 as parent. $edit = array(); $edit['parent[]'] = array($term1->id()); $this->drupalPostForm('taxonomy/term/' . $term2->id() . '/edit', $edit, t('Save')); // Check the hierarchy. $children = taxonomy_term_load_children($term1->id()); $parents = taxonomy_term_load_parents($term2->id()); $this->assertTrue(isset($children[$term2->id()]), 'Child found correctly.'); $this->assertTrue(isset($parents[$term1->id()]), 'Parent found correctly.'); // Load and save a term, confirming that parents are still set. $term = entity_load('taxonomy_term', $term2->id()); $term->save(); $parents = taxonomy_term_load_parents($term2->id()); $this->assertTrue(isset($parents[$term1->id()]), 'Parent found correctly.'); // Create a third term and save this as a parent of term2. $term3 = $this->createTerm($this->vocabulary); $term2->parent = array($term1->id(), $term3->id()); $term2->save(); $parents = taxonomy_term_load_parents($term2->id()); $this->assertTrue(isset($parents[$term1->id()]) && isset($parents[$term3->id()]), 'Both parents found successfully.'); }
/** * Tests that many terms with parents show on each page */ function testTaxonomyTermChildTerms() { // Set limit to 10 terms per page. Set variable to 9 so 10 terms appear. \Drupal::config('taxonomy.settings')->set('terms_per_page_admin', '9')->save(); $term1 = $this->createTerm($this->vocabulary); $terms_array = ''; // Create 40 terms. Terms 1-12 get parent of $term1. All others are // individual terms. for ($x = 1; $x <= 40; $x++) { $edit = array(); // Set terms in order so we know which terms will be on which pages. $edit['weight'] = $x; // Set terms 1-20 to be children of first term created. if ($x <= 12) { $edit['parent'] = $term1->id(); } $term = $this->createTerm($this->vocabulary, $edit); $children = taxonomy_term_load_children($term1->id()); $parents = taxonomy_term_load_parents($term->id()); $terms_array[$x] = taxonomy_term_load($term->id()); } // Get Page 1. $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview'); $this->assertText($term1->getName(), 'Parent Term is displayed on Page 1'); for ($x = 1; $x <= 13; $x++) { $this->assertText($terms_array[$x]->getName(), $terms_array[$x]->getName() . ' found on Page 1'); } // Get Page 2. $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview', array('query' => array('page' => 1))); $this->assertText($term1->getName(), 'Parent Term is displayed on Page 2'); for ($x = 1; $x <= 18; $x++) { $this->assertText($terms_array[$x]->getName(), $terms_array[$x]->getName() . ' found on Page 2'); } // Get Page 3. $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview', array('query' => array('page' => 2))); $this->assertNoText($term1->getName(), 'Parent Term is not displayed on Page 3'); for ($x = 1; $x <= 17; $x++) { $this->assertNoText($terms_array[$x]->getName(), $terms_array[$x]->getName() . ' not found on Page 3'); } for ($x = 18; $x <= 25; $x++) { $this->assertText($terms_array[$x]->getName(), $terms_array[$x]->getName() . ' found on Page 3'); } }
/** * {@inheritdoc} */ public static function postDelete(EntityStorageInterface $storage, array $entities) { parent::postDelete($storage, $entities); // See if any of the term's children are about to be become orphans. $orphans = array(); foreach (array_keys($entities) as $tid) { if ($children = taxonomy_term_load_children($tid)) { foreach ($children as $child) { // If the term has multiple parents, we don't delete it. $parents = taxonomy_term_load_parents($child->id()); if (empty($parents)) { $orphans[] = $child->id(); } } } } // Delete term hierarchy information after looking up orphans but before // deleting them so that their children/parent information is consistent. $storage->deleteTermHierarchy(array_keys($entities)); if (!empty($orphans)) { entity_delete_multiple('taxonomy_term', $orphans); } }
/** * Display FAQ questions and answers filtered by category. * * @param $faq_display * Define the way the FAQ is being shown; can have the values: * 'questions top',hide answers','questions inline','new page'. * @param $category_display * The layout of categories which should be used. * @param $term * The category / term to display FAQs for. * @param $display_header * Set if the header will be shown or not. * @param &$output * Reference which holds the content of the page, HTML formatted. * @param &$output_answer * Reference which holds the answers from the FAQ, when showing questions * on top. */ private function _displayFaqByCategory($faq_display, $category_display, $term, $display_header, &$output, &$output_answers) { $default_sorting = \Drupal::config('faq.settings')->get('default_sorting'); $term_id = $term->id(); $query = db_select('node', 'n'); $query->join('node_field_data', 'd', 'd.nid = n.nid'); $query->innerJoin('taxonomy_index', 'ti', 'n.nid = ti.nid'); $query->leftJoin('faq_weights', 'w', 'w.tid = ti.tid AND n.nid = w.nid'); $query->fields('n', array('nid'))->condition('n.type', 'faq')->condition('d.status', 1)->condition("ti.tid", $term_id)->addTag('node_access'); $default_weight = 0; if ($default_sorting == 'ASC') { $default_weight = 1000000; } // 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)); $query->orderBy('effective_weight', 'ASC')->orderBy('d.sticky', 'DESC'); if ($default_sorting == 'ASC') { $query->orderBy('d.created', 'ASC'); } else { $query->orderBy('d.created', 'DESC'); } // We only want the first column, which is nid, so that we can load all // related nodes. $nids = $query->execute()->fetchCol(); $data = Node::loadMultiple($nids); // Handle indenting of categories. $depth = 0; if (!isset($term->depth)) { $children = taxonomy_term_load_children($term->id()); $term->depth = count($children); } while ($depth < $term->depth) { $display_header = 1; $indent = '<div class="faq-category-indent">'; $output .= $indent; $depth++; } // Set up the class name for hiding the q/a for a category if required. $faq_class = "faq-qa"; if ($category_display == "hide_qa") { $faq_class = "faq-qa-hide"; } $output_render = $output_answers_render = array('#data' => $data, '#display_header' => $display_header, '#category_display' => $category_display, '#term' => $term, '#class' => $faq_class, '#parent_term' => $term); switch ($faq_display) { case 'questions_top': $output_render['#theme'] = 'faq_category_questions_top'; $output .= drupal_render($output_render); $output_answers_render['#theme'] = 'faq_category_questions_top_answers'; $output_answers .= drupal_render($output_answers_render); break; case 'hide_answer': $output_render['#theme'] = 'faq_category_hide_answer'; $output .= drupal_render($output_render); break; case 'questions_inline': $output_render['#theme'] = 'faq_category_questions_inline'; $output .= drupal_render($output_render); break; case 'new_page': $output_render['#theme'] = 'faq_category_new_page'; $output .= drupal_render($output_render); break; } // Handle indenting of categories. while ($depth > 0) { $output .= '</div>'; $depth--; } }
/** * 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; }