Issue #2428881 by amateescu: Remove TermAutocompleteController::autocompletePerVid()

8.0.x
Alex Pott 2015-02-19 09:37:32 +00:00
parent d6458cd8fa
commit 30ea8f7c0c
5 changed files with 15 additions and 217 deletions

View File

@ -127,33 +127,6 @@ class TermAutocompleteController implements ContainerInjectionInterface {
return new JsonResponse($matches);
}
/**
* Retrieves suggestions for taxonomy term autocompletion by vocabulary ID.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object.
* @param \Drupal\taxonomy\VocabularyInterface $taxonomy_vocabulary
* The vocabulary to filter by.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* A JSON response containing the autocomplete suggestions for taxonomy
* terms.
*/
public function autocompletePerVid(Request $request, VocabularyInterface $taxonomy_vocabulary) {
// A comma-separated list of term names entered in the autocomplete form
// element. Only the last term is used for autocompletion.
$tags_typed = $request->query->get('q');
$tags_typed = Tags::explode($tags_typed);
$tag_last = Unicode::strtolower(array_pop($tags_typed));
$matches = array();
if ($tag_last != '') {
$vids = array($taxonomy_vocabulary->id());
$matches = $this->getMatchingTerms($tags_typed, $vids, $tag_last);
}
return new JsonResponse($matches);
}
/**
* Gets terms which matches some typed terms.
*

View File

@ -7,6 +7,7 @@
namespace Drupal\taxonomy\Plugin\views\filter;
use Drupal\Core\Entity\Element\EntityAutocomplete;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\taxonomy\Entity\Term;
@ -159,26 +160,19 @@ class TaxonomyIndexTid extends ManyToOne {
}
if ($this->options['type'] == 'textfield') {
$default = '';
if ($this->value) {
$terms = Term::loadMultiple(($this->value));
foreach ($terms as $term) {
if ($default) {
$default .= ', ';
}
$default .= String::checkPlain(\Drupal::entityManager()->getTranslationFromContext($term)->label());
}
}
$terms = $this->value ? Term::loadMultiple(($this->value)) : array();
$form['value'] = array(
'#title' => $this->options['limit'] ? $this->t('Select terms from vocabulary @voc', array('@voc' => $vocabulary->label())) : $this->t('Select terms'),
'#type' => 'textfield',
'#default_value' => $default,
'#default_value' => EntityAutocomplete::getEntityLabels($terms),
);
if ($this->options['limit']) {
$form['value']['#autocomplete_route_name'] = 'taxonomy.autocomplete_vid';
$form['value']['#autocomplete_route_parameters'] = array('taxonomy_vocabulary' => $vocabulary->id());
$form['value']['#type'] = 'entity_autocomplete';
$form['value']['#target_type'] = 'taxonomy_term';
$form['value']['#selection_settings']['target_bundles'] = array($vocabulary->id());
$form['value']['#tags'] = TRUE;
$form['value']['#process_default_value'] = FALSE;
}
}
else {
@ -270,10 +264,11 @@ class TaxonomyIndexTid extends ManyToOne {
return;
}
$values = Tags::explode($form_state->getValue('options', 'value'));
if ($tids = $this->validate_term_strings($form['value'], $values, $form_state)) {
$form_state->setValue(array('options', 'value'), $tids);
$tids = array();
foreach ($form_state->getValue(array('options', 'value')) as $value) {
$tids[] = $value['target_id'];
}
$form_state->setValue(array('options', 'value'), $tids);
}
public function acceptExposedInput($input) {
@ -322,66 +317,11 @@ class TaxonomyIndexTid extends ManyToOne {
return;
}
$values = Tags::explode($form_state->getValue($identifier));
$tids = $this->validate_term_strings($form[$identifier], $values, $form_state);
if ($tids) {
$this->validated_exposed_input = $tids;
foreach ($form_state->getValue($identifier) as $value) {
$this->validated_exposed_input[] = $value['target_id'];
}
}
/**
* Validate the user string. Since this can come from either the form
* or the exposed filter, this is abstracted out a bit so it can
* handle the multiple input sources.
*
* @param $form
* The form which is used, either the views ui or the exposed filters.
* @param $values
* The taxonomy names which will be converted to tids.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array
* The taxonomy ids fo all validated terms.
*/
function validate_term_strings(&$form, $values, FormStateInterface $form_state) {
if (empty($values)) {
return array();
}
$tids = array();
$names = array();
$missing = array();
foreach ($values as $value) {
$missing[strtolower($value)] = TRUE;
$names[] = $value;
}
if (!$names) {
return FALSE;
}
$query = \Drupal::entityQuery('taxonomy_term')
->condition('name', $names, 'IN')
->condition('vid', $this->options['vid'])
->addTag('term_access');
$terms = Term::loadMultiple($query->execute());
foreach ($terms as $term) {
unset($missing[strtolower(\Drupal::entityManager()->getTranslationFromContext($term)->label())]);
$tids[] = $term->id();
}
if ($missing && !empty($this->options['error_message'])) {
$form_state->setError($form, $this->formatPlural(count($missing), 'Unable to find term: @terms', 'Unable to find terms: @terms', array('@terms' => implode(', ', array_keys($missing)))));
}
elseif ($missing && empty($this->options['error_message'])) {
$tids = array(0);
}
return $tids;
}
protected function valueSubmit($form, FormStateInterface $form_state) {
// prevent array_filter from messing up our arrays in parent submit.
}

