/** * 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 = entity_load('taxonomy_term', $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(); $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()); // 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(); }
/** * 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); }
/** * Tests white-listing of methods doesn't interfere with chaining. */ public function testWhiteListChaining() { $node = Node::create(['type' => 'page', 'title' => 'Some node mmk', 'status' => 1, 'field_term' => $this->term->id()]); $node->save(); $this->setRawContent(twig_render_template(drupal_get_path('theme', 'test_theme') . '/templates/node.html.twig', ['node' => $node])); $this->assertText('Sometimes people are just jerks'); }
/** * 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); }
protected function setUp() { parent::setUp(); $this->mockStandardInstall(); ViewTestData::createTestViews(get_class($this), array('taxonomy_test_views')); $this->term1 = $this->createTerm(); $this->term2 = $this->createTerm(); $node = array(); $node['type'] = 'article'; $node['field_views_testing_tags'][]['target_id'] = $this->term1->id(); $node['field_views_testing_tags'][]['target_id'] = $this->term2->id(); $this->nodes[] = $this->drupalCreateNode($node); $this->nodes[] = $this->drupalCreateNode($node); }
/** * Returns forum page for a given forum. * * @param \Drupal\taxonomy\TermInterface $taxonomy_term * The forum to render the page for. * * @return array * A render array. */ public function catalogPage(TermInterface $taxonomy_term) { // Get forum details. $taxonomy_term->forums = $this->forumManager->getChildren($this->config('forum.settings')->get('vocabulary'), $taxonomy_term->id()); $taxonomy_term->parents = $this->forumManager->getParents($taxonomy_term->id()); if (empty($taxonomy_term->forum_container->value)) { $build = $this->forumManager->getTopics($taxonomy_term->id(), $this->currentUser()); $topics = $build['topics']; $header = $build['header']; } else { $topics = ''; $header = array(); } return $this->build($taxonomy_term->forums, $taxonomy_term, $topics, $taxonomy_term->parents, $header); }
protected function setUp() { parent::setUp(); $type = $this->drupalCreateContentType(array('type' => 'article')); // Create the vocabulary for the tag field. $this->vocabulary = entity_create('taxonomy_vocabulary', array('name' => 'quickedit testing tags', 'vid' => 'quickedit_testing_tags')); $this->vocabulary->save(); $this->field_name = 'field_' . $this->vocabulary->id(); entity_create('field_storage_config', array('name' => $this->field_name, 'entity_type' => 'node', 'type' => 'taxonomy_term_reference', 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, 'settings' => array('allowed_values' => array(array('vocabulary' => $this->vocabulary->id(), 'parent' => 0)))))->save(); entity_create('field_instance_config', array('field_name' => $this->field_name, 'entity_type' => 'node', 'label' => 'Tags', 'bundle' => 'article'))->save(); entity_get_form_display('node', 'article', 'default')->setComponent($this->field_name, array('type' => 'taxonomy_autocomplete', 'weight' => -4))->save(); entity_get_display('node', 'article', 'default')->setComponent($this->field_name, array('type' => 'taxonomy_term_reference_link', 'weight' => 10))->save(); entity_get_display('node', 'article', 'teaser')->setComponent($this->field_name, array('type' => 'taxonomy_term_reference_link', 'weight' => 10))->save(); $this->term1 = $this->createTerm(); $this->term2 = $this->createTerm(); $node = array(); $node['type'] = 'article'; $node[$this->field_name][]['target_id'] = $this->term1->id(); $node[$this->field_name][]['target_id'] = $this->term2->id(); $this->node = $this->drupalCreateNode($node); $this->editor_user = $this->drupalCreateUser(array('access content', 'create article content', 'edit any article content', 'access in-place editing')); }
/** * Tests Quick Edit autocomplete term behavior. */ public function testAutocompleteQuickEdit() { $this->drupalLogin($this->editorUser); $quickedit_uri = 'quickedit/form/node/' . $this->node->id() . '/' . $this->fieldName . '/' . $this->node->language()->getId() . '/full'; $post = array('nocssjs' => 'true') + $this->getAjaxPageStatePostData(); $response = $this->drupalPost($quickedit_uri, 'application/vnd.drupal-ajax', $post); $ajax_commands = Json::decode($response); // Prepare form values for submission. drupalPostAJAX() is not suitable for // handling pages with JSON responses, so we need our own solution here. $form_tokens_found = preg_match('/\\sname="form_token" value="([^"]+)"/', $ajax_commands[0]['data'], $token_match) && preg_match('/\\sname="form_build_id" value="([^"]+)"/', $ajax_commands[0]['data'], $build_id_match); $this->assertTrue($form_tokens_found, 'Form tokens found in output.'); if ($form_tokens_found) { $post = array('form_id' => 'quickedit_field_form', 'form_token' => $token_match[1], 'form_build_id' => $build_id_match[1], $this->fieldName . '[target_id]' => implode(', ', array($this->term1->getName(), 'new term', $this->term2->getName())), 'op' => t('Save')); // Submit field form and check response. Should render back all the terms. $response = $this->drupalPost($quickedit_uri, 'application/vnd.drupal-ajax', $post); $this->assertResponse(200); $ajax_commands = Json::decode($response); $this->setRawContent($ajax_commands[0]['data']); $this->assertLink($this->term1->getName()); $this->assertLink($this->term2->getName()); $this->assertText('new term'); $this->assertNoLink('new term'); // Load the form again, which should now get it back from // PrivateTempStore. $quickedit_uri = 'quickedit/form/node/' . $this->node->id() . '/' . $this->fieldName . '/' . $this->node->language()->getId() . '/full'; $post = array('nocssjs' => 'true') + $this->getAjaxPageStatePostData(); $response = $this->drupalPost($quickedit_uri, 'application/vnd.drupal-ajax', $post); $ajax_commands = Json::decode($response); // The AjaxResponse's first command is an InsertCommand which contains // the form to edit the taxonomy term field, it should contain all three // taxonomy terms, including the one that has just been newly created and // which is not yet stored. $this->setRawContent($ajax_commands[0]['data']); $expected = array($this->term1->getName() . ' (' . $this->term1->id() . ')', 'new term', $this->term2->getName() . ' (' . $this->term2->id() . ')'); $this->assertFieldByName($this->fieldName . '[target_id]', implode(', ', $expected)); // Save the entity. $post = array('nocssjs' => 'true'); $response = $this->drupalPostWithFormat('quickedit/entity/node/' . $this->node->id(), 'json', $post); $this->assertResponse(200); // The full node display should now link to all entities, with the new // one created in the database as well. $this->drupalGet('node/' . $this->node->id()); $this->assertLink($this->term1->getName()); $this->assertLink($this->term2->getName()); $this->assertLink('new term'); } }
protected function setUp() { parent::setUp(); // Use Classy theme for testing markup output. \Drupal::service('theme_handler')->install(['classy']); \Drupal::service('theme_handler')->setDefault('classy'); $this->baseUri = \Drupal::url('<front>', [], ['absolute' => TRUE]); // Create two test users. $this->adminUser = $this->drupalCreateUser(array('administer content types', 'administer comments', 'access comments', 'access content')); $this->webUser = $this->drupalCreateUser(array('access comments', 'post comments', 'skip comment approval', 'access content')); $this->drupalLogin($this->adminUser); // Create term. $this->term = Term::create(['name' => $this->randomMachineName(), 'description' => $this->randomMachineName(), 'vid' => 'tags']); $this->term->save(); // Create image. file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example.jpg'); $this->image = File::create(['uri' => 'public://example.jpg']); $this->image->save(); // Create article. $article_settings = array('type' => 'article', 'promote' => NODE_PROMOTED, 'field_image' => array(array('target_id' => $this->image->id())), 'field_tags' => array(array('target_id' => $this->term->id()))); $this->article = $this->drupalCreateNode($article_settings); // Create second article to test teaser list. $this->drupalCreateNode(array('type' => 'article', 'promote' => NODE_PROMOTED)); // Create article comment. $this->articleComment = $this->saveComment($this->article->id(), $this->webUser->id(), NULL, 0); // Create page. $this->page = $this->drupalCreateNode(array('type' => 'page')); // Set URIs. // Image. $image_file = $this->article->get('field_image')->entity; $this->imageUri = ImageStyle::load('large')->buildUrl($image_file->getFileUri()); // Term. $this->termUri = $this->term->url('canonical', array('absolute' => TRUE)); // Article. $this->articleUri = $this->article->url('canonical', array('absolute' => TRUE)); // Page. $this->pageUri = $this->page->url('canonical', array('absolute' => TRUE)); // Author. $this->authorUri = $this->adminUser->url('canonical', array('absolute' => TRUE)); // Comment. $this->articleCommentUri = $this->articleComment->url('canonical', array('absolute' => TRUE)); // Commenter. $this->commenterUri = $this->webUser->url('canonical', array('absolute' => TRUE)); $this->drupalLogout(); }
protected function setUp() { parent::setUp(); $this->base_uri = \Drupal::url('<front>', [], ['absolute' => TRUE]); // Create two test users. $this->adminUser = $this->drupalCreateUser(array('administer content types', 'administer comments', 'access comments', 'access content')); $this->webUser = $this->drupalCreateUser(array('access comments', 'post comments', 'skip comment approval', 'access content')); $this->drupalLogin($this->adminUser); // Create term. $this->term = entity_create('taxonomy_term', array('name' => $this->randomMachineName(), 'description' => $this->randomMachineName(), 'vid' => 'tags')); $this->term->save(); // Create image. file_unmanaged_copy(DRUPAL_ROOT . '/core/misc/druplicon.png', 'public://example.jpg'); $this->image = entity_create('file', array('uri' => 'public://example.jpg')); $this->image->save(); // Create article. $article_settings = array('type' => 'article', 'promote' => NODE_PROMOTED, 'field_image' => array(array('target_id' => $this->image->id())), 'field_tags' => array(array('target_id' => $this->term->id()))); $this->article = $this->drupalCreateNode($article_settings); // Create second article to test teaser list. $this->drupalCreateNode(array('type' => 'article', 'promote' => NODE_PROMOTED)); // Create article comment. $this->articleComment = $this->saveComment($this->article->id(), $this->webUser->id(), NULL, 0); // Create page. $this->page = $this->drupalCreateNode(array('type' => 'page')); // Set URIs. // Image. $image_file = $this->article->get('field_image')->entity; $this->imageUri = entity_load('image_style', 'large')->buildUrl($image_file->getFileUri()); // Term. $this->termUri = $this->term->url('canonical', array('absolute' => TRUE)); // Article. $this->articleUri = $this->article->url('canonical', array('absolute' => TRUE)); // Page. $this->pageUri = $this->page->url('canonical', array('absolute' => TRUE)); // Author. $this->authorUri = $this->adminUser->url('canonical', array('absolute' => TRUE)); // Comment. $this->articleCommentUri = $this->articleComment->url('canonical', array('absolute' => TRUE)); // Commenter. $this->commenterUri = $this->webUser->url('canonical', array('absolute' => TRUE)); $this->drupalLogout(); }
/** * Generate a menu link plugin definition for a taxonomy term. * * @param \Drupal\taxonomy\TermInterface $term * The taxonomy term for which to build a menu link render array. * @param $base_plugin_definition * The base plugin definition to merge the link with. * * @return array */ protected function buildMenuDefinition(TermInterface $term, $base_plugin_definition) { $term_id = $term->id(); $term_url = $term->urlInfo(); $taxonomy_menu_id = $this->id(); $menu_id = $this->getMenu(); // Determine parent link. // TODO: Evaluate use case of multiple parents (should we make many menu items?) $menu_parent_id = NULL; /** @var $termStorage \Drupal\taxonomy\TermStorageInterface */ $termStorage = $this->entityManager()->getStorage('taxonomy_term'); $parents = $termStorage->loadParents($term_id); $parents = array_values($parents); if (is_array($parents) && count($parents) && !is_null($parents[0]) && $parents[0] != '0') { $menu_parent_id = $this->buildMenuPluginId($parents[0]); } // TODO: Consider implementing a forced weight based on taxonomy tree. // Generate link. $arguments = ['taxonomy_term' => $term_id]; $link = $base_plugin_definition; $link += array('id' => $this->buildMenuPluginId($term), 'title' => $term->label(), 'description' => $term->getDescription(), 'menu_name' => $menu_id, 'metadata' => array('taxonomy_menu_id' => $taxonomy_menu_id, 'taxonomy_term_id' => $term_id), 'route_name' => $term_url->getRouteName(), 'route_parameters' => $term_url->getRouteParameters(), 'load arguments' => $arguments, 'parent' => $menu_parent_id, 'provider' => 'taxonomy_menu', 'class' => 'Drupal\\taxonomy_menu\\Plugin\\Menu\\TaxonomyMenuMenuLink'); return $link; }
/** * Returns a renderable forum index page array. * * @param array $forums * A list of forums. * @param \Drupal\taxonomy\TermInterface $term * The taxonomy term of the forum. * @param array $topics * The topics of this forum. * @param array $parents * The parent forums in relation this forum. * @param array $header * Array of header cells. * * @return array * A render array. */ protected function build($forums, TermInterface $term, $topics = array(), $parents = array(), $header = array()) { $config = $this->config('forum.settings'); $build = array('#theme' => 'forums', '#forums' => $forums, '#topics' => $topics, '#parents' => $parents, '#header' => $header, '#term' => $term, '#sortby' => $config->get('topics.order'), '#forums_per_page' => $config->get('topics.page_limit')); $build['#attached']['library'][] = 'forum/forum.index'; if (empty($term->forum_container->value)) { $build['#attached']['drupal_add_feed'][] = array('taxonomy/term/' . $term->id() . '/feed', 'RSS - ' . $term->getName()); } return $build; }
/** * Generates an action link to display at the top of the forum listing. * * @param string $vid * Vocabulary ID. * @param \Drupal\taxonomy\TermInterface $forum_term * The term for which the links are to be built. * * @return array * Render array containing the links. */ protected function buildActionLinks($vid, TermInterface $forum_term = NULL) { $user = $this->currentUser(); $links = []; // Loop through all bundles for forum taxonomy vocabulary field. foreach ($this->fieldMap['node']['taxonomy_forums']['bundles'] as $type) { if ($this->nodeAccess->createAccess($type)) { $links[$type] = ['#attributes' => ['class' => ['action-links']], '#theme' => 'menu_local_action', '#link' => ['title' => $this->t('Add new @node_type', ['@node_type' => $this->nodeTypeStorage->load($type)->label()]), 'url' => Url::fromRoute('node.add', ['node_type' => $type])]]; if ($forum_term && $forum_term->bundle() == $vid) { // We are viewing a forum term (specific forum), append the tid to // the url. $links[$type]['#link']['localized_options']['query']['forum_id'] = $forum_term->id(); } } } if (empty($links)) { // Authenticated user does not have access to create new topics. if ($user->isAuthenticated()) { $links['disallowed'] = ['#markup' => $this->t('You are not allowed to post new content in the forum.')]; } else { $links['login'] = ['#attributes' => ['class' => ['action-links']], '#theme' => 'menu_local_action', '#link' => array('title' => $this->t('Log in to post new content in the forum.'), 'url' => Url::fromRoute('user.login', [], ['query' => $this->getDestinationArray()]))]; } } return $links; }
/** * Remove menu entries associate with the vocabulary of this term. * * @param \Drupal\taxonomy\TermInterface $term */ public function removeTaxonomyMenuEntries(TermInterface $term, $rebuild_all = TRUE) { // Load relevant taxonomy menus. $tax_menus = $this->getTermMenusByVocabulary($term->getVocabularyId()); foreach ($tax_menus as $menu) { foreach (array_keys($menu->getLinks([], TRUE)) as $plugin_id) { if (!$rebuild_all) { $plugin_id_parts = explode('.', $plugin_id); $term_id = array_pop($plugin_id_parts); if ($term->id() != $term_id) { continue; } } $this->manager->removeDefinition($plugin_id, FALSE); } } }