You are here

HasModeratedContentType.php in Moderation Dashboard 8

Same filename and directory in other branches
  1. 2.0.x src/Plugin/Condition/HasModeratedContentType.php

File

src/Plugin/Condition/HasModeratedContentType.php
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;

/**
 * Provides a 'Has Moderated Content Type' condition.
 *
 * @Condition(
 *   id = "has_moderated_content_type",
 *   label = @Translation("Has Moderated Content Type")
 * )
 */
class HasModeratedContentType extends ConditionPluginBase implements ContainerFactoryPluginInterface {

  /**
   * The moderation information service.
   *
   * @var \Drupal\content_moderation\ModerationInformationInterface
   */
  protected $moderationInformation;

  /**
   * The bundle information service.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $bundleInfo;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * HasModeratedContentType constructor.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\content_moderation\ModerationInformationInterface $moderation_information
   *   The moderation information service.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info
   *   The bundle information service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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'));
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'enable' => FALSE,
    ] + parent::defaultConfiguration();
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::submitConfigurationForm($form, $form_state);
    $this->configuration['enable'] = $form_state
      ->getValue('enable', FALSE);
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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).');
  }

}

Classes

Namesort descending Description
HasModeratedContentType Provides a 'Has Moderated Content Type' condition.