Commit 1308c595 authored by alexpott's avatar alexpott

Issue #2322067 by Temoor, ashutoshsngh, roderik, finn.lewis: Replace all...

Issue #2322067 by Temoor, ashutoshsngh, roderik, finn.lewis: Replace all instances of taxonomy_term_load(), entity_load('taxonomy_term') and entity_load_multiple('taxonomy_term') with static method calls.
parent ecb4d367
......@@ -11,6 +11,7 @@
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\field\Tests\FieldUnitTestBase;
use Drupal\taxonomy\Entity\Term;
/**
* Tests the new entity API for the entity reference field type.
......@@ -92,7 +93,7 @@ public function testContentEntityReferenceItem() {
$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);
$term = Term::load($tid);
$this->assertEqual($term->getName(), $new_name);
// Make sure the computed term reflects updates to the term id.
......
......@@ -84,7 +84,7 @@ public function testTaxonomyTerms() {
'parent' => array(4, 5),
),
);
$terms = entity_load_multiple('taxonomy_term', array_keys($expected_results));
$terms = Term::loadMultiple(array_keys($expected_results));
foreach ($expected_results as $tid => $values) {
/** @var Term $term */
$term = $terms[$tid];
......
......@@ -12,6 +12,7 @@
use Drupal\Core\Database\Database;
use Drupal\Core\Language\LanguageInterface;
use Drupal\block\Entity\Block;
use Drupal\taxonomy\Entity\Term;
/**
* Tests the invocation of hooks when creating, inserting, loading, updating or
......@@ -384,7 +385,7 @@ public function testTaxonomyTermHooks() {
));
$GLOBALS['entity_crud_hook_test'] = array();
$term = entity_load('taxonomy_term', $term->id());
$term = Term::load($term->id());
$this->assertHookMessageOrder(array(
'entity_crud_hook_test_entity_load called for type taxonomy_term',
......
......@@ -7,9 +7,14 @@
namespace Drupal\taxonomy\Plugin\Field\FieldWidget;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\taxonomy\Entity\Term;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin implementation of the 'taxonomy_autocomplete' widget.
......@@ -23,7 +28,35 @@
* multiple_values = TRUE
* )
*/
class TaxonomyAutocompleteWidget extends WidgetBase {
class TaxonomyAutocompleteWidget extends WidgetBase implements ContainerFactoryPluginInterface {
/**
* @var EntityStorageInterface
*/
protected $termStorage;
/**
* {@inheritdoc}
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityStorageInterface $term_storage) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->termStorage = $term_storage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$plugin_id,
$plugin_definition,
$configuration['field_definition'],
$configuration['settings'],
$configuration['third_party_settings'],
$container->get('entity.manager')->getStorage('taxonomy_term')
);
}
/**
* {@inheritdoc}
......@@ -74,7 +107,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
$tags = array();
if (!$items->isEmpty()) {
foreach ($items as $item) {
$tags[] = isset($item->entity) ? $item->entity : entity_load('taxonomy_term', $item->target_id);
$tags[] = isset($item->entity) ? $item->entity : $this->termStorage->load($item->target_id);
}
}
$element += array(
......
......@@ -7,9 +7,13 @@
namespace Drupal\taxonomy\Plugin\views\argument;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
use Drupal\Component\Utility\String;
use Drupal\taxonomy\Entity\Term;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Argument handler for taxonomy terms with depth.
......@@ -21,7 +25,28 @@
*
* @ViewsArgument("taxonomy_index_tid_depth")
*/
class IndexTidDepth extends ArgumentPluginBase {
class IndexTidDepth extends ArgumentPluginBase implements ContainerFactoryPluginInterface {
/**
* @var EntityStorageInterface
*/
protected $termStorage;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $termStorage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->termStorage = $termStorage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container->get('entity.manager')->getStorage('taxonomy_term'));
}
protected function defineOptions() {
$options = parent::defineOptions();
......@@ -113,7 +138,7 @@ public function query($group_by = FALSE) {
}
function title() {
$term = entity_load('taxonomy_term', $this->argument);
$term = $this->termStorage->load($this->argument);
if (!empty($term)) {
return String::checkPlain($term->getName());
}
......
......@@ -7,8 +7,11 @@
namespace Drupal\taxonomy\Plugin\views\argument;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\views\Plugin\views\argument\Numeric;
use Drupal\Component\Utility\String;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Argument handler for basic taxonomy tid.
......@@ -17,7 +20,33 @@
*
* @ViewsArgument("taxonomy")
*/
class Taxonomy extends Numeric {
class Taxonomy extends Numeric implements ContainerFactoryPluginInterface {
/**
* @var EntityStorageInterface
*/
protected $termStorage;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $term_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->termStorage = $term_storage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity.manager')->getStorage('taxonomy_term')
);
}
/**
* Override the behavior of title(). Get the title of the node.
......@@ -25,7 +54,7 @@ class Taxonomy extends Numeric {
function title() {
// There might be no valid argument.
if ($this->argument) {
$term = entity_load('taxonomy_term', $this->argument);
$term = $this->termStorage->load($this->argument);
if (!empty($term)) {
return String::checkPlain($term->getName());
}
......
......@@ -7,6 +7,8 @@
namespace Drupal\taxonomy\Tests;
use Drupal\taxonomy\Entity\Term;
/**
* Tests the loading of multiple taxonomy terms at once.
*
......@@ -40,14 +42,14 @@ function testTaxonomyTermMultipleLoad() {
$this->assertEqual($count, 5, format_string('Correct number of terms were loaded. !count terms.', array('!count' => $count)));
// Load the same terms again by tid.
$terms2 = entity_load_multiple('taxonomy_term', array_keys($terms));
$terms2 = Term::loadMultiple(array_keys($terms));
$this->assertEqual($count, count($terms2), 'Five terms were loaded by tid.');
$this->assertEqual($terms, $terms2, 'Both arrays contain the same terms.');
// Remove one term from the array, then delete it.
$deleted = array_shift($terms2);
$deleted->delete();
$deleted_term = entity_load('taxonomy_term', $deleted->id());
$deleted_term = Term::load($deleted->id());
$this->assertFalse($deleted_term);
// Load terms from the vocabulary by vid.
......
......@@ -12,6 +12,7 @@
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\field\Tests\FieldUnitTestBase;
use Drupal\taxonomy\Entity\Term;
/**
* Tests the new entity API for the taxonomy term reference field type.
......@@ -96,7 +97,7 @@ public function testTaxonomyTermReferenceItem() {
$entity->field_test_taxonomy->entity->setName($new_name);
$entity->field_test_taxonomy->entity->save();
// Verify it is the correct name.
$term = entity_load('taxonomy_term', $tid);
$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.
......
......@@ -12,6 +12,7 @@
use Drupal\Component\Utility\Tags;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\taxonomy\Entity\Term;
/**
* Tests load, save and delete for taxonomy terms.
......@@ -84,7 +85,7 @@ function testTaxonomyTermHierarchy() {
$this->assertTrue(isset($parents[$term1->id()]), 'Parent found correctly.');
// Load and save a term, confirming that parents are still set.
$term = entity_load('taxonomy_term', $term2->id());
$term = Term::load($term2->id());
$term->save();
$parents = taxonomy_term_load_parents($term2->id());
$this->assertTrue(isset($parents[$term1->id()]), 'Parent found correctly.');
......@@ -120,7 +121,7 @@ function testTaxonomyTermChildTerms() {
$term = $this->createTerm($this->vocabulary, $edit);
$children = taxonomy_term_load_children($term1->id());
$parents = taxonomy_term_load_parents($term->id());
$terms_array[$x] = taxonomy_term_load($term->id());
$terms_array[$x] = Term::load($term->id());
}
// Get Page 1.
......
......@@ -7,6 +7,8 @@
namespace Drupal\taxonomy\Tests;
use Drupal\taxonomy\Entity\Term;
/**
* Unit tests for taxonomy term functions.
*
......@@ -39,11 +41,14 @@ function testMultipleParentDelete() {
$child_term_id = $child_term->id();
$parent_term1->delete();
$child_term = entity_load('taxonomy_term', $child_term_id, TRUE);
$term_storage = $this->container->get('entity.manager')->getStorage('taxonomy_term');
$term_storage->resetCache(array($child_term_id));
$child_term = Term::load($child_term_id);
$this->assertTrue(!empty($child_term), 'Child term is not deleted if only one of its parents is removed.');
$parent_term2->delete();
$child_term = entity_load('taxonomy_term', $child_term_id, TRUE);
$term_storage->resetCache(array($child_term_id));
$child_term = Term::load($child_term_id);
$this->assertTrue(empty($child_term), 'Child term is deleted if all of its parents are removed.');
}
......
......@@ -267,7 +267,7 @@ function taxonomy_term_view(Term $term, $view_mode = 'full', $langcode = NULL) {
* Constructs a drupal_render() style array from an array of loaded terms.
*
* @param array $terms
* An array of taxonomy terms as returned by entity_load_multiple('taxonomy_term').
* An array of taxonomy terms as returned by Term::loadMultiple().
* @param string $view_mode
* View mode, e.g. 'full', 'teaser'...
* @param string $langcode
......
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