Commit ba509afb authored by larowlan's avatar larowlan

Issue #3047866 by mikelutz, Berdir, TR, alexpott, andypost, Satyanarayan...

Issue #3047866 by mikelutz, Berdir, TR, alexpott, andypost, Satyanarayan Reddy: Remove usage of deprecated \Drupal::entityManager() in core
parent 5ad7fe32
......@@ -1914,7 +1914,7 @@ function hook_cron() {
// Long-running operation example, leveraging a queue:
// Queue news feeds for updates once their refresh interval has elapsed.
$queue = \Drupal::queue('aggregator_feeds');
$ids = \Drupal::entityManager()->getStorage('aggregator_feed')->getFeedIdsToRefresh();
$ids = \Drupal::entityTypeManager()->getStorage('aggregator_feed')->getFeedIdsToRefresh();
foreach (Feed::loadMultiple($ids) as $feed) {
if ($queue->createItem($feed)) {
// Add timestamp to avoid queueing item more than once.
......
......@@ -1621,7 +1621,7 @@ function install_profile_modules(&$install_state) {
*/
function install_core_entity_type_definitions() {
$update_manager = \Drupal::entityDefinitionUpdateManager();
foreach (\Drupal::entityManager()->getDefinitions() as $entity_type) {
foreach (\Drupal::entityTypeManager()->getDefinitions() as $entity_type) {
if ($entity_type->getProvider() == 'core') {
$update_manager->installEntityType($entity_type);
}
......
......@@ -270,6 +270,7 @@ public static function currentUser() {
* correct interface or service.
*/
public static function entityManager() {
@trigger_error("\Drupal::entityManager() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager() instead in most cases. If the needed method is not on \Drupal\Core\Entity\EntityTypeManagerInterface, see the deprecated \Drupal\Core\Entity\EntityManager to find the correct interface or service. See https://www.drupal.org/node/2549139", E_USER_DEPRECATED);
return static::getContainer()->get('entity.manager');
}
......
......@@ -96,7 +96,7 @@ public static function collectRenderDisplay(FieldableEntityInterface $entity, $f
->execute();
// Load the first valid candidate display, if any.
$storage = \Drupal::entityManager()->getStorage('entity_form_display');
$storage = \Drupal::entityTypeManager()->getStorage('entity_form_display');
foreach ($candidate_ids as $candidate_id) {
if (isset($results[$candidate_id])) {
$display = $storage->load($candidate_id);
......
......@@ -112,7 +112,7 @@ public static function collectRenderDisplays($entities, $view_mode) {
}
// Load the selected displays.
$storage = \Drupal::entityManager()->getStorage('entity_view_display');
$storage = \Drupal::entityTypeManager()->getStorage('entity_view_display');
$displays = $storage->loadMultiple($load_ids);
$displays_by_bundle = [];
......@@ -182,8 +182,8 @@ public function __construct(array $values, $entity_type) {
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
// Reset the render cache for the target entity type.
parent::postSave($storage, $update);
if (\Drupal::entityManager()->hasHandler($this->targetEntityType, 'view_builder')) {
\Drupal::entityManager()->getViewBuilder($this->targetEntityType)->resetCache();
if (\Drupal::entityTypeManager()->hasHandler($this->targetEntityType, 'view_builder')) {
\Drupal::entityTypeManager()->getViewBuilder($this->targetEntityType)->resetCache();
}
}
......
......@@ -86,7 +86,7 @@ public function setTargetType($target_entity_type) {
*/
public function calculateDependencies() {
parent::calculateDependencies();
$target_entity_type = \Drupal::entityManager()->getDefinition($this->targetEntityType);
$target_entity_type = \Drupal::entityTypeManager()->getDefinition($this->targetEntityType);
$this->addDependency('module', $target_entity_type->getProvider());
return $this;
}
......
......@@ -22,7 +22,7 @@ abstract class EntityStorageBase extends EntityHandlerBase implements EntityStor
*
* The following code returns the same object:
* @code
* \Drupal::entityManager()->getDefinition($this->entityTypeId)
* \Drupal::entityTypeManager()->getDefinition($this->entityTypeId)
* @endcode
*
* @var \Drupal\Core\Entity\EntityTypeInterface
......
......@@ -910,7 +910,7 @@ public function getBundleConfigDependency($bundle) {
// If this entity type uses entities to manage its bundles then depend on
// the bundle entity.
if ($bundle_entity_type_id = $this->getBundleEntityType()) {
if (!$bundle_entity = \Drupal::entityManager()->getStorage($bundle_entity_type_id)->load($bundle)) {
if (!$bundle_entity = \Drupal::entityTypeManager()->getStorage($bundle_entity_type_id)->load($bundle)) {
throw new \LogicException(sprintf('Missing bundle entity, entity type %s, entity id %s.', $bundle_entity_type_id, $bundle));
}
$config_dependency = [
......
......@@ -232,7 +232,7 @@ public function getFormObject($entity_type_id, $operation) {
->setOperation($operation)
// The entity manager cannot be injected due to a circular dependency.
// @todo Remove this set call in https://www.drupal.org/node/2603542.
->setEntityManager(\Drupal::entityManager());
->setEntityManager(\Drupal::service('entity.manager'));
}
/**
......
......@@ -17,7 +17,7 @@ public function validate($entity, Constraint $constraint) {
if (isset($entity)) {
/** @var \Drupal\Core\Entity\EntityInterface $entity */
if (!$entity->isNew()) {
$saved_entity = \Drupal::entityManager()->getStorage($entity->getEntityTypeId())->loadUnchanged($entity->id());
$saved_entity = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())->loadUnchanged($entity->id());
// Ensure that all the entity translations are the same as or newer
// than their current version in the storage in order to avoid
// reverting other changes. In fact the entity object that is being
......
......@@ -29,7 +29,7 @@ public function validate($value, Constraint $constraint) {
$entity = $value->getEntity();
$check_permission = TRUE;
if (!$entity->isNew()) {
$existing_entity = \Drupal::entityManager()->getStorage($entity->getEntityTypeId())->loadUnchanged($entity->id());
$existing_entity = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())->loadUnchanged($entity->id());
$referenced_entities = $existing_entity->{$value->getFieldDefinition()->getName()}->referencedEntities();
// Check permission if we are not already referencing the entity.
foreach ($referenced_entities as $ref) {
......
......@@ -70,7 +70,7 @@ public function getPropertyDefinitions() {
if (!isset($this->propertyDefinitions)) {
if ($entity_type_id = $this->getEntityTypeId()) {
// Return an empty array for entities that are not content entities.
$entity_type_class = \Drupal::entityManager()->getDefinition($entity_type_id)->getClass();
$entity_type_class = \Drupal::entityTypeManager()->getDefinition($entity_type_id)->getClass();
if (!in_array('Drupal\Core\Entity\FieldableEntityInterface', class_implements($entity_type_class))) {
$this->propertyDefinitions = [];
}
......
......@@ -580,7 +580,7 @@
* implementing \Drupal\Core\Entity\EntityViewBuilderInterface that you can
* retrieve with:
* @code
* $view_builder = \Drupal::entityManager()->getViewBuilder('your_entity_type');
* $view_builder = \Drupal::entityTypeManager()->getViewBuilder('your_entity_type');
* // Or if you have a $container variable:
* $view_builder = $container->get('entity.manager')->getViewBuilder('your_entity_type');
* @endcode
......@@ -1943,7 +1943,7 @@ function hook_entity_bundle_field_info_alter(&$fields, \Drupal\Core\Entity\Entit
* @see \Drupal\Core\Entity\EntityManagerInterface::getFieldStorageDefinitions()
*/
function hook_entity_field_storage_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type) {
if (\Drupal::entityManager()->getStorage($entity_type->id()) instanceof DynamicallyFieldableEntityStorageInterface) {
if (\Drupal::entityTypeManager()->getStorage($entity_type->id()) instanceof DynamicallyFieldableEntityStorageInterface) {
// Query by filtering on the ID as this is more efficient than filtering
// on the entity_type property directly.
$ids = \Drupal::entityQuery('field_storage_config')
......
......@@ -729,7 +729,7 @@ function hook_post_update_NAME(&$sandbox) {
$block_update_8001 = \Drupal::keyValue('update_backup')->get('block_update_8001', []);
$block_ids = array_keys($block_update_8001);
$block_storage = \Drupal::entityManager()->getStorage('block');
$block_storage = \Drupal::entityTypeManager()->getStorage('block');
$blocks = $block_storage->loadMultiple($block_ids);
/** @var $blocks \Drupal\block\BlockInterface[] */
foreach ($blocks as $block) {
......
......@@ -64,7 +64,7 @@ public static function createFromBaseFieldDefinition(BaseFieldDefinition $base_f
$values = $base_field_definition->toArray();
$values['bundle'] = $bundle;
$values['baseFieldDefinition'] = $base_field_definition;
return \Drupal::entityManager()->getStorage('base_field_override')->create($values);
return \Drupal::entityTypeManager()->getStorage('base_field_override')->create($values);
}
/**
......@@ -238,7 +238,7 @@ public static function postDelete(EntityStorageInterface $storage, array $field_
* provided field name, otherwise NULL.
*/
public static function loadByName($entity_type_id, $bundle, $field_name) {
return \Drupal::entityManager()->getStorage('base_field_override')->load($entity_type_id . '.' . $bundle . '.' . $field_name);
return \Drupal::entityTypeManager()->getStorage('base_field_override')->load($entity_type_id . '.' . $bundle . '.' . $field_name);
}
/**
......
......@@ -43,7 +43,7 @@ public function referencedEntities() {
// Load and add the existing entities.
if ($ids) {
$target_type = $this->getFieldDefinition()->getSetting('target_type');
$entities = \Drupal::entityManager()->getStorage($target_type)->loadMultiple($ids);
$entities = \Drupal::entityTypeManager()->getStorage($target_type)->loadMultiple($ids);
foreach ($ids as $delta => $target_id) {
if (isset($entities[$target_id])) {
$target_entities[$delta] = $entities[$target_id];
......@@ -75,7 +75,7 @@ public static function processDefaultValue($default_value, FieldableEntityInterf
$entity_ids = \Drupal::entityQuery($target_type)
->condition('uuid', $uuids, 'IN')
->execute();
$entities = \Drupal::entityManager()
$entities = \Drupal::entityTypeManager()
->getStorage($target_type)
->loadMultiple($entity_ids);
......@@ -117,7 +117,7 @@ public function defaultValuesFormSubmit(array $element, array &$form, FormStateI
}
$ids[] = $default_value[$delta]['target_id'];
}
$entities = \Drupal::entityManager()
$entities = \Drupal::entityTypeManager()
->getStorage($this->getSetting('target_type'))
->loadMultiple($ids);
......
......@@ -184,7 +184,7 @@ public function __unset($name) {
* {@inheritdoc}
*/
public function view($display_options = []) {
$view_builder = \Drupal::entityManager()->getViewBuilder($this->getEntity()->getEntityTypeId());
$view_builder = \Drupal::entityTypeManager()->getViewBuilder($this->getEntity()->getEntityTypeId());
return $view_builder->viewFieldItem($this, $display_options);
}
......
......@@ -150,7 +150,7 @@ public function __unset($property_name) {
* {@inheritdoc}
*/
public function access($operation = 'view', AccountInterface $account = NULL, $return_as_object = FALSE) {
$access_control_handler = \Drupal::entityManager()->getAccessControlHandler($this->getEntity()->getEntityTypeId());
$access_control_handler = \Drupal::entityTypeManager()->getAccessControlHandler($this->getEntity()->getEntityTypeId());
return $access_control_handler->fieldAccess($operation, $this->getFieldDefinition(), $account, $this, $return_as_object);
}
......@@ -239,7 +239,7 @@ protected function delegateMethod($method) {
* {@inheritdoc}
*/
public function view($display_options = []) {
$view_builder = \Drupal::entityManager()->getViewBuilder($this->getEntity()->getEntityTypeId());
$view_builder = \Drupal::entityTypeManager()->getViewBuilder($this->getEntity()->getEntityTypeId());
return $view_builder->viewField($this, $display_options);
}
......
......@@ -213,7 +213,7 @@ public static function isApplicable(FieldDefinitionInterface $field_definition)
// This formatter is only available for entity types that have a view
// builder.
$target_type = $field_definition->getFieldStorageDefinition()->getSetting('target_type');
return \Drupal::entityManager()->getDefinition($target_type)->hasViewBuilderClass();
return \Drupal::entityTypeManager()->getDefinition($target_type)->hasViewBuilderClass();
}
}
......@@ -136,7 +136,7 @@ public function prepareView(array $entities_items) {
}
if ($ids) {
$target_type = $this->getFieldSetting('target_type');
$target_entities = \Drupal::entityManager()->getStorage($target_type)->loadMultiple($ids);
$target_entities = \Drupal::entityTypeManager()->getStorage($target_type)->loadMultiple($ids);
}
// For each item, pre-populate the loaded entity in $item->entity, and set
......
......@@ -400,7 +400,7 @@ function hook_contextual_links_alter(array &$links, $group, array $route_paramet
if ($group == 'menu') {
// Dynamically use the menu name for the title of the menu_edit contextual
// link.
$menu = \Drupal::entityManager()->getStorage('menu')->load($route_parameters['menu']);
$menu = \Drupal::entityTypeManager()->getStorage('menu')->load($route_parameters['menu']);
$links['menu_edit']['title'] = t('Edit menu: @label', ['@label' => $menu->label()]);
}
}
......
......@@ -195,7 +195,7 @@ public function setInitialAccountId($account_id) {
* An account or NULL if none is found.
*/
protected function loadUserEntity($account_id) {
return \Drupal::entityManager()->getStorage('user')->load($account_id);
return \Drupal::entityTypeManager()->getStorage('user')->load($account_id);
}
}
......@@ -203,7 +203,7 @@ public function getLastAccessedTime() {
* The role storage object.
*/
protected function getRoleStorage() {
return \Drupal::entityManager()->getStorage('user_role');
return \Drupal::entityTypeManager()->getStorage('user_role');
}
}
......@@ -143,7 +143,7 @@ function aggregator_entity_extra_field_info() {
function aggregator_cron() {
$queue = \Drupal::queue('aggregator_feeds');
$ids = \Drupal::entityManager()->getStorage('aggregator_feed')->getFeedIdsToRefresh();
$ids = \Drupal::entityTypeManager()->getStorage('aggregator_feed')->getFeedIdsToRefresh();
foreach (Feed::loadMultiple($ids) as $feed) {
if ($queue->createItem($feed)) {
// Add timestamp to avoid queueing item more than once.
......
......@@ -125,7 +125,7 @@ public static function postDelete(EntityStorageInterface $storage, array $entiti
->condition('settings.feed', array_keys($entities))
->execute();
if ($ids) {
$block_storage = \Drupal::entityManager()->getStorage('block');
$block_storage = \Drupal::entityTypeManager()->getStorage('block');
$block_storage->delete($block_storage->loadMultiple($ids));
}
}
......
......@@ -57,7 +57,7 @@ public function testPostProcess() {
$this->updateFeedItems($feed);
$feed_id = $feed->id();
// Reset entity cache manually.
\Drupal::entityManager()->getStorage('aggregator_feed')->resetCache([$feed_id]);
\Drupal::entityTypeManager()->getStorage('aggregator_feed')->resetCache([$feed_id]);
// Reload the feed to get new values.
$feed = Feed::load($feed_id);
// Make sure its refresh rate doubled.
......
......@@ -30,7 +30,7 @@ function block_post_update_disable_blocks_with_missing_contexts() {
$block_update_8001 = \Drupal::keyValue('update_backup')->get('block_update_8001', []);
$block_ids = array_keys($block_update_8001);
$block_storage = \Drupal::entityManager()->getStorage('block');
$block_storage = \Drupal::entityTypeManager()->getStorage('block');
$blocks = $block_storage->loadMultiple($block_ids);
/** @var $blocks \Drupal\block\BlockInterface[] */
foreach ($blocks as $block) {
......
......@@ -132,7 +132,7 @@ protected function doTestBasicTranslation() {
// as in the original language.
$default_langcode = $this->langcodes[0];
$values = $this->getNewEntityValues($default_langcode);
$storage = \Drupal::entityManager()->getStorage($this->entityTypeId);
$storage = \Drupal::entityTypeManager()->getStorage($this->entityTypeId);
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $storage->create(['type' => 'basic'] + $values);
$entity->save();
......
......@@ -57,7 +57,7 @@ public function testPageEdit() {
$this->drupalPostForm(NULL, $edit, t('Save'));
// Ensure that the block revision has been created.
\Drupal::entityManager()->getStorage('block_content')->resetCache([$block->id()]);
\Drupal::entityTypeManager()->getStorage('block_content')->resetCache([$block->id()]);
$revised_block = BlockContent::load($block->id());
$this->assertNotIdentical($block->getRevisionId(), $revised_block->getRevisionId(), 'A new revision has been created.');
......
......@@ -96,7 +96,7 @@ function book_node_links_alter(array &$links, NodeInterface $node, array &$conte
if (isset($node->book['depth'])) {
if ($context['view_mode'] == 'full' && node_is_page($node)) {
$child_type = \Drupal::config('book.settings')->get('child_type');
$access_control_handler = \Drupal::entityManager()->getAccessControlHandler('node');
$access_control_handler = \Drupal::entityTypeManager()->getAccessControlHandler('node');
if (($account->hasPermission('add content to books') || $account->hasPermission('administer book outlines')) && $access_control_handler->createAccess($child_type) && $node->isPublished() && $node->book['depth'] < BookManager::BOOK_MAX_DEPTH) {
$book_links['book_add_child'] = [
'title' => t('Add child page'),
......
......@@ -118,7 +118,7 @@ protected function createBookNode($book_nid, $parent = NULL) {
$edit['book[pid]'] = $parent;
$this->drupalPostForm(NULL, $edit, t('Save'));
// Make sure the parent was flagged as having children.
$parent_node = \Drupal::entityManager()->getStorage('node')->loadUnchanged($parent);
$parent_node = \Drupal::entityTypeManager()->getStorage('node')->loadUnchanged($parent);
$this->assertFalse(empty($parent_node->book['has_children']), 'Parent node is marked as having children');
}
else {
......
......@@ -405,7 +405,7 @@ public function testBookOutline() {
$edit = [];
$edit['book[bid]'] = '1';
$this->drupalPostForm('node/' . $empty_book->id() . '/outline', $edit, t('Add to book outline'));
$node = \Drupal::entityManager()->getStorage('node')->load($empty_book->id());
$node = \Drupal::entityTypeManager()->getStorage('node')->load($empty_book->id());
// Test the book array.
$this->assertEqual($node->book['nid'], $empty_book->id());
$this->assertEqual($node->book['bid'], $empty_book->id());
......@@ -428,7 +428,7 @@ public function testBookOutline() {
$edit = [];
$edit['book[bid]'] = $node->id();
$this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
$node = \Drupal::entityManager()->getStorage('node')->load($node->id());
$node = \Drupal::entityTypeManager()->getStorage('node')->load($node->id());
// Test the book array.
$this->assertEqual($node->book['nid'], $node->id());
......@@ -519,7 +519,7 @@ public function testHookNodeLoadAccess() {
$this->drupalLogin($this->bookAuthor);
$this->book = $this->createBookNode('new');
// Reset any internal static caching.
$node_storage = \Drupal::entityManager()->getStorage('node');
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
$node_storage->resetCache();
// Log in as user without access to the book node, so no 'node test view'
......
......@@ -198,7 +198,7 @@ public function createBookNode($book_nid, $parent = NULL, $edit = []) {
$edit['book[pid]'] = $parent;
$this->drupalPostForm(NULL, $edit, t('Save'));
// Make sure the parent was flagged as having children.
$parent_node = \Drupal::entityManager()->getStorage('node')->loadUnchanged($parent);
$parent_node = \Drupal::entityTypeManager()->getStorage('node')->loadUnchanged($parent);
$this->assertFalse(empty($parent_node->book['has_children']), 'Parent node is marked as having children');
}
else {
......
......@@ -121,7 +121,7 @@ protected function createBookNode($book_nid, $parent = NULL) {
$edit['book[pid]'] = $parent;
$this->drupalPostForm(NULL, $edit, t('Save'));
// Make sure the parent was flagged as having children.
$parent_node = \Drupal::entityManager()->getStorage('node')->loadUnchanged($parent);
$parent_node = \Drupal::entityTypeManager()->getStorage('node')->loadUnchanged($parent);
$this->assertFalse(empty($parent_node->book['has_children']), 'Parent node is marked as having children');
}
else {
......
......@@ -177,7 +177,7 @@ function comment_field_config_update(FieldConfigInterface $field) {
if ($field->getType() == 'comment') {
// Comment field settings also affects the rendering of *comment* entities,
// not only the *commented* entities.
\Drupal::entityManager()->getViewBuilder('comment')->resetCache();
\Drupal::entityTypeManager()->getViewBuilder('comment')->resetCache();
}
}
......@@ -357,7 +357,7 @@ function comment_form_field_ui_display_overview_form_alter(&$form, FormStateInte
*/
function comment_entity_storage_load($entities, $entity_type) {
// Comments can only be attached to content entities, so skip others.
if (!\Drupal::entityManager()->getDefinition($entity_type)->entityClassImplements(FieldableEntityInterface::class)) {
if (!\Drupal::entityTypeManager()->getDefinition($entity_type)->entityClassImplements(FieldableEntityInterface::class)) {
return;
}
if (!\Drupal::service('comment.manager')->getFields($entity_type)) {
......@@ -426,7 +426,7 @@ function comment_entity_predelete(EntityInterface $entity) {
* FALSE otherwise.
*/
function _comment_entity_uses_integer_id($entity_type_id) {
$entity_type = \Drupal::entityManager()->getDefinition($entity_type_id);
$entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
$entity_type_id_key = $entity_type->getKey('id');
if ($entity_type_id_key === FALSE) {
return FALSE;
......@@ -453,7 +453,7 @@ function comment_node_update_index(EntityInterface $node) {
// edit could change the security situation so it is not safe to index the
// comments.
$index_comments = TRUE;
$roles = \Drupal::entityManager()->getStorage('user_role')->loadMultiple();
$roles = \Drupal::entityTypeManager()->getStorage('user_role')->loadMultiple();
$authenticated_can_access = $roles[RoleInterface::AUTHENTICATED_ID]->hasPermission('access comments');
foreach ($roles as $rid => $role) {
if ($role->hasPermission('search content') && !$role->hasPermission('access comments')) {
......@@ -477,10 +477,10 @@ function comment_node_update_index(EntityInterface $node) {
$mode = $field_definition->getSetting('default_mode');
$comments_per_page = $field_definition->getSetting('per_page');
if ($node->get($field_name)->status) {
$comments = \Drupal::entityManager()->getStorage('comment')
$comments = \Drupal::entityTypeManager()->getStorage('comment')
->loadThread($node, $field_name, $mode, $comments_per_page);
if ($comments) {
$build[] = \Drupal::entityManager()->getViewBuilder('comment')->viewMultiple($comments);
$build[] = \Drupal::entityTypeManager()->getViewBuilder('comment')->viewMultiple($comments);
}
}
}
......
......@@ -23,7 +23,7 @@ function comment_views_data_alter(&$data) {
];
// Provide a integration for each entity type except comment.
foreach (\Drupal::entityManager()->getDefinitions() as $entity_type_id => $entity_type) {
foreach (\Drupal::entityTypeManager()->getDefinitions() as $entity_type_id => $entity_type) {
if ($entity_type_id == 'comment' || !$entity_type->entityClassImplements(ContentEntityInterface::class) || !$entity_type->getBaseTable()) {
continue;
}
......
......@@ -193,7 +193,7 @@ public function getViewsData() {
unset($data['comment_field_data']['thread']['filter']);
unset($data['comment_field_data']['thread']['argument']);
$entities_types = \Drupal::entityManager()->getDefinitions();
$entities_types = \Drupal::entityTypeManager()->getDefinitions();
// Provide a relationship for each entity type except comment.
foreach ($entities_types as $type => $entity_type) {
......
......@@ -4,6 +4,8 @@
use Drupal\Core\Database\Connection;
use Drupal\comment\CommentInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\Entity\Node;
use Drupal\views\Plugin\views\field\NumericField;
......@@ -35,6 +37,20 @@ public function usesGroupBy() {
*/
protected $database;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* Constructs a \Drupal\comment\Plugin\views\field\NodeNewComments object.
*
......@@ -46,18 +62,38 @@ public function usesGroupBy() {
* The plugin implementation definition.
* @param \Drupal\Core\Database\Connection $database
* Database Service Object.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database, EntityTypeManagerInterface $entity_type_manager = NULL, EntityFieldManagerInterface $entity_field_manager = NULL) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->database = $database;
if (!$entity_type_manager) {
@trigger_error("Not passing the entity type manager to the NodeNewComments constructor is deprecated in drupal:8.8.0 and will be required in drupal 9.0.0. @see https://www.drupal.org/node/3047897");
$entity_type_manager = \Drupal::entityTypeManager();
}
if (!$entity_field_manager) {
@trigger_error("Not passing the entity type manager to the NodeNewComments constructor is deprecated in drupal:8.8.0 and will be required in drupal 9.0.0. @see https://www.drupal.org/node/3047897");
$entity_field_manager = \Drupal::service('entity_field.manager');
}
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container->get('database'));
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('database'),
$container->get('entity_type.manager'),
$container->get('entity_field.manager')
);
}
/**
......@@ -166,8 +202,7 @@ protected function renderLink($data, ResultRow $values) {
// reference, we arbitrarily use the first such field name we find.
// @todo Provide a means for selecting the comment field.
// https://www.drupal.org/node/2594201
$entity_type_manager = \Drupal::entityTypeManager();
$field_map = \Drupal::service('entity_field.manager')->getFieldMapByFieldType('comment');
$field_map = $this->entityFieldManager->getFieldMapByFieldType('comment');
$comment_field_name = 'comment';
foreach ($field_map['node'] as $field_name => $field_data) {
foreach ($field_data['bundles'] as $bundle_name) {
......@@ -177,7 +212,7 @@ protected function renderLink($data, ResultRow $values) {
}
}
}
$page_number = $entity_type_manager->getStorage('comment')
$page_number = $this->entityTypeManager->getStorage('comment')
->getNewCommentPageNumber($this->getValue($values, 'comment_count'), $this->getValue($values), $node, $comment_field_name);
$this->options['alter']['make_link'] = TRUE;
$this->options['alter']['url'] = $node->toUrl();
......
......@@ -176,7 +176,7 @@ public function postComment($entity, $comment, $subject = '', $contact = NULL, $
}
if (isset($match[1])) {
\Drupal::entityManager()->getStorage('comment')->resetCache([$match[1]]);
\Drupal::entityTypeManager()->getStorage('comment')->resetCache([$match[1]]);
return Comment::load($match[1]);
}
}
......
......@@ -283,7 +283,7 @@ public function testCommentNewPageIndicator() {
$node = Node::load($node->id());
foreach ($expected_pages as $new_replies => $expected_page) {
$returned_page = \Drupal::entityManager()->getStorage('comment')
$returned_page = \Drupal::entityTypeManager()->getStorage('comment')
->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node, 'comment');
$this->assertIdentical($expected_page, $returned_page, format_string('Flat mode, @new replies: expected page @expected, returned page @returned.', ['@new' => $new_replies, '@expected' => $expected_page, '@returned' => $returned_page]));
}
......@@ -305,10 +305,10 @@ public function testCommentNewPageIndicator() {
6 => 0,
];
\Drupal::entityManager()->getStorage('node')->resetCache([$node->id()]);
\Drupal::entityTypeManager()->getStorage('node')->resetCache([$node->id()]);
$node = Node::load($node->id());
foreach ($expected_pages as $new_replies => $expected_page) {
$returned_page = \Drupal::entityManager()->getStorage('comment')
$returned_page = \Drupal::entityTypeManager()->getStorage('comment')
->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node, 'comment');
$this->assertEqual($expected_page, $returned_page, format_string('Threaded mode, @new replies: expected page @expected, returned page @returned.', ['@new' => $new_replies, '@expected' => $expected_page, '@returned' => $returned_page]));
}
......