View File

@ -101,8 +101,7 @@ class TaxonomyIndexTidUiTest extends UITestBase {
$display['display_options']['filters']['tid']['type'] = 'textfield';
$view->save();
$this->drupalGet('admin/structure/views/nojs/handler/test_filter_taxonomy_index_tid/default/filter/tid');
$result = $this->xpath('//input[@id="edit-options-value"]/@data-autocomplete-path');
$this->assertEqual((string) $result[0], \Drupal::url('taxonomy.autocomplete_vid', ['taxonomy_vocabulary' => 'tags']));
$this->assertFieldByXPath('//input[@id="edit-options-value"]');
// Tests \Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid::calculateDependencies().
$expected = [

View File

@ -73,13 +73,6 @@ taxonomy.autocomplete:
requirements:
_permission: 'access content'
taxonomy.autocomplete_vid:
path: '/taxonomy/autocomplete_vid/{taxonomy_vocabulary}'
defaults:
_controller: '\Drupal\taxonomy\Controller\TermAutocompleteController::autocompletePerVid'
requirements:
_permission: 'access content'
entity.taxonomy_vocabulary.overview_form:
path: '/admin/structure/taxonomy/manage/{taxonomy_vocabulary}/overview'
defaults:

View File

@ -1,107 +0,0 @@
<?php
/**
* @file
* Contains \Drupal\views\Tests\ViewsTaxonomyAutocompleteTest.
*/
namespace Drupal\views\Tests;
use Drupal\Component\Utility\String;
use Drupal\Core\Language\LanguageInterface;
use Drupal\views\Tests\ViewTestBase;
/**
* Tests the views taxonomy complete menu callback.
*
* @group views
* @see views_ajax_autocomplete_taxonomy()
*/
class ViewsTaxonomyAutocompleteTest extends ViewTestBase {
/**
* The taxonomy vocabulary created for this test.
*
* @var \Drupal\taxonomy\VocabularyInterface
*/
protected $vocabulary;
/**
* Stores the first term used in the different tests.
*
* @var \Drupal\taxonomy\TermInterface
*/
protected $term1;
/**
* Stores the second term used in the different tests.
*
* @var \Drupal\taxonomy\TermInterface
*/
protected $term2;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('node', 'taxonomy');
protected function setUp() {
parent::setUp();
// Create the vocabulary for the tag field.
$this->vocabulary = entity_create('taxonomy_vocabulary', array(
'name' => 'Views testing tags',
'vid' => 'views_testing_tags',
));
$this->vocabulary->save();
$this->term1 = $this->createTerm('term');
$this->term2 = $this->createTerm('another');
}
/**
* Tests the views_ajax_autocomplete_taxonomy() AJAX callback.
*/
public function testTaxonomyAutocomplete() {
$this->user = $this->drupalCreateUser(array('access content'));
$this->drupalLogin($this->user);
$base_autocomplete_path = 'taxonomy/autocomplete_vid/' . $this->vocabulary->id();
// Test that no terms returns an empty array.
$this->assertIdentical(array(), $this->drupalGetJSON($base_autocomplete_path));
// Test a with whole name term.
$label = $this->term1->getName();
$expected = array(array(
'value' => $label,
'label' => String::checkPlain($label),
));
$this->assertIdentical($expected, $this->drupalGetJSON($base_autocomplete_path, array('query' => array('q' => $label))));
// Test a term by partial name.
$partial = substr($label, 0, 2);
$this->assertIdentical($expected, $this->drupalGetJSON($base_autocomplete_path, array('query' => array('q' => $partial))));
}
/**
* Returns a new term with random properties.
*
* @param string $name
* (optional) The name of the taxonomy term.
*
* @return \Drupal\taxonomy\Entity\Term
* The created taxonomy term.
*/
protected function createTerm($name = NULL) {
$term = entity_create('taxonomy_term', array(
'name' => $name ?: $this->randomMachineName(),
'description' => $this->randomMachineName(),
'vid' => $this->vocabulary->id(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
));
$term->save();
return $term;
}
}