Skip to content

Commit 9e9d6d2

Browse files
Inject optional service (#6491)
1 parent a79a8a5 commit 9e9d6d2

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/Commands/entity/EntitySaveCommand.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Drupal\Component\Datetime\TimeInterface;
99
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
1010
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
11+
use Drupal\content_moderation\ModerationInformationInterface;
1112
use Drupal\Core\Entity\ContentEntityInterface;
1213
use Drupal\Core\Entity\EntityChangedInterface;
1314
use Drupal\Core\Entity\EntityPublishedInterface;
@@ -43,7 +44,8 @@ final class EntitySaveCommand extends Command
4344
public function __construct(
4445
protected readonly EntityTypeManagerInterface $entityTypeManager,
4546
protected readonly TimeInterface $time,
46-
protected readonly AccountInterface $currentUser
47+
protected readonly AccountInterface $currentUser,
48+
protected readonly ?ModerationInformationInterface $moderationInformation = null
4749
) {
4850
parent::__construct();
4951
}
@@ -146,9 +148,8 @@ public function doSave(string $entity_type, array $ids, ?string $action, ?string
146148
$entity = $storage->createRevision($entity, true);
147149
}
148150
if ($state) {
149-
// AutowireTrait does not support optional params so can't use DI.
150-
$moderationInformation = \Drupal::service('content_moderation.moderation_information');
151-
if (!$moderationInformation->isModeratedEntity($entity)) {
151+
assert($this->moderationInformation instanceof ModerationInformationInterface);
152+
if (!$this->moderationInformation->isModeratedEntity($entity)) {
152153
throw new \InvalidArgumentException(dt('!bundle !id does not support content moderation.', ['!bundle' => $entity->bundle(), '!id' => $entity->id()]));
153154
}
154155

0 commit comments

Comments
 (0)