本文整理汇总了PHP中Drupal\taxonomy\Entity\Term::create方法的典型用法代码示例。如果您正苦于以下问题:PHP Term::create方法的具体用法?PHP Term::create怎么用?PHP Term::create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\taxonomy\Entity\Term
的用法示例。
在下文中一共展示了Term::create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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();
}
示例2: 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');
}
示例3: 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;
}
示例4: 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.');
}
示例5: 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;
}
示例6: 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'));
}
示例7: testEntityRow
/**
* Tests the entity row handler.
*/
public function testEntityRow()
{
$vocab = Vocabulary::create(['name' => $this->randomMachineName(), 'vid' => strtolower($this->randomMachineName())]);
$vocab->save();
$term = Term::create(['name' => $this->randomMachineName(), 'vid' => $vocab->id()]);
$term->save();
$view = Views::getView('test_entity_row');
$build = $view->preview();
$this->render($build);
$this->assertText($term->getName(), 'The rendered entity appears as row in the view.');
// Tests the available view mode options.
$form = array();
$form_state = new FormState();
$form_state->set('view', $view->storage);
$view->rowPlugin->buildOptionsForm($form, $form_state);
$this->assertTrue(isset($form['view_mode']['#options']['default']), 'Ensure that the default view mode is available');
}
示例8: testTerm
/**
* Tests the normalization of terms.
*/
public function testTerm()
{
$vocabulary = Vocabulary::create(['vid' => 'example_vocabulary']);
$vocabulary->save();
$account = User::create(['name' => $this->randomMachineName()]);
$account->save();
// @todo Until https://www.drupal.org/node/2327935 is fixed, if no parent is
// set, the test fails because target_id => 0 is reserialized to NULL.
$term_parent = Term::create(['name' => $this->randomMachineName(), 'vid' => $vocabulary->id()]);
$term_parent->save();
$term = Term::create(['name' => $this->randomMachineName(), 'vid' => $vocabulary->id(), 'description' => array('value' => $this->randomMachineName(), 'format' => $this->randomMachineName()), 'parent' => $term_parent->id()]);
$term->save();
$original_values = $term->toArray();
$normalized = $this->serializer->normalize($term, $this->format, ['account' => $account]);
/** @var \Drupal\taxonomy\TermInterface $denormalized_term */
$denormalized_term = $this->serializer->denormalize($normalized, 'Drupal\\taxonomy\\Entity\\Term', $this->format, ['account' => $account]);
$this->assertEqual($original_values, $denormalized_term->toArray(), 'Term values are restored after normalizing and denormalizing.');
}
示例9: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
Vocabulary::create(['vid' => 'tags', 'name' => 'Tags'])->save();
// Setup a hierarchy which looks like this:
// term 0.0
// term 1.0
// - term 1.1
// term 2.0
// - term 2.1
// - term 2.2
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j <= $i; $j++) {
$this->terms[$i][$j] = $term = Term::create(['vid' => 'tags', 'name' => "Term {$i}.{$j}", 'parent' => isset($terms[$i][0]) ? $terms[$i][0]->id() : 0]);
$term->save();
}
}
ViewTestData::createTestViews(get_class($this), array('taxonomy_test_views'));
}
示例10: testUrlAlter
/**
* Test that URL altering works and that it occurs in the correct order.
*/
function testUrlAlter()
{
// Ensure that the url_alias table exists after Drupal installation.
$this->assertTrue(Database::getConnection()->schema()->tableExists('url_alias'), 'The url_alias table exists after Drupal installation.');
// User names can have quotes and plus signs so we should ensure that URL
// altering works with this.
$account = $this->drupalCreateUser(array('administer url aliases'), "a'foo+bar");
$this->drupalLogin($account);
$uid = $account->id();
$name = $account->getUsername();
// Test a single altered path.
$this->drupalGet("user/{$name}");
$this->assertResponse('200', 'The user/username path gets resolved correctly');
$this->assertUrlOutboundAlter("/user/{$uid}", "/user/{$name}");
// Test that a path always uses its alias.
$path = array('source' => "/user/{$uid}/test1", 'alias' => '/alias/test1');
$this->container->get('path.alias_storage')->save($path['source'], $path['alias']);
$this->rebuildContainer();
$this->assertUrlInboundAlter('/alias/test1', "/user/{$uid}/test1");
$this->assertUrlOutboundAlter("/user/{$uid}/test1", '/alias/test1');
// Test adding an alias via the UI.
$edit = array('source' => "/user/{$uid}/edit", 'alias' => '/alias/test2');
$this->drupalPostForm('admin/config/search/path/add', $edit, t('Save'));
$this->assertText(t('The alias has been saved.'));
$this->drupalGet('alias/test2');
$this->assertResponse('200', 'The path alias gets resolved correctly');
$this->assertUrlOutboundAlter("/user/{$uid}/edit", '/alias/test2');
// Test a non-existent user is not altered.
$uid++;
$this->assertUrlOutboundAlter("/user/{$uid}", "/user/{$uid}");
// Test that 'forum' is altered to 'community' correctly, both at the root
// level and for a specific existing forum.
$this->drupalGet('community');
$this->assertText('General discussion', 'The community path gets resolved correctly');
$this->assertUrlOutboundAlter('/forum', '/community');
$forum_vid = $this->config('forum.settings')->get('vocabulary');
$term_name = $this->randomMachineName();
$term = Term::create(['name' => $term_name, 'vid' => $forum_vid]);
$term->save();
$this->drupalGet("community/" . $term->id());
$this->assertText($term_name, 'The community/{tid} path gets resolved correctly');
$this->assertUrlOutboundAlter("/forum/" . $term->id(), "/community/" . $term->id());
}
示例11: 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);
}
示例12: testTermFields
/**
* Check access for taxonomy fields.
*/
public function testTermFields()
{
$vocab = Vocabulary::create(['vid' => 'random', 'name' => 'Randomness']);
$vocab->save();
$term1 = Term::create(['name' => 'Semi random', 'vid' => $vocab->id()]);
$term1->save();
$term2 = Term::create(['name' => 'Majorly random', 'vid' => $vocab->id()]);
$term2->save();
$term3 = Term::create(['name' => 'Not really random', 'vid' => $vocab->id()]);
$term3->save();
$this->assertFieldAccess('taxonomy_term', 'name', 'Majorly random');
$this->assertFieldAccess('taxonomy_term', 'name', 'Semi random');
$this->assertFieldAccess('taxonomy_term', 'name', 'Not really random');
$this->assertFieldAccess('taxonomy_term', 'tid', $term1->id());
$this->assertFieldAccess('taxonomy_term', 'tid', $term2->id());
$this->assertFieldAccess('taxonomy_term', 'tid', $term3->id());
$this->assertFieldAccess('taxonomy_term', 'uuid', $term1->uuid());
$this->assertFieldAccess('taxonomy_term', 'uuid', $term2->uuid());
$this->assertFieldAccess('taxonomy_term', 'uuid', $term3->uuid());
}
示例13: testValidation
/**
* Tests the forum validation constraints.
*/
public function testValidation()
{
// Add a forum.
$forum = Term::create(['name' => 'forum 1', 'vid' => 'forums', 'forum_container' => 0]);
// Add a container.
$container = Term::create(['name' => 'container 1', 'vid' => 'forums', 'forum_container' => 1]);
// Add a forum post.
$forum_post = Node::create(['type' => 'forum', 'title' => 'Do these pants make my butt look big?']);
$violations = $forum_post->validate();
$this->assertEqual(count($violations), 1);
$this->assertEqual($violations[0]->getMessage(), 'This value should not be null.');
// Add the forum term.
$forum_post->set('taxonomy_forums', $forum);
$violations = $forum_post->validate();
$this->assertEqual(count($violations), 0);
// Try to use a container.
$forum_post->set('taxonomy_forums', $container);
$violations = $forum_post->validate();
$this->assertEqual(count($violations), 1);
$this->assertEqual($violations[0]->getMessage(), t('The item %forum is a forum container, not a forum. Select one of the forums below instead.', ['%forum' => $container->label()]));
}
示例14: testTitleOptionsFromTaxonomy
public function testTitleOptionsFromTaxonomy()
{
$field = $this->createNameField('field_name_test', 'entity_test', 'entity_test');
$vocabulary = Vocabulary::create(array('vid' => 'title_options', 'name' => 'Title options'));
$vocabulary->save();
foreach (array('foo', 'bar', 'baz') as $name) {
$term = Term::create(array('name' => $name, 'vid' => $vocabulary->id()));
$term->save();
}
/**
* @var \Drupal\field\Entity\FieldStorageConfig $field_storage
*/
$field_storage = $field->getFieldStorageDefinition();
$settings = $field_storage->getSettings();
$settings['title_options'] = array('-- --', '[vocabulary:title_options]');
$settings['sort_options']['title'] = TRUE;
$field_storage->set('settings', $settings);
$field_storage->save();
$expected = array('' => '--', 'bar' => 'bar', 'baz' => 'baz', 'foo' => 'foo');
$this->assertEqual($expected, $this->optionsProvider->getOptions($field, 'title'));
}
示例15: setUp
protected function setUp()
{
parent::setUp();
// Install all available non-testing themes.
$listing = new ExtensionDiscovery(\Drupal::root());
$this->themes = $listing->scan('theme', FALSE);
\Drupal::service('theme_handler')->install(array_keys($this->themes));
// Create a test user.
$this->user = $this->drupalCreateUser(array('access content', 'access user profiles'));
$this->user->name = $this->xssLabel;
$this->user->save();
$this->drupalLogin($this->user);
// Create a test term.
$this->term = Term::create(['name' => $this->xssLabel, 'vid' => 1]);
$this->term->save();
// Add a comment field.
$this->addDefaultCommentField('node', 'article', 'comment', CommentItemInterface::OPEN);
// Create a test node tagged with the test term.
$this->node = $this->drupalCreateNode(array('title' => $this->xssLabel, 'type' => 'article', 'promote' => NODE_PROMOTED, 'field_tags' => array(array('target_id' => $this->term->id()))));
// Create a test comment on the test node.
$this->comment = Comment::create(array('entity_id' => $this->node->id(), 'entity_type' => 'node', 'field_name' => 'comment', 'status' => CommentInterface::PUBLISHED, 'subject' => $this->xssLabel, 'comment_body' => array($this->randomMachineName())));
$this->comment->save();
}