View source
<?php
namespace Drupal\moderation_dashboard\Plugin\Condition;
use Drupal\content_moderation\ModerationInformationInterface;
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class HasModeratedContentType extends ConditionPluginBase implements ContainerFactoryPluginInterface {
protected $moderationInformation;
protected $bundleInfo;
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ModerationInformationInterface $moderation_information, EntityTypeBundleInfoInterface $bundle_info, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->moderationInformation = $moderation_information;
$this->bundleInfo = $bundle_info;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('content_moderation.moderation_information'), $container
->get('entity_type.bundle.info'), $container
->get('entity_type.manager'));
}
public function defaultConfiguration() {
return [
'enable' => FALSE,
] + parent::defaultConfiguration();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['enable'] = [
'#title' => $this
->t('Enable'),
'#type' => 'checkbox',
'#default_value' => $this->configuration['enable'],
'#description' => $this
->t('Leaving this unchecked will bypass this condition.'),
'#weight' => 0,
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['enable'] = $form_state
->getValue('enable', FALSE);
}
public function evaluate() {
if (!$this->configuration['enable']) {
return TRUE;
}
$entity_type = $this->entityTypeManager
->getDefinition('node');
foreach ($this->bundleInfo
->getBundleInfo('node') as $bundle => $info) {
if ($this->moderationInformation
->shouldModerateEntitiesOfBundle($entity_type, $bundle)) {
return TRUE;
}
}
return FALSE;
}
public function summary() {
if ($this
->isNegated()) {
return $this
->t("Site doesn't have moderated content type(s).");
}
return $this
->t('Site has moderated content type(s).');
}
}