Beispiel #1
0
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     $this->drupalPlaceBlock('system_breadcrumb_block');
     $this->drupalPlaceBlock('local_tasks_block');
     // Create a test user.
     $this->adminUser = $this->drupalCreateUser(array('access content', 'admin classes', 'admin display suite', 'admin fields', 'administer nodes', 'view all revisions', 'administer content types', 'administer node fields', 'administer node form display', 'administer node display', 'administer taxonomy', 'administer taxonomy_term fields', 'administer taxonomy_term display', 'administer users', 'administer permissions', 'administer account settings', 'administer user display', 'administer software updates', 'access site in maintenance mode', 'administer site configuration', 'bypass node access', 'ds switch view mode'));
     $this->drupalLogin($this->adminUser);
     // Create random field name.
     $this->fieldLabel = $this->randomMachineName(8);
     $this->fieldNameInput = strtolower($this->randomMachineName(8));
     $this->fieldName = 'field_' . $this->fieldNameInput;
     // Create Article node type.
     $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article', 'revision' => TRUE));
     $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Page', 'revision' => TRUE));
     // Create a vocabulary named "Tags".
     $this->vocabulary = Vocabulary::create(array('name' => 'Tags', 'vid' => 'tags', 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED));
     $this->vocabulary->save();
     $term1 = Term::create(array('name' => 'Tag 1', 'vid' => 'tags', 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED));
     $term1->save();
     $term2 = Term::create(array('name' => 'Tag 2', 'vid' => 'tags', 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED));
     $term2->save();
     $handler_settings = array('target_bundles' => array($this->vocabulary->id() => $this->vocabulary->id()), 'auto_create' => TRUE);
     $this->createEntityReferenceField('node', 'article', 'field_' . $this->vocabulary->id(), 'Tags', 'taxonomy_term', 'default', $handler_settings, 10);
     entity_get_form_display('node', 'article', 'default')->setComponent('field_' . $this->vocabulary->id())->save();
 }
Beispiel #2
0
 /**
  * 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 = entity_create('taxonomy_term', $values + array('name' => $this->randomMachineName(), 'description' => array('value' => $this->randomMachineName(), 'format' => $format->id()), 'vid' => $vocabulary->id(), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED));
     $term->save();
     return $term;
 }
Beispiel #3
0
 /**
  * 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;
 }
 /**
  * Tests that config bundle deletions are mirrored in field config settings.
  */
 public function testConfigTargetBundleDeletion()
 {
     // Attach an entity reference field to $this->nodeType.
     $name = Unicode::strtolower($this->randomMachineName());
     $label = $this->randomString();
     $vid = $this->vocabulary->id();
     $handler_settings = ['target_bundles' => [$vid => $vid]];
     $this->createEntityReferenceField('node', $this->nodeType->id(), $name, $label, 'taxonomy_term', 'default', $handler_settings);
     // Check that the 'target_bundle' setting contains the vocabulary.
     $field_config = FieldConfig::loadByName('node', $this->nodeType->id(), $name);
     $actual_handler_settings = $field_config->getSetting('handler_settings');
     $this->assertEqual($handler_settings, $actual_handler_settings);
     // Delete the vocabulary.
     $this->vocabulary->delete();
     // Check that the deleted vocabulary is no longer present in the
     // 'target_bundles' field setting.
     $field_config = FieldConfig::loadByName('node', $this->nodeType->id(), $name);
     $handler_settings = $field_config->getSetting('handler_settings');
     $this->assertTrue(empty($handler_settings['target_bundles']));
 }
