comparison core/modules/forum/src/ForumManager.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents 129ea1e6d783
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
3 namespace Drupal\forum; 3 namespace Drupal\forum;
4 4
5 use Drupal\Core\Config\ConfigFactoryInterface; 5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Database\Connection; 6 use Drupal\Core\Database\Connection;
7 use Drupal\Core\DependencyInjection\DependencySerializationTrait; 7 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
8 use Drupal\Core\Entity\EntityManagerInterface; 8 use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
9 use Drupal\Core\Entity\EntityFieldManagerInterface;
10 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Drupal\Core\Session\AccountInterface; 11 use Drupal\Core\Session\AccountInterface;
10 use Drupal\Core\StringTranslation\TranslationInterface; 12 use Drupal\Core\StringTranslation\TranslationInterface;
11 use Drupal\Core\StringTranslation\StringTranslationTrait; 13 use Drupal\Core\StringTranslation\StringTranslationTrait;
12 use Drupal\comment\CommentManagerInterface; 14 use Drupal\comment\CommentManagerInterface;
13 use Drupal\node\NodeInterface; 15 use Drupal\node\NodeInterface;
19 use StringTranslationTrait; 21 use StringTranslationTrait;
20 use DependencySerializationTrait { 22 use DependencySerializationTrait {
21 __wakeup as defaultWakeup; 23 __wakeup as defaultWakeup;
22 __sleep as defaultSleep; 24 __sleep as defaultSleep;
23 } 25 }
26 use DeprecatedServicePropertyTrait;
27
28 /**
29 * {@inheritdoc}
30 */
31 protected $deprecatedProperties = ['entityManager' => 'entity.manager'];
24 32
25 /** 33 /**
26 * Forum sort order, newest first. 34 * Forum sort order, newest first.
27 */ 35 */
28 const NEWEST_FIRST = 1; 36 const NEWEST_FIRST = 1;
48 * @var \Drupal\Core\Config\ConfigFactoryInterface 56 * @var \Drupal\Core\Config\ConfigFactoryInterface
49 */ 57 */
50 protected $configFactory; 58 protected $configFactory;
51 59
52 /** 60 /**
53 * Entity manager service 61 * Entity field manager.
54 * 62 *
55 * @var \Drupal\Core\Entity\EntityManagerInterface 63 * @var \Drupal\Core\Entity\EntityFieldManagerInterface
56 */ 64 */
57 protected $entityManager; 65 protected $entityFieldManager;
66
67 /**
68 * Entity type manager.
69 *
70 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
71 */
72 protected $entityTypeManager;
58 73
59 /** 74 /**
60 * Database connection 75 * Database connection
61 * 76 *
62 * @var \Drupal\Core\Database\Connection 77 * @var \Drupal\Core\Database\Connection
108 /** 123 /**
109 * Constructs the forum manager service. 124 * Constructs the forum manager service.
110 * 125 *
111 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory 126 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
112 * The config factory service. 127 * The config factory service.
113 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager 128 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
114 * The entity manager service. 129 * The entity type manager.
115 * @param \Drupal\Core\Database\Connection $connection 130 * @param \Drupal\Core\Database\Connection $connection
116 * The current database connection. 131 * The current database connection.
117 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation 132 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
118 * The translation manager service. 133 * The translation manager service.
119 * @param \Drupal\comment\CommentManagerInterface $comment_manager 134 * @param \Drupal\comment\CommentManagerInterface $comment_manager
120 * The comment manager service. 135 * The comment manager service.
121 */ 136 * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
122 public function __construct(ConfigFactoryInterface $config_factory, EntityManagerInterface $entity_manager, Connection $connection, TranslationInterface $string_translation, CommentManagerInterface $comment_manager) { 137 * The entity field manager.
138 */
139 public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, Connection $connection, TranslationInterface $string_translation, CommentManagerInterface $comment_manager, EntityFieldManagerInterface $entity_field_manager = NULL) {
123 $this->configFactory = $config_factory; 140 $this->configFactory = $config_factory;
124 $this->entityManager = $entity_manager; 141 $this->entityTypeManager = $entity_type_manager;
125 $this->connection = $connection; 142 $this->connection = $connection;
126 $this->stringTranslation = $string_translation; 143 $this->stringTranslation = $string_translation;
127 $this->commentManager = $comment_manager; 144 $this->commentManager = $comment_manager;
145 if (!$entity_field_manager) {
146 @trigger_error('The entity_field.manager service must be passed to ForumManager::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
147 $entity_field_manager = \Drupal::service('entity_field.manager');
148 }
149 $this->entityFieldManager = $entity_field_manager;
128 } 150 }
129 151
130 /** 152 /**
131 * {@inheritdoc} 153 * {@inheritdoc}
132 */ 154 */
171 $nids = []; 193 $nids = [];
172 foreach ($result as $record) { 194 foreach ($result as $record) {
173 $nids[] = $record->nid; 195 $nids[] = $record->nid;
174 } 196 }
175 if ($nids) { 197 if ($nids) {
176 $nodes = $this->entityManager->getStorage('node')->loadMultiple($nids); 198 $nodes = $this->entityTypeManager->getStorage('node')->loadMultiple($nids);
177 199
178 $query = $this->connection->select('node_field_data', 'n') 200 $query = $this->connection->select('node_field_data', 'n')
179 ->extend('Drupal\Core\Database\Query\TableSortExtender'); 201 ->extend('Drupal\Core\Database\Query\TableSortExtender');
180 $query->fields('n', ['nid']); 202 $query->fields('n', ['nid']);
181 203
398 public function getChildren($vid, $tid) { 420 public function getChildren($vid, $tid) {
399 if (!empty($this->forumChildren[$tid])) { 421 if (!empty($this->forumChildren[$tid])) {
400 return $this->forumChildren[$tid]; 422 return $this->forumChildren[$tid];
401 } 423 }
402 $forums = []; 424 $forums = [];
403 $_forums = $this->entityManager->getStorage('taxonomy_term')->loadTree($vid, $tid, NULL, TRUE); 425 $_forums = $this->entityTypeManager->getStorage('taxonomy_term')->loadTree($vid, $tid, NULL, TRUE);
404 foreach ($_forums as $forum) { 426 foreach ($_forums as $forum) {
405 // Merge in the topic and post counters. 427 // Merge in the topic and post counters.
406 if (($count = $this->getForumStatistics($forum->id()))) { 428 if (($count = $this->getForumStatistics($forum->id()))) {
407 $forum->num_topics = $count->topic_count; 429 $forum->num_topics = $count->topic_count;
408 $forum->num_posts = $count->topic_count + $count->comment_count; 430 $forum->num_posts = $count->topic_count + $count->comment_count;
428 if ($this->index) { 450 if ($this->index) {
429 return $this->index; 451 return $this->index;
430 } 452 }
431 453
432 $vid = $this->configFactory->get('forum.settings')->get('vocabulary'); 454 $vid = $this->configFactory->get('forum.settings')->get('vocabulary');
433 $index = $this->entityManager->getStorage('taxonomy_term')->create([ 455 $index = $this->entityTypeManager->getStorage('taxonomy_term')->create([
434 'tid' => 0, 456 'tid' => 0,
435 'container' => 1, 457 'container' => 1,
436 'parents' => [], 458 'parents' => [],
437 'isIndex' => TRUE, 459 'isIndex' => TRUE,
438 'vid' => $vid, 460 'vid' => $vid,
456 478
457 /** 479 /**
458 * {@inheritdoc} 480 * {@inheritdoc}
459 */ 481 */
460 public function getParents($tid) { 482 public function getParents($tid) {
461 return $this->entityManager->getStorage('taxonomy_term')->loadAllParents($tid); 483 return $this->entityTypeManager->getStorage('taxonomy_term')->loadAllParents($tid);
462 } 484 }
463 485
464 /** 486 /**
465 * {@inheritdoc} 487 * {@inheritdoc}
466 */ 488 */
467 public function checkNodeType(NodeInterface $node) { 489 public function checkNodeType(NodeInterface $node) {
468 // Fetch information about the forum field. 490 // Fetch information about the forum field.
469 $field_definitions = $this->entityManager->getFieldDefinitions('node', $node->bundle()); 491 $field_definitions = $this->entityFieldManager->getFieldDefinitions('node', $node->bundle());
470 return !empty($field_definitions['taxonomy_forums']); 492 return !empty($field_definitions['taxonomy_forums']);
471 } 493 }
472 494
473 /** 495 /**
474 * {@inheritdoc} 496 * {@inheritdoc}