本文整理汇总了PHP中Drupal\taxonomy\Entity\Term类的典型用法代码示例。如果您正苦于以下问题:PHP Term类的具体用法?PHP Term怎么用?PHP Term使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Term类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: buildTermListItem
/**
* Builds an individual term item for the term item list depending on the formatter.
*
* @param \Drupal\taxonomy\Entity\Term $term
* @return string
*/
private function buildTermListItem(Term $term, $formatter)
{
if ($formatter === 'linked') {
$link_url = Url::fromRoute('entity.taxonomy_term.canonical', array('taxonomy_term' => $term->id()));
return \Drupal::l($term->label(), $link_url);
}
return HTML::escape($term->label());
}
示例2: testTaxonomyTermMultipleLoad
/**
* Create a vocabulary and some taxonomy terms, ensuring they're loaded
* correctly using entity_load_multiple().
*/
function testTaxonomyTermMultipleLoad()
{
// Create a vocabulary.
$vocabulary = $this->createVocabulary();
// Create five terms in the vocabulary.
$i = 0;
while ($i < 5) {
$i++;
$this->createTerm($vocabulary);
}
// Load the terms from the vocabulary.
$terms = entity_load_multiple_by_properties('taxonomy_term', array('vid' => $vocabulary->id()));
$count = count($terms);
$this->assertEqual($count, 5, format_string('Correct number of terms were loaded. @count terms.', array('@count' => $count)));
// Load the same terms again by tid.
$terms2 = Term::loadMultiple(array_keys($terms));
$this->assertEqual($count, count($terms2), 'Five terms were loaded by tid.');
$this->assertEqual($terms, $terms2, 'Both arrays contain the same terms.');
// Remove one term from the array, then delete it.
$deleted = array_shift($terms2);
$deleted->delete();
$deleted_term = Term::load($deleted->id());
$this->assertFalse($deleted_term);
// Load terms from the vocabulary by vid.
$terms3 = entity_load_multiple_by_properties('taxonomy_term', array('vid' => $vocabulary->id()));
$this->assertEqual(count($terms3), 4, 'Correct number of terms were loaded.');
$this->assertFalse(isset($terms3[$deleted->id()]));
// Create a single term and load it by name.
$term = $this->createTerm($vocabulary);
$loaded_terms = entity_load_multiple_by_properties('taxonomy_term', array('name' => $term->getName()));
$this->assertEqual(count($loaded_terms), 1, 'One term was loaded.');
$loaded_term = reset($loaded_terms);
$this->assertEqual($term->id(), $loaded_term->id(), 'Term loaded by name successfully.');
}
示例3: testTaxonomyTerms
/**
* 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(0)), '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));
// Find each term in the tree.
$storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
$vids = array_unique(array_column($expected_results, 'vid'));
$tree_terms = [];
foreach ($vids as $vid) {
foreach ($storage->loadTree($vid) as $term) {
$tree_terms[$term->tid] = $term;
}
}
foreach ($expected_results as $tid => $values) {
/** @var Term $term */
$term = $terms[$tid];
$this->assertIdentical("term {$tid} of vocabulary {$values['source_vid']}", $term->name->value);
$this->assertIdentical("description of term {$tid} of vocabulary {$values['source_vid']}", $term->description->value);
$this->assertIdentical($values['vid'], $term->vid->target_id);
$this->assertIdentical((string) $values['weight'], $term->weight->value);
if ($values['parent'] === array(0)) {
$this->assertNull($term->parent->target_id);
} else {
$parents = array();
foreach (\Drupal::entityManager()->getStorage('taxonomy_term')->loadParents($tid) as $parent) {
$parents[] = (int) $parent->id();
}
$this->assertIdentical($parents, $values['parent']);
}
$this->assertArrayHasKey($tid, $tree_terms, "Term {$tid} exists in vocabulary tree");
$tree_term = $tree_terms[$tid];
$this->assertEquals($values['parent'], $tree_term->parents, "Term {$tid} has correct parents in vocabulary tree");
}
}
示例4: testTaxonomyTermReferenceItem
/**
* Tests using entity fields of the taxonomy term reference field type.
*/
public function testTaxonomyTermReferenceItem()
{
$tid = $this->term->id();
// Just being able to create the entity like this verifies a lot of code.
$entity = entity_create('entity_test');
$entity->field_test_taxonomy->target_id = $this->term->id();
$entity->name->value = $this->randomMachineName();
$entity->save();
$entity = entity_load('entity_test', $entity->id());
$this->assertTrue($entity->field_test_taxonomy instanceof FieldItemListInterface, 'Field implements interface.');
$this->assertTrue($entity->field_test_taxonomy[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEqual($entity->field_test_taxonomy->target_id, $this->term->id(), 'Field item contains the expected TID.');
$this->assertEqual($entity->field_test_taxonomy->entity->getName(), $this->term->getName(), 'Field item entity contains the expected name.');
$this->assertEqual($entity->field_test_taxonomy->entity->id(), $tid, 'Field item entity contains the expected ID.');
$this->assertEqual($entity->field_test_taxonomy->entity->uuid(), $this->term->uuid(), 'Field item entity contains the expected UUID.');
// Change the name of the term via the reference.
$new_name = $this->randomMachineName();
$entity->field_test_taxonomy->entity->setName($new_name);
$entity->field_test_taxonomy->entity->save();
// Verify it is the correct name.
$term = Term::load($tid);
$this->assertEqual($term->getName(), $new_name, 'The name of the term was changed.');
// Make sure the computed term reflects updates to the term id.
$term2 = entity_create('taxonomy_term', array('name' => $this->randomMachineName(), 'vid' => $this->term->getVocabularyId(), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED));
$term2->save();
$entity->field_test_taxonomy->target_id = $term2->id();
$this->assertEqual($entity->field_test_taxonomy->entity->id(), $term2->id(), 'Field item entity contains the new TID.');
$this->assertEqual($entity->field_test_taxonomy->entity->getName(), $term2->getName(), 'Field item entity contains the new name.');
// Test sample item generation.
$entity = entity_create('entity_test');
$entity->field_test_taxonomy->generateSampleItems();
$this->entityValidateAndSave($entity);
}
示例5: setUp
protected function setUp()
{
parent::setUp();
$this->addDefaultCommentField('node', 'page');
$web_user = $this->drupalCreateUser(array('edit own page content', 'create page content'));
$this->drupalLogin($web_user);
// Add a vocabulary so we can test different view modes.
$vocabulary = Vocabulary::create(['name' => $this->randomMachineName(), 'description' => $this->randomMachineName(), 'vid' => $this->randomMachineName(), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, 'help' => '']);
$vocabulary->save();
$this->vocabulary = $vocabulary;
// Add a term to the vocabulary.
$term = Term::create(['name' => $this->randomMachineName(), 'description' => $this->randomMachineName(), 'vid' => $this->vocabulary->id(), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED]);
$term->save();
$this->term = $term;
// Create an image field.
FieldStorageConfig::create(['field_name' => 'field_image', 'entity_type' => 'node', 'type' => 'image', 'settings' => [], 'cardinality' => FieldStorageConfig::CARDINALITY_UNLIMITED])->save();
$field_config = FieldConfig::create(['field_name' => 'field_image', 'label' => 'Images', 'entity_type' => 'node', 'bundle' => 'page', 'required' => FALSE, 'settings' => []]);
$field_config->save();
// Create a field.
$this->fieldName = Unicode::strtolower($this->randomMachineName());
$handler_settings = array('target_bundles' => array($this->vocabulary->id() => $this->vocabulary->id()), 'auto_create' => TRUE);
$this->createEntityReferenceField('node', 'page', $this->fieldName, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
entity_get_form_display('node', 'page', 'default')->setComponent($this->fieldName, array('type' => 'entity_reference_autocomplete_tags'))->save();
// Show on default display and teaser.
entity_get_display('node', 'page', 'default')->setComponent($this->fieldName, array('type' => 'entity_reference_label'))->save();
entity_get_display('node', 'page', 'teaser')->setComponent($this->fieldName, array('type' => 'entity_reference_label'))->save();
entity_get_form_display('node', 'page', 'default')->setComponent('field_image', array('type' => 'image_image', 'settings' => []))->save();
entity_get_display('node', 'page', 'default')->setComponent('field_image')->save();
}
示例6: testRollback
/**
* Tests rolling back configuration and content entities.
*/
public function testRollback()
{
// We use vocabularies to demonstrate importing and rolling back
// configuration entities.
$vocabulary_data_rows = [['id' => '1', 'name' => 'categories', 'weight' => '2'], ['id' => '2', 'name' => 'tags', 'weight' => '1']];
$ids = ['id' => ['type' => 'integer']];
$config = ['id' => 'vocabularies', 'migration_tags' => ['Import and rollback test'], 'source' => ['plugin' => 'embedded_data', 'data_rows' => $vocabulary_data_rows, 'ids' => $ids], 'process' => ['vid' => 'id', 'name' => 'name', 'weight' => 'weight'], 'destination' => ['plugin' => 'entity:taxonomy_vocabulary']];
$vocabulary_migration = Migration::create($config);
$vocabulary_id_map = $vocabulary_migration->getIdMap();
$this->assertTrue($vocabulary_migration->getDestinationPlugin()->supportsRollback());
// Import and validate vocabulary config entities were created.
$vocabulary_executable = new MigrateExecutable($vocabulary_migration, $this);
$vocabulary_executable->import();
foreach ($vocabulary_data_rows as $row) {
/** @var Vocabulary $vocabulary */
$vocabulary = Vocabulary::load($row['id']);
$this->assertTrue($vocabulary);
$map_row = $vocabulary_id_map->getRowBySource(['id' => $row['id']]);
$this->assertNotNull($map_row['destid1']);
}
// We use taxonomy terms to demonstrate importing and rolling back
// content entities.
$term_data_rows = [['id' => '1', 'vocab' => '1', 'name' => 'music'], ['id' => '2', 'vocab' => '2', 'name' => 'Bach'], ['id' => '3', 'vocab' => '2', 'name' => 'Beethoven']];
$ids = ['id' => ['type' => 'integer']];
$config = ['id' => 'terms', 'migration_tags' => ['Import and rollback test'], 'source' => ['plugin' => 'embedded_data', 'data_rows' => $term_data_rows, 'ids' => $ids], 'process' => ['tid' => 'id', 'vid' => 'vocab', 'name' => 'name'], 'destination' => ['plugin' => 'entity:taxonomy_term'], 'migration_dependencies' => ['required' => ['vocabularies']]];
$term_migration = Migration::create($config);
$term_id_map = $term_migration->getIdMap();
$this->assertTrue($term_migration->getDestinationPlugin()->supportsRollback());
// Import and validate term entities were created.
$term_executable = new MigrateExecutable($term_migration, $this);
$term_executable->import();
foreach ($term_data_rows as $row) {
/** @var Term $term */
$term = Term::load($row['id']);
$this->assertTrue($term);
$map_row = $term_id_map->getRowBySource(['id' => $row['id']]);
$this->assertNotNull($map_row['destid1']);
}
// Rollback and verify the entities are gone.
$term_executable->rollback();
foreach ($term_data_rows as $row) {
$term = Term::load($row['id']);
$this->assertNull($term);
$map_row = $term_id_map->getRowBySource(['id' => $row['id']]);
$this->assertFalse($map_row);
}
$vocabulary_executable->rollback();
foreach ($vocabulary_data_rows as $row) {
$term = Vocabulary::load($row['id']);
$this->assertNull($term);
$map_row = $vocabulary_id_map->getRowBySource(['id' => $row['id']]);
$this->assertFalse($map_row);
}
// Test that simple configuration is not rollbackable.
$term_setting_rows = [['id' => 1, 'override_selector' => '0', 'terms_per_page_admin' => '10']];
$ids = ['id' => ['type' => 'integer']];
$config = ['id' => 'taxonomy_settings', 'migration_tags' => ['Import and rollback test'], 'source' => ['plugin' => 'embedded_data', 'data_rows' => $term_setting_rows, 'ids' => $ids], 'process' => ['override_selector' => 'override_selector', 'terms_per_page_admin' => 'terms_per_page_admin'], 'destination' => ['plugin' => 'config', 'config_name' => 'taxonomy.settings'], 'migration_dependencies' => ['required' => ['vocabularies']]];
$settings_migration = Migration::create($config);
$this->assertFalse($settings_migration->getDestinationPlugin()->supportsRollback());
}
示例7: testStubWithWeightMapping
/**
* Tests creation of stubs when weight is mapped.
*/
public function testStubWithWeightMapping()
{
// Create a vocabulary via migration for the terms to reference.
$vocabulary_data_rows = [['id' => '1', 'name' => 'tags']];
$ids = ['id' => ['type' => 'integer']];
$config = ['id' => 'vocabularies', 'migration_tags' => ['Stub test'], 'source' => ['plugin' => 'embedded_data', 'data_rows' => $vocabulary_data_rows, 'ids' => $ids], 'process' => ['vid' => 'id', 'name' => 'name'], 'destination' => ['plugin' => 'entity:taxonomy_vocabulary']];
$vocabulary_migration = Migration::create($config);
$vocabulary_executable = new MigrateExecutable($vocabulary_migration, $this);
$vocabulary_executable->import();
// We have a term referencing an unmigrated parent, forcing a stub to be
// created.
$term_data_rows = [['id' => '1', 'vocab' => '1', 'name' => 'music', 'parent' => '2']];
$ids = ['id' => ['type' => 'integer']];
$config = ['id' => 'terms', 'migration_tags' => ['Import and rollback test'], 'source' => ['plugin' => 'embedded_data', 'data_rows' => $term_data_rows, 'ids' => $ids], 'process' => ['tid' => 'id', 'vid' => 'vocab', 'name' => 'name', 'weight' => 'weight', 'parent' => ['plugin' => 'migration', 'migration' => 'terms', 'source' => 'parent']], 'destination' => ['plugin' => 'entity:taxonomy_term'], 'migration_dependencies' => ['required' => ['vocabularies']]];
$term_migration = Migration::create($config);
$term_migration->save();
$term_executable = new MigrateExecutable($term_migration, $this);
$term_executable->import();
// Load the referenced term, which should exist as a stub.
/** @var \Drupal\Core\Entity\ContentEntityBase $stub_entity */
$stub_entity = Term::load(2);
$this->assertTrue($stub_entity, 'Stub successfully created');
if ($stub_entity) {
$this->assertIdentical(count($stub_entity->validate()), 0, 'Stub is a valid entity');
}
}
示例8: setUp
protected function setUp()
{
parent::setUp();
$this->installEntitySchema('taxonomy_term');
// We want an entity reference field. It needs a vocabulary, terms, a field
// storage and a field. First, create the vocabulary.
$vocabulary = Vocabulary::create(['vid' => Unicode::strtolower($this->randomMachineName())]);
$vocabulary->save();
// Second, create the field.
entity_test_create_bundle('test_bundle');
$this->fieldName = strtolower($this->randomMachineName());
$handler_settings = array('target_bundles' => array($vocabulary->id() => $vocabulary->id()), 'auto_create' => TRUE);
$this->createEntityReferenceField('entity_test', 'test_bundle', $this->fieldName, NULL, 'taxonomy_term', 'default', $handler_settings);
// Create two terms and also two accounts.
for ($i = 0; $i <= 1; $i++) {
$term = Term::create(['name' => $this->randomMachineName(), 'vid' => $vocabulary->id()]);
$term->save();
$this->terms[] = $term;
$this->accounts[] = $this->createUser();
}
// Create three entity_test entities, the 0th entity will point to the
// 0th account and 0th term, the 1st and 2nd entity will point to the
// 1st account and 1st term.
for ($i = 0; $i <= 2; $i++) {
$entity = EntityTest::create(array('type' => 'test_bundle'));
$entity->name->value = $this->randomMachineName();
$index = $i ? 1 : 0;
$entity->user_id->target_id = $this->accounts[$index]->id();
$entity->{$this->fieldName}->target_id = $this->terms[$index]->id();
$entity->save();
$this->entities[] = $entity;
}
$this->factory = \Drupal::service('entity.query');
}
示例9: createTerm
/**
* Returns a new term with random properties in vocabulary $vid.
*
* @param \Drupal\taxonomy\Entity\Vocabulary $vocabulary
* The vocabulary object.
* @param array $values
* (optional) An array of values to set, keyed by property name. If the
* entity type has bundles, the bundle key has to be specified.
*
* @return \Drupal\taxonomy\Entity\Term
* The new taxonomy term object.
*/
function createTerm(Vocabulary $vocabulary, $values = array())
{
$filter_formats = filter_formats();
$format = array_pop($filter_formats);
$term = Term::create($values + ['name' => $this->randomMachineName(), 'description' => ['value' => $this->randomMachineName(), 'format' => $format->id()], 'vid' => $vocabulary->id(), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED]);
$term->save();
return $term;
}
示例10: testForumUninstallWithField
/**
* Tests if forum module uninstallation properly deletes the field.
*/
public function testForumUninstallWithField()
{
$this->drupalLogin($this->drupalCreateUser(['administer taxonomy', 'administer nodes', 'administer modules', 'delete any forum content', 'administer content types']));
// Ensure that the field exists before uninstallation.
$field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums');
$this->assertNotNull($field_storage, 'The taxonomy_forums field storage exists.');
// Create a taxonomy term.
$term = Term::create(['name' => t('A term'), 'langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId(), 'description' => '', 'parent' => array(0), 'vid' => 'forums', 'forum_container' => 0]);
$term->save();
// Create a forum node.
$node = $this->drupalCreateNode(array('title' => 'A forum post', 'type' => 'forum', 'taxonomy_forums' => array(array('target_id' => $term->id()))));
// Create at least one comment against the forum node.
$comment = Comment::create(array('entity_id' => $node->nid->value, 'entity_type' => 'node', 'field_name' => 'comment_forum', 'pid' => 0, 'uid' => 0, 'status' => CommentInterface::PUBLISHED, 'subject' => $this->randomMachineName(), 'hostname' => '127.0.0.1'));
$comment->save();
// Attempt to uninstall forum.
$this->drupalGet('admin/modules/uninstall');
// Assert forum is required.
$this->assertNoFieldByName('uninstall[forum]');
$this->assertText('To uninstall Forum, first delete all Forum content');
// Delete the node.
$this->drupalPostForm('node/' . $node->id() . '/delete', array(), t('Delete'));
// Attempt to uninstall forum.
$this->drupalGet('admin/modules/uninstall');
// Assert forum is still required.
$this->assertNoFieldByName('uninstall[forum]');
$this->assertText('To uninstall Forum, first delete all Forums terms');
// Delete any forum terms.
$vid = $this->config('forum.settings')->get('vocabulary');
$terms = entity_load_multiple_by_properties('taxonomy_term', ['vid' => $vid]);
foreach ($terms as $term) {
$term->delete();
}
// Ensure that the forum node type can not be deleted.
$this->drupalGet('admin/structure/types/manage/forum');
$this->assertNoLink(t('Delete'));
// Now attempt to uninstall forum.
$this->drupalGet('admin/modules/uninstall');
// Assert forum is no longer required.
$this->assertFieldByName('uninstall[forum]');
$this->drupalPostForm('admin/modules/uninstall', array('uninstall[forum]' => 1), t('Uninstall'));
$this->drupalPostForm(NULL, [], t('Uninstall'));
// Check that the field is now deleted.
$field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums');
$this->assertNull($field_storage, 'The taxonomy_forums field storage has been deleted.');
// Check that a node type with a machine name of forum can be created after
// uninstalling the forum module and the node type is not locked.
$edit = array('name' => 'Forum', 'title_label' => 'title for forum', 'type' => 'forum');
$this->drupalPostForm('admin/structure/types/add', $edit, t('Save content type'));
$this->assertTrue((bool) NodeType::load('forum'), 'Node type with machine forum created.');
$this->drupalGet('admin/structure/types/manage/forum');
$this->clickLink(t('Delete'));
$this->drupalPostForm(NULL, array(), t('Delete'));
$this->assertResponse(200);
$this->assertFalse((bool) NodeType::load('forum'), 'Node type with machine forum deleted.');
// Double check everything by reinstalling the forum module again.
$this->drupalPostForm('admin/modules', ['modules[Core][forum][enable]' => 1], 'Install');
$this->assertText('Module Forum has been enabled.');
}
示例11: testContentEntityReferenceItem
/**
* Tests the entity reference field type for referencing content entities.
*/
public function testContentEntityReferenceItem()
{
$tid = $this->term->id();
// Just being able to create the entity like this verifies a lot of code.
$entity = entity_create('entity_test');
$entity->field_test_taxonomy_term->target_id = $tid;
$entity->name->value = $this->randomMachineName();
$entity->save();
$entity = entity_load('entity_test', $entity->id());
$this->assertTrue($entity->field_test_taxonomy_term instanceof FieldItemListInterface, 'Field implements interface.');
$this->assertTrue($entity->field_test_taxonomy_term[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEqual($entity->field_test_taxonomy_term->target_id, $tid);
$this->assertEqual($entity->field_test_taxonomy_term->entity->getName(), $this->term->getName());
$this->assertEqual($entity->field_test_taxonomy_term->entity->id(), $tid);
$this->assertEqual($entity->field_test_taxonomy_term->entity->uuid(), $this->term->uuid());
// Change the name of the term via the reference.
$new_name = $this->randomMachineName();
$entity->field_test_taxonomy_term->entity->setName($new_name);
$entity->field_test_taxonomy_term->entity->save();
// Verify it is the correct name.
$term = Term::load($tid);
$this->assertEqual($term->getName(), $new_name);
// Make sure the computed term reflects updates to the term id.
$term2 = entity_create('taxonomy_term', array('name' => $this->randomMachineName(), 'vid' => $this->term->bundle(), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED));
$term2->save();
// Test all the possible ways of assigning a value.
$entity->field_test_taxonomy_term->target_id = $term->id();
$this->assertEqual($entity->field_test_taxonomy_term->entity->id(), $term->id());
$this->assertEqual($entity->field_test_taxonomy_term->entity->getName(), $term->getName());
$entity->field_test_taxonomy_term = [['target_id' => $term2->id()]];
$this->assertEqual($entity->field_test_taxonomy_term->entity->id(), $term2->id());
$this->assertEqual($entity->field_test_taxonomy_term->entity->getName(), $term2->getName());
// Test value assignment via the computed 'entity' property.
$entity->field_test_taxonomy_term->entity = $term;
$this->assertEqual($entity->field_test_taxonomy_term->target_id, $term->id());
$this->assertEqual($entity->field_test_taxonomy_term->entity->getName(), $term->getName());
$entity->field_test_taxonomy_term = [['entity' => $term2]];
$this->assertEqual($entity->field_test_taxonomy_term->target_id, $term2->id());
$this->assertEqual($entity->field_test_taxonomy_term->entity->getName(), $term2->getName());
// Test assigning an invalid item throws an exception.
try {
$entity->field_test_taxonomy_term = ['target_id' => 'invalid', 'entity' => $term2];
$this->fail('Assigning an invalid item throws an exception.');
} catch (\InvalidArgumentException $e) {
$this->pass('Assigning an invalid item throws an exception.');
}
// Delete terms so we have nothing to reference and try again
$term->delete();
$term2->delete();
$entity = entity_create('entity_test', array('name' => $this->randomMachineName()));
$entity->save();
// Test the generateSampleValue() method.
$entity = entity_create('entity_test');
$entity->field_test_taxonomy_term->generateSampleItems();
$entity->field_test_taxonomy_vocabulary->generateSampleItems();
$this->entityValidateAndSave($entity);
}
示例12: titleQuery
public function titleQuery()
{
$titles = array();
$terms = Term::loadMultiple($this->value);
foreach ($terms as $term) {
$titles[] = \Drupal::entityManager()->getTranslationFromContext($term)->label();
}
return $titles;
}
示例13: createEntity
/**
* {@inheritdoc}
*/
protected function createEntity()
{
// Create a "Camelids" vocabulary.
$vocabulary = Vocabulary::create(['name' => 'Camelids', 'vid' => 'camelids']);
$vocabulary->save();
// Create a "Llama" taxonomy term.
$term = Term::create(['name' => 'Llama', 'vid' => $vocabulary->id()]);
$term->save();
return $term;
}
示例14: assertEntity
/**
* Validate a migrated term contains the expected values.
*
* @param $id
* Entity ID to load and check.
* @param $expected_label
* The label the migrated entity should have.
* @param $expected_vid
* The parent vocabulary the migrated entity should have.
* @param string $expected_description
* The description the migrated entity should have.
* @param int $expected_weight
* The weight the migrated entity should have.
* @param array $expected_parents
* The parent terms the migrated entity should have.
*/
protected function assertEntity($id, $expected_label, $expected_vid, $expected_description = '', $expected_weight = 0, $expected_parents = [])
{
/** @var \Drupal\taxonomy\TermInterface $entity */
$entity = Term::load($id);
$this->assertTrue($entity instanceof TermInterface);
$this->assertIdentical($expected_label, $entity->label());
$this->assertIdentical($expected_vid, $entity->getVocabularyId());
$this->assertEqual($expected_description, $entity->getDescription());
$this->assertEqual($expected_weight, $entity->getWeight());
$this->assertIdentical($expected_parents, $this->getParentIDs($id));
}
示例15: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp(FALSE);
// Setup vocabulary and terms so the initial import is valid.
Vocabulary::create(['vid' => 'tags', 'name' => 'Tags'])->save();
// This will get a term ID of 3.
$term = Term::create(['vid' => 'tags', 'name' => 'muh']);
$term->save();
// This will get a term ID of 4.
$this->terms[$term->id()] = $term;
$term = Term::create(['vid' => 'tags', 'name' => 'muh']);
$term->save();
$this->terms[$term->id()] = $term;
ViewTestData::createTestViews(get_class($this), array('taxonomy_test_views'));
}