Beispiel #5
0
  /**
   * Creates and saves a new term with 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.
   */
  private function createTerm(Vocabulary $vocabulary, $values = array()) {
    $filter_formats = filter_formats();
    $format = array_pop($filter_formats);

    $termStorage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
    $term = $termStorage->create($values + array(
        'name' => $this->randomMachineName(),
        'description' => array(
          'value' => $this->randomMachineName(),
          // Use the first available text format.
          'format' => $format->id(),
        ),
        'vid' => $vocabulary->id(),
        'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
      ));
    $term->save();
    return $term;
  }
 /**
  * Tests translate link on vocabulary term list.
  */
 function testTranslateLinkVocabularyAdminPage()
 {
     $this->admin_user = $this->drupalCreateUser(array_merge(parent::getTranslatorPermissions(), array('access administration pages', 'administer taxonomy')));
     $this->drupalLogin($this->admin_user);
     $values = array('name' => $this->randomName());
     $translatable_tid = $this->createEntity($values, $this->langcodes[0], $this->vocabulary->id());
     // Create an untranslatable vocabulary.
     $untranslatable_vocabulary = entity_create('taxonomy_vocabulary', array('name' => 'untranslatable_voc', 'description' => $this->randomName(), 'vid' => 'untranslatable_voc', 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, 'weight' => mt_rand(0, 10)));
     $untranslatable_vocabulary->save();
     $values = array('name' => $this->randomName());
     $untranslatable_tid = $this->createEntity($values, $this->langcodes[0], $untranslatable_vocabulary->id());
     // Verify translation links.
     $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview');
     $this->assertResponse(200, 'The translatable vocabulary page was found.');
     $this->assertLinkByHref('term/' . $translatable_tid . '/translations', 0, 'The translations link exists for a translatable vocabulary.');
     $this->assertLinkByHref('term/' . $translatable_tid . '/edit', 0, 'The edit link exists for a translatable vocabulary.');
     $this->drupalGet('admin/structure/taxonomy/manage/' . $untranslatable_vocabulary->id() . '/overview');
     $this->assertResponse(200);
     $this->assertLinkByHref('term/' . $untranslatable_tid . '/edit');
     $this->assertNoLinkByHref('term/' . $untranslatable_tid . '/translations');
 }
 /**
  * Create the taxonomy term reference field for testing.
  *
  * @param string $field_name
  *   The name of the field to create.
  * @param \Drupal\taxonomy\Entity\Vocabulary $vocabulary
  *   The vocabulary that the field should use.
  *
  * @todo Move this to TaxonomyTestBase, like the other field modules.
  */
 protected function createTaxonomyTermReferenceField($field_name, $vocabulary)
 {
     entity_create('field_storage_config', array('name' => $field_name, 'entity_type' => 'node', 'type' => 'taxonomy_term_reference', 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, 'settings' => array('allowed_values' => array(array('vocabulary' => $vocabulary->id(), 'parent' => '0')))))->save();
     entity_create('field_instance_config', array('field_name' => $field_name, 'entity_type' => 'node', 'bundle' => 'article'))->save();
     entity_get_form_display('node', 'article', 'default')->setComponent($field_name, array('type' => 'options_select'))->save();
     entity_get_display('node', 'article', 'full')->setComponent($field_name, array('type' => 'taxonomy_term_reference_link'))->save();
 }
  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    // Create a vocabulary.
    $this->vocabulary = $this->createVocabulary();

    // Create 11 terms, which have some sub-string in common, in a
    // non-alphabetical order, so that we will have more than 10 matches later
    // when we test the correct number of results is returned, and we can test
    // the order of the results. The location of the sub-string to match varies
    // also, since it should not be necessary to start with the sub-string to
    // match it. Save term IDs to reuse later.
    $termNames = [
      'aaa 20 bbb',
      'aaa 70 bbb',
      'aaa 10 bbb',
      'aaa 12 bbb',
      'aaa 40 bbb',
      'aaa 11 bbb',
      'aaa 30 bbb',
      'aaa 50 bbb',
      'aaa 80',
      'aaa 90',
      'bbb 60 aaa',
    ];
    foreach ($termNames as $termName) {
      $term = $this->createTerm($this->vocabulary, ['name' => $termName]);
      $this->termIds[$termName] = $term->id();
    }

    // Create a taxonomy_term_reference field on the article Content Type that
    // uses a taxonomy_autocomplete widget.
    $this->fieldName = Unicode::strtolower($this->randomMachineName());
    FieldStorageConfig::create([
      'field_name' => $this->fieldName,
      'entity_type' => 'node',
      'type' => 'entity_reference',
      'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
      'settings' => [
        'target_type' => 'taxonomy_term',
      ],
    ])->save();
    FieldConfig::create([
      'field_name' => $this->fieldName,
      'bundle' => 'article',
      'entity_type' => 'node',
      'settings' => [
        'handler' => 'default',
        'handler_settings' => [
          // Restrict selection of terms to a single vocabulary.
          'target_bundles' => [
            $this->vocabulary->id() => $this->vocabulary->id(),
          ],
        ],
      ],
    ])->save();
    EntityFormDisplay::load('node.article.default')
      ->setComponent($this->fieldName, [
        'type' => 'entity_reference_autocomplete',
      ])
      ->save();
    EntityViewDisplay::load('node.article.default')
      ->setComponent($this->fieldName, [
        'type' => 'entity_reference_label',
      ])
      ->save();

    // Create a user and then login.
    $this->adminUser = $this->drupalCreateUser(['create article content']);
    $this->drupalLogin($this->adminUser);

    // Retrieve the autocomplete url.
    $this->drupalGet('node/add/article');
    $result = $this->xpath('//input[@name="' . $this->fieldName . '[0][target_id]"]');
    $this->autocompleteUrl = $this->getAbsoluteUrl($result[0]['data-autocomplete-path']);
  }