本文整理汇总了PHP中Drupal\taxonomy\Entity\Term::load方法的典型用法代码示例。如果您正苦于以下问题:PHP Term::load方法的具体用法?PHP Term::load怎么用?PHP Term::load使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\taxonomy\Entity\Term
的用法示例。
在下文中一共展示了Term::load方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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);
}
示例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: 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());
}
示例4: 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');
}
}
示例5: getTempStoreCarburant
/**
* @return Drupal\taxonomy\Entity\Term|null
*/
private static function getTempStoreCarburant()
{
$temp_store = \Drupal::getContainer()->get('user.private_tempstore')->get();
$id = $temp_store->get('tip_carburant');
if ($id) {
return Term::load($id);
}
}
示例6: 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);
}
示例7: 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));
}
示例8: testMixedBundles
/**
* Tests setting bundles both in process and destination.
*/
public function testMixedBundles()
{
$term_data_rows = [['id' => 1, 'vocab' => 'categories', 'name' => 'Category 1'], ['id' => 2, 'name' => 'Tag 1']];
$ids = ['id' => ['type' => 'integer']];
$definition = ['id' => 'terms', 'migration_tags' => ['Bundle test'], 'source' => ['plugin' => 'embedded_data', 'data_rows' => $term_data_rows, 'ids' => $ids], 'process' => ['tid' => 'id', 'vid' => 'vocab', 'name' => 'name'], 'destination' => ['plugin' => 'entity:taxonomy_term', 'default_bundle' => 'tags'], 'migration_dependencies' => []];
$term_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
// Import and validate the term entities were created with the correct bundle.
$term_executable = new MigrateExecutable($term_migration, $this);
$term_executable->import();
/** @var Term $term */
$term = Term::load(1);
$this->assertEquals($term->bundle(), 'categories');
$term = Term::load(2);
$this->assertEquals($term->bundle(), 'tags');
}
示例9: build
/**
* {@inheritdoc}
*/
public function build(FacetInterface $facet, array $results)
{
$language_interface = \Drupal::languageManager()->getCurrentLanguage();
/** @var \Drupal\facets\Result\ResultInterface $result */
foreach ($results as &$result) {
/** @var \Drupal\taxonomy\Entity\Term $term */
$term = Term::load($result->getRawValue());
if ($term->hasTranslation($language_interface->getId())) {
$term_trans = $term->getTranslation($language_interface->getId());
$result->setDisplayValue($term_trans->getName());
} else {
$result->setDisplayValue($term->getName());
}
}
return $results;
}
示例10: title
/**
* {@inheritdoc}
*/
public function title()
{
if (!empty($this->argument)) {
$this->fillValue();
$terms = array();
foreach ($this->value as $tid) {
$taxonomy_term = Term::load($tid);
if ($taxonomy_term) {
$terms[] = Html::escape($taxonomy_term->label());
}
}
return $terms ? implode(', ', $terms) : Html::escape($this->argument);
} else {
return Html::escape($this->argument);
}
}
示例11: title
/**
* {@inheritdoc}
*/
public function title()
{
if (!empty($this->argument)) {
$this->fillValue();
$terms = array();
foreach ($this->value as $tid) {
$taxonomy_term = Term::load($tid);
if ($taxonomy_term) {
$terms[] = SafeMarkup::checkPlain($taxonomy_term->label());
}
}
return $terms ? implode(', ', $terms) : SafeMarkup::checkPlain($this->argument);
} else {
return SafeMarkup::checkPlain($this->argument);
}
}
示例12: build
/**
* {@inheritdoc}
*/
public function build(RouteMatchInterface $route_match)
{
$breadcrumb = new Breadcrumb();
$node = $route_match->getParameter('node');
// Homepage link.
$links[] = Link::createFromRoute($this->t('Home'), '<front>');
// First assigned tag.
if (!empty($node->field_tags[0]->target_id)) {
$tid = $node->field_tags[0]->target_id;
$term_name = Term::load($tid)->get('name')->value;
$links[] = Link::createFromRoute($term_name, 'entity.taxonomy_term.canonical', ['taxonomy_term' => $tid]);
}
// The node itself.
$links[] = Link::fromTextAndUrl($node->getTitle(), Url::fromRoute('<current>'));
$breadcrumb->addCacheContexts(['route']);
return $breadcrumb->setLinks($links);
}
示例13: migrateNcdTaxonomy
public function migrateNcdTaxonomy($import)
{
$query = \Drupal::entityQuery('taxonomy_term')->condition('field_old_id', $import->tid);
$newId = $query->execute();
if (!empty($newId)) {
//load entity for update
$targetEntity = Term::load(array_pop($newId));
} else {
//create entity and save
$targetEntity = Term::create(array('vid' => $import->vocabulary_machine_name, 'name' => $import->name));
}
$targetEntity->name = $import->name;
$targetEntity->description = $import->description ?? '';
$targetEntity->field_menu_image = $this->processFile($import->field_menu_image);
$targetEntity->field_category_header = $import->field_category_header ?? '';
$targetEntity->field_old_id = $import->tid;
$targetEntity->save();
}
示例14: testMultipleParentDelete
/**
* Deleting a parent of a term with multiple parents does not delete the term.
*/
public function testMultipleParentDelete()
{
$vocabulary = $this->createVocabulary();
$parent_term1 = $this->createTerm($vocabulary);
$parent_term2 = $this->createTerm($vocabulary);
$child_term = $this->createTerm($vocabulary);
$child_term->parent = array($parent_term1->id(), $parent_term2->id());
$child_term->save();
$child_term_id = $child_term->id();
$parent_term1->delete();
$term_storage = $this->container->get('entity.manager')->getStorage('taxonomy_term');
$term_storage->resetCache(array($child_term_id));
$child_term = Term::load($child_term_id);
$this->assertTrue(!empty($child_term), 'Child term is not deleted if only one of its parents is removed.');
$parent_term2->delete();
$term_storage->resetCache(array($child_term_id));
$child_term = Term::load($child_term_id);
$this->assertTrue(empty($child_term), 'Child term is deleted if all of its parents are removed.');
}
示例15: testExportContent
/**
* Tests exportContent().
*/
public function testExportContent()
{
\Drupal::service('module_installer')->install(['taxonomy', 'default_content']);
\Drupal::service('router.builder')->rebuild();
$this->defaultContentManager = \Drupal::service('default_content.manager');
$vocabulary = Vocabulary::create(['vid' => 'test']);
$vocabulary->save();
$term = Term::create(['vid' => $vocabulary->id(), 'name' => 'test_name']);
$term->save();
$term = Term::load($term->id());
/** @var \Symfony\Component\Serializer\Serializer $serializer */
$serializer = \Drupal::service('serializer');
\Drupal::service('rest.link_manager')->setLinkDomain(DefaultContentManager::LINK_DOMAIN);
$expected = $serializer->serialize($term, 'hal_json', ['json_encode_options' => JSON_PRETTY_PRINT]);
$exported = $this->defaultContentManager->exportContent('taxonomy_term', $term->id());
$exported_decoded = json_decode($exported);
// Ensure the proper UUID is part of it.
$this->assertEqual($exported_decoded->uuid[0]->value, $term->uuid());
$this->assertEqual($exported, $expected);
}