Commit 8b6fdb56 authored by catch's avatar catch

Issue #2068337 by marcingy, pfrenssen: Convert taxonomy term SQL queries to the Entity Query API.

parent 0d4acb30
...@@ -244,10 +244,12 @@ function forum_node_validate(EntityInterface $node, $form) { ...@@ -244,10 +244,12 @@ function forum_node_validate(EntityInterface $node, $form) {
form_set_error('taxonomy_forums', t('Select a forum.')); form_set_error('taxonomy_forums', t('Select a forum.'));
continue; continue;
} }
$used = db_query_range('SELECT 1 FROM {taxonomy_term_data} WHERE tid = :tid AND vid = :vid', 0, 1, array( $used = \Drupal::entityQuery('taxonomy_term')
':tid' => $term->id(), ->condition('tid', $term->id())
':vid' => $term->bundle(), ->condition('vid', $term->bundle())
))->fetchField(); ->range(0, 1)
->count()
->execute();
if ($used && !empty($term->forum_container->value)) { if ($used && !empty($term->forum_container->value)) {
form_set_error('taxonomy_forums', t('The item %forum is a forum container, not a forum. Select one of the forums below instead.', array('%forum' => $term->label()))); form_set_error('taxonomy_forums', t('The item %forum is a forum container, not a forum. Select one of the forums below instead.', array('%forum' => $term->label())));
} }
......
...@@ -7,8 +7,8 @@ ...@@ -7,8 +7,8 @@
namespace Drupal\taxonomy\Form; namespace Drupal\taxonomy\Form;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityConfirmFormBase; use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\taxonomy\TermStorageControllerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
...@@ -17,17 +17,20 @@ ...@@ -17,17 +17,20 @@
class VocabularyResetForm extends EntityConfirmFormBase { class VocabularyResetForm extends EntityConfirmFormBase {
/** /**
* The database connection object. * The term storage.
* *
* @var \Drupal\Core\Database\Connection * @var \Drupal\taxonomy\TermStorageControllerInterface
*/ */
protected $connection; protected $termStorage;
/** /**
* Constructs a new VocabularyResetForm object. * Constructs a new VocabularyResetForm object.
*
* @param \Drupal\taxonomy\TermStorageControllerInterface $term_storage
* The term storage.
*/ */
public function __construct(Connection $connection) { public function __construct(TermStorageControllerInterface $term_storage) {
$this->connection = $connection; $this->termStorage = $term_storage;
} }
/** /**
...@@ -35,7 +38,7 @@ public function __construct(Connection $connection) { ...@@ -35,7 +38,7 @@ public function __construct(Connection $connection) {
*/ */
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
return new static( return new static(
$container->get('database') $container->get('entity.manager')->getStorageController('taxonomy_term')
); );
} }
...@@ -83,11 +86,7 @@ public function getConfirmText() { ...@@ -83,11 +86,7 @@ public function getConfirmText() {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function save(array $form, array &$form_state) { public function save(array $form, array &$form_state) {
$this->connection->update('taxonomy_term_data') $this->termStorage->resetWeights($this->entity->id());
->fields(array('weight' => 0))
->condition('vid', $this->entity->id())
->execute();
drupal_set_message($this->t('Reset vocabulary %name to alphabetical order.', array('%name' => $this->entity->label()))); drupal_set_message($this->t('Reset vocabulary %name to alphabetical order.', array('%name' => $this->entity->label())));
watchdog('taxonomy', 'Reset vocabulary %name to alphabetical order.', array('%name' => $this->entity->label()), WATCHDOG_NOTICE); watchdog('taxonomy', 'Reset vocabulary %name to alphabetical order.', array('%name' => $this->entity->label()), WATCHDOG_NOTICE);
$form_state['redirect'] = 'admin/structure/taxonomy/manage/' . $this->entity->id(); $form_state['redirect'] = 'admin/structure/taxonomy/manage/' . $this->entity->id();
......
...@@ -82,4 +82,73 @@ public function updateTermHierarchy(EntityInterface $term) { ...@@ -82,4 +82,73 @@ public function updateTermHierarchy(EntityInterface $term) {
$query->execute(); $query->execute();
} }
/**
* {@inheritdoc}
*/
public function loadParents($tid) {
$query = $this->database->select('taxonomy_term_data', 't');
$query->join('taxonomy_term_hierarchy', 'h', 'h.parent = t.tid');
$query->addField('t', 'tid');
$query->condition('h.tid', $tid);
$query->addTag('term_access');
$query->orderBy('t.weight');
$query->orderBy('t.name');
return $query->execute()->fetchCol();
}
/**
* {@inheritdoc}
*/
public function loadChildren($tid, $vid = NULL) {
$query = $this->database->select('taxonomy_term_data', 't');
$query->join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid');
$query->addField('t', 'tid');
$query->condition('h.parent', $tid);
if ($vid) {
$query->condition('t.vid', $vid);
}
$query->addTag('term_access');
$query->orderBy('t.weight');
$query->orderBy('t.name');
return $query->execute()->fetchCol();
}
/**
* {@inheritdoc}
*/
public function loadTree($vid) {
$query = $this->database->select('taxonomy_term_data', 't');
$query->join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid');
return $query
->addTag('term_access')
->fields('t')
->fields('h', array('parent'))
->condition('t.vid', $vid)
->orderBy('t.weight')
->orderBy('t.name')
->execute();
}
/**
* {@inheritdoc}
*/
public function nodeCount($vid) {
$query = $this->database->select('taxonomy_index', 'ti');
$query->addExpression('COUNT(DISTINCT ti.nid)');
$query->leftJoin('taxonomy_term_data', 'td', 'ti.tid = td.tid');
$query->condition('td.vid', $vid);
$query->addTag('vocabulary_node_count');
return $query->execute()->fetchField();
}
/**
* {@inheritdoc}
*/
public function resetWeights($vid) {
$this->database->update('taxonomy_term_data')
->fields(array('weight' => 0))
->condition('vid', $vid)
->execute();
}
} }
...@@ -18,7 +18,7 @@ interface TermStorageControllerInterface extends EntityStorageControllerInterfac ...@@ -18,7 +18,7 @@ interface TermStorageControllerInterface extends EntityStorageControllerInterfac
/** /**
* Removed reference to terms from term_hierarchy. * Removed reference to terms from term_hierarchy.
* *
* @param array * @param array $tids
* Array of terms that need to be removed from hierarchy. * Array of terms that need to be removed from hierarchy.
*/ */
public function deleteTermHierarchy($tids); public function deleteTermHierarchy($tids);
...@@ -31,4 +31,58 @@ public function deleteTermHierarchy($tids); ...@@ -31,4 +31,58 @@ public function deleteTermHierarchy($tids);
*/ */
public function updateTermHierarchy(EntityInterface $term); public function updateTermHierarchy(EntityInterface $term);
/**
* Finds all parents of a given term ID.
*
* @param int $tid
* Term ID to retrieve parents for.
*
* @return array
* An array of term objects which are the parents of the term $tid.
*/
public function loadParents($tid);
/**
* Finds all children of a term ID.
*
* @param int $tid
* Term ID to retrieve parents for.
* @param string $vid
* An optional vocabulary ID to restrict the child search.
*
* @return array
* An array of term objects that are the children of the term $tid.
*/
public function loadChildren($tid, $vid = NULL);
/**
* Finds all terms in a given vocabulary ID.
*
* @param string $vid
* Vocabulary ID to retrieve terms for.
*
* @return array
* An array of term objects that are the children of the vocabulary $vid.
*/
public function loadTree($vid);
/**
* Count the number of nodes in a given vocabulary ID.
*
* @param string $vid
* Vocabulary ID to retrieve terms for.
*
* @return int
* A count of the nodes in a given vocabulary ID.
*/
public function nodeCount($vid);
/**
* Reset the weights for a given vocabulary ID.
*
* @param string $vid
* Vocabulary ID to retrieve terms for.
*/
public function resetWeights($vid);
} }
...@@ -526,14 +526,7 @@ function taxonomy_term_load_parents($tid) { ...@@ -526,14 +526,7 @@ function taxonomy_term_load_parents($tid) {
$parents = &drupal_static(__FUNCTION__, array()); $parents = &drupal_static(__FUNCTION__, array());
if ($tid && !isset($parents[$tid])) { if ($tid && !isset($parents[$tid])) {
$query = db_select('taxonomy_term_data', 't'); $tids = \Drupal::entityManager()->getStorageController('taxonomy_term')->loadParents($tid);
$query->join('taxonomy_term_hierarchy', 'h', 'h.parent = t.tid');
$query->addField('t', 'tid');
$query->condition('h.tid', $tid);
$query->addTag('term_access');
$query->orderBy('t.weight');
$query->orderBy('t.name');
$tids = $query->execute()->fetchCol();
$parents[$tid] = entity_load_multiple('taxonomy_term', $tids); $parents[$tid] = entity_load_multiple('taxonomy_term', $tids);
} }
...@@ -581,17 +574,7 @@ function taxonomy_term_load_children($tid, $vid = NULL) { ...@@ -581,17 +574,7 @@ function taxonomy_term_load_children($tid, $vid = NULL) {
$children = &drupal_static(__FUNCTION__, array()); $children = &drupal_static(__FUNCTION__, array());
if ($tid && !isset($children[$tid])) { if ($tid && !isset($children[$tid])) {
$query = db_select('taxonomy_term_data', 't'); $tids = \Drupal::entityManager()->getStorageController('taxonomy_term')->loadChildren($tid);
$query->join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid');
$query->addField('t', 'tid');
$query->condition('h.parent', $tid);
if ($vid) {
$query->condition('t.vid', $vid);
}
$query->addTag('term_access');
$query->orderBy('t.weight');
$query->orderBy('t.name');
$tids = $query->execute()->fetchCol();
$children[$tid] = entity_load_multiple('taxonomy_term', $tids); $children[$tid] = entity_load_multiple('taxonomy_term', $tids);
} }
...@@ -632,16 +615,7 @@ function taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $load_entities ...@@ -632,16 +615,7 @@ function taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $load_entities
$parents[$vid] = array(); $parents[$vid] = array();
$terms[$vid] = array(); $terms[$vid] = array();
$query = db_select('taxonomy_term_data', 't'); $result = \Drupal::entityManager()->getStorageController('taxonomy_term')->loadTree($vid);
$query->join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid');
$result = $query
->addTag('term_access')
->fields('t')
->fields('h', array('parent'))
->condition('t.vid', $vid)
->orderBy('t.weight')
->orderBy('t.name')
->execute();
foreach ($result as $term) { foreach ($result as $term) {
$children[$vid][$term->parent][] = $term->tid; $children[$vid][$term->parent][] = $term->tid;
......
...@@ -167,21 +167,15 @@ function taxonomy_tokens($type, $tokens, array $data = array(), array $options = ...@@ -167,21 +167,15 @@ function taxonomy_tokens($type, $tokens, array $data = array(), array $options =
break; break;
case 'term-count': case 'term-count':
$query = db_select('taxonomy_term_data'); $replacements[$original] = \Drupal::entityQuery('taxonomy_term')
$query->condition('vid', $vocabulary->id()); ->condition('vid', $vocabulary->id())
$query->addTag('vocabulary_term_count'); ->addTag('vocabulary_term_count')
$count = $query->countQuery()->execute()->fetchField(); ->count()
$replacements[$original] = $count; ->execute();
break; break;
case 'node-count': case 'node-count':
$query = db_select('taxonomy_index', 'ti'); $replacements[$original] = \Drupal::entityManager()->getStorageController('taxonomy_term')->nodeCount($vocabulary->id());
$query->addExpression('COUNT(DISTINCT ti.nid)');
$query->leftJoin('taxonomy_term_data', 'td', 'ti.tid = td.tid');
$query->condition('td.vid', $vocabulary->id());
$query->addTag('vocabulary_node_count');
$count = $query->execute()->fetchField();
$replacements[$original] = $count;
break; break;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment