/** * {@inheritdoc} */ public function build(RouteMatchInterface $route_match) { $term = $route_match->getParameter('taxonomy_term'); // @todo This overrides any other possible breadcrumb and is a pure // hard-coded presumption. Make this behavior configurable per // vocabulary or term. $breadcrumb = array(); while ($parents = taxonomy_term_load_parents($term->id())) { $term = array_shift($parents); $breadcrumb[] = Link::createFromRoute($term->getName(), 'entity.taxonomy_term.canonical', array('taxonomy_term' => $term->id())); } $breadcrumb[] = Link::createFromRoute($this->t('Home'), '<front>'); $breadcrumb = array_reverse($breadcrumb); return $breadcrumb; }
/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $term = $this->entity; $vocab_storage = $this->entityManager->getStorage('taxonomy_vocabulary'); $vocabulary = $vocab_storage->load($term->bundle()); $parent = array_keys(taxonomy_term_load_parents($term->id())); $form_state['taxonomy']['parent'] = $parent; $form_state['taxonomy']['vocabulary'] = $vocabulary; $language_configuration = $this->moduleHandler->moduleExists('language') ? language_get_default_configuration('taxonomy_term', $vocabulary->id()) : FALSE; $form['langcode'] = array('#type' => 'language_select', '#title' => $this->t('Language'), '#languages' => LanguageInterface::STATE_ALL, '#default_value' => $term->getUntranslated()->language()->id, '#access' => !empty($language_configuration['language_show'])); $form['relations'] = array('#type' => 'details', '#title' => $this->t('Relations'), '#open' => $vocabulary->hierarchy == TAXONOMY_HIERARCHY_MULTIPLE, '#weight' => 10); // taxonomy_get_tree and taxonomy_term_load_parents may contain large // numbers of items so we check for taxonomy.settings:override_selector // before loading the full vocabulary. Contrib modules can then intercept // before hook_form_alter to provide scalable alternatives. if (!$this->config('taxonomy.settings')->get('override_selector')) { $parent = array_keys(taxonomy_term_load_parents($term->id())); $children = taxonomy_get_tree($vocabulary->id(), $term->id()); // A term can't be the child of itself, nor of its children. foreach ($children as $child) { $exclude[] = $child->tid; } $exclude[] = $term->id(); $tree = taxonomy_get_tree($vocabulary->id()); $options = array('<' . $this->t('root') . '>'); if (empty($parent)) { $parent = array(0); } foreach ($tree as $item) { if (!in_array($item->tid, $exclude)) { $options[$item->tid] = str_repeat('-', $item->depth) . $item->name; } } $form['relations']['parent'] = array('#type' => 'select', '#title' => $this->t('Parent terms'), '#options' => $options, '#default_value' => $parent, '#multiple' => TRUE); } $form['relations']['weight'] = array('#type' => 'textfield', '#title' => $this->t('Weight'), '#size' => 6, '#default_value' => $term->getWeight(), '#description' => $this->t('Terms are displayed in ascending order by weight.'), '#required' => TRUE); $form['vid'] = array('#type' => 'value', '#value' => $vocabulary->id()); $form['tid'] = array('#type' => 'value', '#value' => $term->id()); if ($term->isNew()) { $form_state['redirect'] = current_path(); } return parent::form($form, $form_state, $term); }
/** * Function to set up the FAQ breadcrumbs for a given taxonomy term. * * @param $term * The taxonomy term object. */ public static function setFaqBreadcrumb($term = NULL) { $faq_settings = \Drupal::config('faq.settings'); $site_settings = \Drupal::config('system.site'); $breadcrumb = array(); if ($faq_settings->get('custom_breadcrumbs')) { if (\Drupal::moduleHandler()->moduleExists('taxonomy') && $term) { $breadcrumb[] = l(t($term->getName()), 'faq-page/' . $term->id()); while ($parents = taxonomy_term_load_parents($term->id())) { $term = array_shift($parents); $breadcrumb[] = l(t($term->getName()), 'faq-page/' . $term->id()); } } $breadcrumb[] = l($faq_settings->get('title'), 'faq-page'); $breadcrumb[] = l(t('Home'), NULL, array('attributes' => array('title' => $site_settings->get('name')))); $breadcrumb = array_reverse($breadcrumb); } return $breadcrumb; }
/** * Tests the Drupal 6 taxonomy term to Drupal 8 migration. */ public function testTaxonomyTerms() { $expected_results = array('1' => array('source_vid' => 1, 'vid' => 'vocabulary_1_i_0_', 'weight' => 0, 'parent' => array(2)), '2' => array('source_vid' => 2, 'vid' => 'vocabulary_2_i_1_', 'weight' => 3, 'parent' => array(0)), '3' => array('source_vid' => 2, 'vid' => 'vocabulary_2_i_1_', 'weight' => 4, 'parent' => array(2)), '4' => array('source_vid' => 3, 'vid' => 'vocabulary_3_i_2_', 'weight' => 6, 'parent' => array(0)), '5' => array('source_vid' => 3, 'vid' => 'vocabulary_3_i_2_', 'weight' => 7, 'parent' => array(4)), '6' => array('source_vid' => 3, 'vid' => 'vocabulary_3_i_2_', 'weight' => 8, 'parent' => array(4, 5))); $terms = Term::loadMultiple(array_keys($expected_results)); foreach ($expected_results as $tid => $values) { /** @var Term $term */ $term = $terms[$tid]; $this->assertIdentical($term->name->value, "term {$tid} of vocabulary {$values['source_vid']}"); $this->assertIdentical($term->description->value, "description of term {$tid} of vocabulary {$values['source_vid']}"); $this->assertEqual($term->vid->target_id, $values['vid']); $this->assertEqual($term->weight->value, $values['weight']); if ($values['parent'] === array(0)) { $this->assertEqual($term->parent->target_id, 0); } else { $parents = array(); foreach (taxonomy_term_load_parents($tid) as $parent) { $parents[] = $parent->id(); } $this->assertEqual($values['parent'], $parents); } } }
/** * {@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); } }
/** * Tests term indentation. */ function testTermIndentation() { // Create three taxonomy terms. $term1 = $this->createTerm($this->vocabulary); $term2 = $this->createTerm($this->vocabulary); $term3 = $this->createTerm($this->vocabulary); // Indent the second term under the first one. $edit = array('terms[tid:' . $term2->id() . ':0][term][tid]' => 2, 'terms[tid:' . $term2->id() . ':0][term][parent]' => 1, 'terms[tid:' . $term2->id() . ':0][term][depth]' => 1, 'terms[tid:' . $term2->id() . ':0][weight]' => 1); // Submit the edited form and check for HTML indentation element presence. $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->get('vid') . '/overview', $edit, t('Save')); $this->assertPattern('|<div class="indentation"> </div>|'); // Check explicitly that term 2's parent is term 1. $parents = taxonomy_term_load_parents($term2->id()); $this->assertEqual(key($parents), 1, 'Term 1 is the term 2\'s parent'); // Move the second term back out to the root level. $edit = array('terms[tid:' . $term2->id() . ':0][term][tid]' => 2, 'terms[tid:' . $term2->id() . ':0][term][parent]' => 0, 'terms[tid:' . $term2->id() . ':0][term][depth]' => 0, 'terms[tid:' . $term2->id() . ':0][weight]' => 1); $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->get('vid') . '/overview', $edit, t('Save')); // All terms back at the root level, no identation should be present. $this->assertNoPattern('|<div class="indentation"> </div>|'); // Check explicitly that term 2 has no parents. drupal_static_reset(); $parents = taxonomy_term_load_parents($term2->id()); $this->assertTrue(empty($parents), 'Term 2 has no parents now'); }
/** * Test saving a term with multiple parents through the UI. */ function testTermMultipleParentsInterface() { // Add a new term to the vocabulary so that we can have multiple parents. $parent = $this->createTerm($this->vocabulary); // Add a new term with multiple parents. $edit = array('name[0][value]' => $this->randomMachineName(12), 'description[0][value]' => $this->randomMachineName(100), 'parent[]' => array(0, $parent->id())); // Save the new term. $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add', $edit, t('Save')); // Check that the term was successfully created. $terms = taxonomy_term_load_multiple_by_name($edit['name[0][value]']); $term = reset($terms); $this->assertNotNull($term, 'Term found in database.'); $this->assertEqual($edit['name[0][value]'], $term->getName(), 'Term name was successfully saved.'); $this->assertEqual($edit['description[0][value]'], $term->getDescription(), 'Term description was successfully saved.'); // Check that the parent tid is still there. The other parent (<root>) is // not added by taxonomy_term_load_parents(). $parents = taxonomy_term_load_parents($term->id()); $parent = reset($parents); $this->assertEqual($edit['parent[]'][1], $parent->id(), 'Term parents were successfully saved.'); }
/** * Returns a select box for available parent terms. * * @param int $tid * ID of the term that is being added or edited. * @param string $title * Title for the select box. * * @return array * A select form element. */ protected function forumParentSelect($tid, $title) { // @todo Inject a taxonomy service when one exists. $parents = taxonomy_term_load_parents($tid); if ($parents) { $parent = array_shift($parents); $parent = $parent->id(); } else { $parent = 0; } $vid = $this->config('forum.settings')->get('vocabulary'); // @todo Inject a taxonomy service when one exists. $children = taxonomy_get_tree($vid, $tid, NULL, TRUE); // A term can't be the child of itself, nor of its children. foreach ($children as $child) { $exclude[] = $child->tid; } $exclude[] = $tid; // @todo Inject a taxonomy service when one exists. $tree = taxonomy_get_tree($vid, 0, NULL, TRUE); $options[0] = '<' . $this->t('root') . '>'; if ($tree) { foreach ($tree as $term) { if (!in_array($term->id(), $exclude)) { $options[$term->id()] = str_repeat(' -- ', $term->depth) . $term->getName(); } } } $description = $this->t('Forums may be placed at the top (root) level, or inside another container or forum.'); return array('#type' => 'select', '#title' => $title, '#default_value' => $parent, '#options' => $options, '#description' => $description, '#required' => TRUE); }