本文整理汇总了PHP中Drupal\taxonomy\VocabularyInterface类的典型用法代码示例。如果您正苦于以下问题:PHP VocabularyInterface类的具体用法?PHP VocabularyInterface怎么用?PHP VocabularyInterface使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了VocabularyInterface类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: addTerm
function addTerm(VocabularyInterface $vocabulary, array $term = array())
{
$term += array('name' => Unicode::strtolower($this->randomMachineName(5)), 'vid' => $vocabulary->id());
$term = entity_create('taxonomy_term', $term);
$term->save();
return $term;
}
示例2: buildForm
/**
* Form constructor.
*
* Display a tree of all the terms in a vocabulary, with options to edit
* each one. The form implements the Taxonomy Manager intefrace.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param VocabularyInterface $taxonomy_vocabulary
* The vocabulary being with worked with
*
* @return array
* The form structure.
*/
public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL)
{
$form['voc'] = array('#type' => 'value', "#value" => $taxonomy_vocabulary);
$form['#attached']['library'][] = 'taxonomy_manager/form';
if (TaxonomyManagerHelper::_taxonomy_manager_voc_is_empty($taxonomy_vocabulary->id())) {
$form['text'] = array('#markup' => $this->t('No terms available'));
$form[] = \Drupal::formBuilder()->getForm('Drupal\\taxonomy_manager\\Form\\AddTermsToVocabularyForm', $taxonomy_vocabulary);
return $form;
}
$form['toolbar'] = array('#type' => 'fieldset', '#title' => $this->t('Toolbar'));
$form['toolbar']['add'] = array('#type' => 'submit', '#name' => 'add', '#value' => $this->t('Add'), '#ajax' => array('callback' => '::addFormCallback'));
$form['toolbar']['delete'] = array('#type' => 'submit', '#name' => 'delete', '#value' => $this->t('Delete'), '#ajax' => array('callback' => '::deleteFormCallback'));
$form['toolbar']['move'] = array('#type' => 'submit', '#name' => 'move', '#value' => $this->t('Move'), '#ajax' => array('callback' => '::moveFormCallback'));
$form['toolbar']['export'] = array('#type' => 'submit', '#name' => 'export', '#value' => $this->t('Export'), '#ajax' => array('callback' => '::exportFormCallback'));
/* Taxonomy manager. */
$form['taxonomy']['#tree'] = TRUE;
$form['taxonomy']['manager'] = array('#type' => 'fieldset', '#title' => HTML::escape($taxonomy_vocabulary->label()), '#tree' => TRUE);
$form['taxonomy']['manager']['top'] = array('#markup' => '', '#prefix' => '<div class="taxonomy-manager-tree-top">', '#suffix' => '</div>');
/*$grippie_image = array(
'#theme' => 'image',
'#uri' => drupal_get_path('module', 'taxonomy_manager') . "/images/grippie.png",
'#alt' => $this->t("Resize tree"),
'#title' => $this->t("Resize tree"),
'#attributes' => array('class' => array('div-grippie')),
);
$form['taxonomy']['manager']['top']['size'] = array(
'#markup' =>
'<div class="taxonomy-manager-tree-size">'
. \Drupal::service('renderer')->render($grippie_image, true)
. '</div>'
);*/
$form['taxonomy']['manager']['tree'] = array('#type' => 'taxonomy_manager_tree', '#vocabulary' => $taxonomy_vocabulary->id(), '#pager_size' => \Drupal::config('taxonomy_manager.settings')->get('taxonomy_manager_pager_tree_page_size'));
$form['taxonomy']['manager']['pager'] = array('#type' => 'pager');
// Add placeholder for term data form, the load-term-data field has AJAX
// events attached and will trigger the load of the term data form. The
// field is hidden via CSS and the value gets set in termData.js.
$form['term-data']['#prefix'] = '<div id="taxonomy-term-data-form">';
$form['term-data']['#suffix'] = '</div>';
$form['load-term-data'] = array('#type' => 'textfield', '#ajax' => array('callback' => '::termDataCallback', 'event' => 'change'));
return $form;
}
示例3: testTaxonomyEfq
/**
* Tests that a basic taxonomy entity query works.
*/
function testTaxonomyEfq()
{
$terms = array();
for ($i = 0; $i < 5; $i++) {
$term = $this->createTerm($this->vocabulary);
$terms[$term->id()] = $term;
}
$result = \Drupal::entityQuery('taxonomy_term')->execute();
sort($result);
$this->assertEqual(array_keys($terms), $result, 'Taxonomy terms were retrieved by entity query.');
$tid = reset($result);
$ids = (object) array('entity_type' => 'taxonomy_term', 'entity_id' => $tid, 'bundle' => $this->vocabulary->id());
$term = _field_create_entity_from_ids($ids);
$this->assertEqual($term->id(), $tid, 'Taxonomy term can be created based on the IDs.');
// Create a second vocabulary and five more terms.
$vocabulary2 = $this->createVocabulary();
$terms2 = array();
for ($i = 0; $i < 5; $i++) {
$term = $this->createTerm($vocabulary2);
$terms2[$term->id()] = $term;
}
$result = \Drupal::entityQuery('taxonomy_term')->condition('vid', $vocabulary2->id())->execute();
sort($result);
$this->assertEqual(array_keys($terms2), $result, format_string('Taxonomy terms from the %name vocabulary were retrieved by entity query.', array('%name' => $vocabulary2->label())));
$tid = reset($result);
$ids = (object) array('entity_type' => 'taxonomy_term', 'entity_id' => $tid, 'bundle' => $vocabulary2->id());
$term = _field_create_entity_from_ids($ids);
$this->assertEqual($term->id(), $tid, 'Taxonomy term can be created based on the IDs.');
}
示例4: testDevelGenerate
/**
* Tests generate commands
*/
public function testDevelGenerate()
{
// Creating users.
$edit = array('num' => 4);
$this->drupalPostForm('admin/config/development/generate/user', $edit, t('Generate'));
$this->assertText(t('4 users created.'));
$this->assertText(t('Generate process complete.'));
// Creating content.
// First we create a node in order to test the Delete content checkbox.
$this->drupalCreateNode(array('type' => 'article'));
$edit = array('num' => 4, 'kill' => TRUE, 'node_types[article]' => TRUE, 'time_range' => 604800, 'max_comments' => 3, 'title_length' => 4);
$this->drupalPostForm('admin/config/development/generate/content', $edit, t('Generate'));
$this->assertText(t('Deleted 1 nodes.'));
$this->assertText(t('Finished creating 4 nodes'));
$this->assertText(t('Generate process complete.'));
// Creating terms.
$edit = array('vids[]' => $this->vocabulary->id(), 'num' => 5, 'title_length' => 12);
$this->drupalPostForm('admin/config/development/generate/term', $edit, t('Generate'));
$this->assertText(t('Created the following new terms: '));
$this->assertText(t('Generate process complete.'));
// Creating vocabularies.
$edit = array('num' => 5, 'title_length' => 12, 'kill' => TRUE);
$this->drupalPostForm('admin/config/development/generate/vocabs', $edit, t('Generate'));
$this->assertText(t('Created the following new vocabularies: '));
$this->assertText(t('Generate process complete.'));
// Creating menus.
$edit = array('num_menus' => 5, 'num_links' => 7, 'title_length' => 12, 'link_types[node]' => 1, 'link_types[front]' => 1, 'link_types[external]' => 1, 'max_depth' => 4, 'max_width' => 6, 'kill' => 1);
$this->drupalPostForm('admin/config/development/generate/menu', $edit, t('Generate'));
$this->assertText(t('Created the following new menus: '));
$this->assertText(t('Created 7 new menu links'));
$this->assertText(t('Generate process complete.'));
}
示例5: setUp
protected function setUp()
{
parent::setUp();
$this->vocabulary = $this->createVocabulary();
// RDF mapping - term bundle.
rdf_get_mapping('taxonomy_term', $this->vocabulary->id())->setBundleMapping(array('types' => array('skos:Concept')))->setFieldMapping('name', array('properties' => array('rdfs:label', 'skos:prefLabel')))->save();
}
示例6: testTaxonomyImageAccess
public function testTaxonomyImageAccess()
{
$user = $this->drupalCreateUser(array('administer site configuration', 'administer taxonomy', 'access user profiles'));
$this->drupalLogin($user);
// Create a term and upload the image.
$files = $this->drupalGetTestFiles('image');
$image = array_pop($files);
$edit['name[0][value]'] = $this->randomMachineName();
$edit['files[field_test_0]'] = drupal_realpath($image->uri);
$this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add', $edit, t('Save'));
$this->drupalPostForm(NULL, ['field_test[0][alt]' => $this->randomMachineName()], t('Save'));
$terms = entity_load_multiple_by_properties('taxonomy_term', array('name' => $edit['name[0][value]']));
$term = reset($terms);
$this->assertText(t('Created new term @name.', array('@name' => $term->getName())));
// Create a user that should have access to the file and one that doesn't.
$access_user = $this->drupalCreateUser(array('access content'));
$no_access_user = $this->drupalCreateUser();
$image = File::load($term->field_test->target_id);
$this->drupalLogin($access_user);
$this->drupalGet(file_create_url($image->getFileUri()));
$this->assertResponse(200, 'Private image on term is accessible with right permission');
$this->drupalLogin($no_access_user);
$this->drupalGet(file_create_url($image->getFileUri()));
$this->assertResponse(403, 'Private image on term not accessible without right permission');
}
示例7: testTermIndentation
/**
* Tests term indentation.
*/
function testTermIndentation()
{
// Create three taxonomy terms.
$term1 = $this->createTerm($this->vocabulary);
$term2 = $this->createTerm($this->vocabulary);
$term3 = $this->createTerm($this->vocabulary);
// Get the taxonomy storage.
$taxonomy_storage = $this->container->get('entity.manager')->getStorage('taxonomy_term');
// 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="js-indentation indentation"> </div>|');
// Check explicitly that term 2's parent is term 1.
$parents = $taxonomy_storage->loadParents($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 indentation should be present.
$this->assertNoPattern('|<div class="js-indentation indentation"> </div>|');
// Check explicitly that term 2 has no parents.
\Drupal::entityManager()->getStorage('taxonomy_term')->resetCache();
$parents = $taxonomy_storage->loadParents($term2->id());
$this->assertTrue(empty($parents), 'Term 2 has no parents now');
}
示例8: setUp
protected function setUp()
{
parent::setUp();
$web_user = $this->drupalCreateUser(array('bypass node access', 'administer taxonomy'));
$this->drupalLogin($web_user);
$this->vocabulary = $this->createVocabulary();
// Create the field.
$this->fieldName = 'field_taxonomy_test';
$this->createTaxonomyTermReferenceField($this->fieldName, $this->vocabulary);
// Set the RDF mapping for the new field.
rdf_get_mapping('node', 'article')->setFieldMapping($this->fieldName, array('properties' => array('dc:subject'), 'mapping_type' => 'rel'))->save();
rdf_get_mapping('taxonomy_term', $this->vocabulary->id())->setBundleMapping(array('types' => array('skos:Concept')))->setFieldMapping('name', array('properties' => array('rdfs:label')))->save();
}
示例9: testTaggedWithByNodeType
/**
* Tests that the "tagged with" form element only shows for node types that support it.
*/
function testTaggedWithByNodeType()
{
// The tagging field is associated with one of our node types only. So the
// "tagged with" form element on the view wizard should appear on the form
// by default (when the wizard is configured to display all content) and
// also when the node type that has the tagging field is selected, but not
// when the node type that doesn't have the tagging field is selected.
$tags_xpath = '//input[@name="show[tagged_with]"]';
$this->drupalGet('admin/structure/views/add');
$this->assertFieldByXpath($tags_xpath);
$view['show[type]'] = $this->nodeTypeWithTags->id();
$this->drupalPostForm('admin/structure/views/add', $view, t('Update "of type" choice'));
$this->assertFieldByXpath($tags_xpath);
$view['show[type]'] = $this->nodeTypeWithoutTags->id();
$this->drupalPostForm(NULL, $view, t('Update "of type" choice'));
$this->assertNoFieldByXpath($tags_xpath);
// If we add an instance of the tagging field to the second node type, the
// "tagged with" form element should not appear for it too.
entity_create('field_config', array('field_name' => $this->tagFieldName, 'entity_type' => 'node', 'bundle' => $this->nodeTypeWithoutTags->id(), 'settings' => array('handler' => 'default', 'handler_settings' => array('target_bundles' => array($this->tagVocabulary->id() => $this->tagVocabulary->id()), 'auto_create' => TRUE))))->save();
entity_get_form_display('node', $this->nodeTypeWithoutTags->id(), 'default')->setComponent($this->tagFieldName, array('type' => 'entity_reference_autocomplete_tags'))->save();
$view['show[type]'] = $this->nodeTypeWithTags->id();
$this->drupalPostForm('admin/structure/views/add', $view, t('Update "of type" choice'));
$this->assertFieldByXpath($tags_xpath);
$view['show[type]'] = $this->nodeTypeWithoutTags->id();
$this->drupalPostForm(NULL, $view, t('Update "of type" choice'));
$this->assertFieldByXpath($tags_xpath);
}
示例10: testConfigEntityReferenceItem
/**
* Tests the entity reference field type for referencing config entities.
*/
public function testConfigEntityReferenceItem()
{
$referenced_entity_id = $this->vocabulary->id();
// Just being able to create the entity like this verifies a lot of code.
$entity = EntityTest::create();
$entity->field_test_taxonomy_vocabulary->target_id = $referenced_entity_id;
$entity->name->value = $this->randomMachineName();
$entity->save();
$entity = entity_load('entity_test', $entity->id());
$this->assertTrue($entity->field_test_taxonomy_vocabulary instanceof FieldItemListInterface, 'Field implements interface.');
$this->assertTrue($entity->field_test_taxonomy_vocabulary[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEqual($entity->field_test_taxonomy_vocabulary->target_id, $referenced_entity_id);
$this->assertEqual($entity->field_test_taxonomy_vocabulary->entity->label(), $this->vocabulary->label());
$this->assertEqual($entity->field_test_taxonomy_vocabulary->entity->id(), $referenced_entity_id);
$this->assertEqual($entity->field_test_taxonomy_vocabulary->entity->uuid(), $this->vocabulary->uuid());
// Change the name of the term via the reference.
$new_name = $this->randomMachineName();
$entity->field_test_taxonomy_vocabulary->entity->set('name', $new_name);
$entity->field_test_taxonomy_vocabulary->entity->save();
// Verify it is the correct name.
$vocabulary = Vocabulary::load($referenced_entity_id);
$this->assertEqual($vocabulary->label(), $new_name);
// Make sure the computed term reflects updates to the term id.
$vocabulary2 = $vocabulary = Vocabulary::create(['name' => $this->randomMachineName(), 'vid' => Unicode::strtolower($this->randomMachineName()), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED]);
$vocabulary2->save();
$entity->field_test_taxonomy_vocabulary->target_id = $vocabulary2->id();
$this->assertEqual($entity->field_test_taxonomy_vocabulary->entity->id(), $vocabulary2->id());
$this->assertEqual($entity->field_test_taxonomy_vocabulary->entity->label(), $vocabulary2->label());
// Delete terms so we have nothing to reference and try again
$this->vocabulary->delete();
$vocabulary2->delete();
$entity = EntityTest::create(array('name' => $this->randomMachineName()));
$entity->save();
}
示例11: setUp
protected function setUp()
{
parent::setUp();
$web_user = $this->drupalCreateUser(array('bypass node access', 'administer taxonomy'));
$this->drupalLogin($web_user);
$this->vocabulary = $this->createVocabulary();
// Create the field.
$this->fieldName = 'field_taxonomy_test';
$handler_settings = array('target_bundles' => array($this->vocabulary->id() => $this->vocabulary->id()), 'auto_create' => TRUE);
$this->createEntityReferenceField('node', 'article', $this->fieldName, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
entity_get_form_display('node', 'article', 'default')->setComponent($this->fieldName, array('type' => 'options_select'))->save();
entity_get_display('node', 'article', 'full')->setComponent($this->fieldName, array('type' => 'entity_reference_label'))->save();
// Set the RDF mapping for the new field.
rdf_get_mapping('node', 'article')->setFieldMapping($this->fieldName, array('properties' => array('dc:subject'), 'mapping_type' => 'rel'))->save();
rdf_get_mapping('taxonomy_term', $this->vocabulary->id())->setBundleMapping(array('types' => array('skos:Concept')))->setFieldMapping('name', array('properties' => array('rdfs:label')))->save();
}
示例12: testTaxonomyRss
/**
* Tests that terms added to nodes are displayed in core RSS feed.
*
* Create a node and assert that taxonomy terms appear in rss.xml.
*/
function testTaxonomyRss()
{
// Create two taxonomy terms.
$term1 = $this->createTerm($this->vocabulary);
// RSS display must be added manually.
$this->drupalGet("admin/structure/types/manage/article/display");
$edit = array("display_modes_custom[rss]" => '1');
$this->drupalPostForm(NULL, $edit, t('Save'));
// Change the format to 'RSS category'.
$this->drupalGet("admin/structure/types/manage/article/display/rss");
$edit = array("fields[taxonomy_" . $this->vocabulary->id() . "][type]" => 'entity_reference_rss_category');
$this->drupalPostForm(NULL, $edit, t('Save'));
// Post an article.
$edit = array();
$edit['title[0][value]'] = $this->randomMachineName();
$edit[$this->fieldName . '[]'] = $term1->id();
$this->drupalPostForm('node/add/article', $edit, t('Save'));
// Check that the term is displayed when the RSS feed is viewed.
$this->drupalGet('rss.xml');
$test_element = sprintf('<category %s>%s</category>', 'domain="' . $term1->url('canonical', array('absolute' => TRUE)) . '"', $term1->getName());
$this->assertRaw($test_element, 'Term is displayed when viewing the rss feed.');
// Test that the feed icon exists for the term.
$this->drupalGet("taxonomy/term/{$term1->id()}");
$this->assertLinkByHref("taxonomy/term/{$term1->id()}/feed");
// Test that the feed page exists for the term.
$this->drupalGet("taxonomy/term/{$term1->id()}/feed");
$this->assertRaw('<rss version="2.0"', "Feed page is RSS.");
// Check that the "Exception value" is disabled by default.
$this->drupalGet('taxonomy/term/all/feed');
$this->assertResponse(404);
// Set the exception value to 'all'.
$view = Views::getView('taxonomy_term');
$arguments = $view->getDisplay()->getOption('arguments');
$arguments['tid']['exception']['value'] = 'all';
$view->getDisplay()->overrideOption('arguments', $arguments);
$view->storage->save();
// Check the article is shown in the feed.
$node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
$raw_xml = '<title>' . $node->label() . '</title>';
$this->drupalGet('taxonomy/term/all/feed');
$this->assertRaw($raw_xml, "Raw text '{$raw_xml}' is found.");
// Unpublish the article and check that it is not shown in the feed.
$node->setPublished(FALSE)->save();
$this->drupalGet('taxonomy/term/all/feed');
$this->assertNoRaw($raw_xml);
}
示例13: createTerm
/**
* Returns a new term with random name and description in $this->vocabulary.
*
* @return \Drupal\taxonomy\TermInterface
* The created taxonomy term.
*/
protected function createTerm()
{
$filter_formats = filter_formats();
$format = array_pop($filter_formats);
$term = Term::create(['name' => $this->randomMachineName(), 'description' => $this->randomMachineName(), 'format' => $format->id(), 'vid' => $this->vocabulary->id(), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED]);
$term->save();
return $term;
}
示例14: createTerm
/**
* Returns a new term with random name and description in $this->vocabulary.
*
* @return \Drupal\taxonomy\TermInterface
* The created taxonomy term.
*/
protected function createTerm()
{
$filter_formats = filter_formats();
$format = array_pop($filter_formats);
$term = entity_create('taxonomy_term', array('name' => $this->randomName(), 'description' => $this->randomName(), 'format' => $format->format, 'vid' => $this->vocabulary->id(), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED));
$term->save();
return $term;
}
示例15: setUp
protected function setUp()
{
parent::setUp();
// Create an administrative user.
$this->drupalLogin($this->drupalCreateUser(['administer taxonomy', 'bypass node access']));
// Create a vocabulary and add two term reference fields to article nodes.
$this->vocabulary = $this->createVocabulary();
$this->fieldName1 = Unicode::strtolower($this->randomMachineName());
$handler_settings = array('target_bundles' => array($this->vocabulary->id() => $this->vocabulary->id()), 'auto_create' => TRUE);
$this->createEntityReferenceField('node', 'article', $this->fieldName1, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
entity_get_form_display('node', 'article', 'default')->setComponent($this->fieldName1, array('type' => 'options_select'))->save();
entity_get_display('node', 'article', 'default')->setComponent($this->fieldName1, array('type' => 'entity_reference_label'))->save();
$this->fieldName2 = Unicode::strtolower($this->randomMachineName());
$this->createEntityReferenceField('node', 'article', $this->fieldName2, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
entity_get_form_display('node', 'article', 'default')->setComponent($this->fieldName2, array('type' => 'options_select'))->save();
entity_get_display('node', 'article', 'default')->setComponent($this->fieldName2, array('type' => 'entity_reference_label'))->save();
}