ModerationStateFilter.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. namespace Drupal\content_moderation\Plugin\views\filter;
  3. use Drupal\content_moderation\Plugin\views\ModerationStateJoinViewsHandlerTrait;
  4. use Drupal\Core\Cache\Cache;
  5. use Drupal\Core\Database\Query\Condition;
  6. use Drupal\Core\Entity\EntityStorageInterface;
  7. use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
  8. use Drupal\Core\Entity\EntityTypeManagerInterface;
  9. use Drupal\views\Plugin\DependentWithRemovalPluginInterface;
  10. use Drupal\views\Plugin\views\filter\InOperator;
  11. use Drupal\views\Views;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. /**
  14. * Provides a filter for the moderation state of an entity.
  15. *
  16. * @ingroup views_filter_handlers
  17. *
  18. * @ViewsFilter("moderation_state_filter")
  19. */
  20. class ModerationStateFilter extends InOperator implements DependentWithRemovalPluginInterface {
  21. use ModerationStateJoinViewsHandlerTrait;
  22. /**
  23. * {@inheritdoc}
  24. */
  25. protected $valueFormType = 'select';
  26. /**
  27. * The entity type manager.
  28. *
  29. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  30. */
  31. protected $entityTypeManager;
  32. /**
  33. * The bundle information service.
  34. *
  35. * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
  36. */
  37. protected $bundleInfo;
  38. /**
  39. * The storage handler of the workflow entity type.
  40. *
  41. * @var \Drupal\Core\Entity\EntityStorageInterface
  42. */
  43. protected $workflowStorage;
  44. /**
  45. * Creates an instance of ModerationStateFilter.
  46. */
  47. public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $bundle_info, EntityStorageInterface $workflow_storage) {
  48. parent::__construct($configuration, $plugin_id, $plugin_definition);
  49. $this->entityTypeManager = $entity_type_manager;
  50. $this->bundleInfo = $bundle_info;
  51. $this->workflowStorage = $workflow_storage;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  57. return new static(
  58. $configuration,
  59. $plugin_id,
  60. $plugin_definition,
  61. $container->get('entity_type.manager'),
  62. $container->get('entity_type.bundle.info'),
  63. $container->get('entity_type.manager')->getStorage('workflow')
  64. );
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function getCacheTags() {
  70. return Cache::mergeTags(parent::getCacheTags(), $this->entityTypeManager->getDefinition('workflow')->getListCacheTags());
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function getCacheContexts() {
  76. return Cache::mergeContexts(parent::getCacheContexts(), $this->entityTypeManager->getDefinition('workflow')->getListCacheContexts());
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function getValueOptions() {
  82. if (isset($this->valueOptions)) {
  83. return $this->valueOptions;
  84. }
  85. $this->valueOptions = [];
  86. // Find all workflows which are moderating entity types of the same type the
  87. // view is displaying.
  88. foreach ($this->workflowStorage->loadByProperties(['type' => 'content_moderation']) as $workflow) {
  89. /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModerationInterface $workflow_type */
  90. $workflow_type = $workflow->getTypePlugin();
  91. if (in_array($this->getEntityType(), $workflow_type->getEntityTypes(), TRUE)) {
  92. foreach ($workflow_type->getStates() as $state_id => $state) {
  93. $this->valueOptions[$workflow->label()][implode('-', [$workflow->id(), $state_id])] = $state->label();
  94. }
  95. }
  96. }
  97. return $this->valueOptions;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. protected function opSimple() {
  103. if (empty($this->value)) {
  104. return;
  105. }
  106. $this->ensureMyTable();
  107. $entity_type = $this->entityTypeManager->getDefinition($this->getEntityType());
  108. $bundle_condition = NULL;
  109. if ($entity_type->hasKey('bundle')) {
  110. // Get a list of bundles that are being moderated by the workflows
  111. // configured in this filter.
  112. $workflow_ids = $this->getWorkflowIds();
  113. $moderated_bundles = [];
  114. foreach ($this->bundleInfo->getBundleInfo($this->getEntityType()) as $bundle_id => $bundle) {
  115. if (isset($bundle['workflow']) && in_array($bundle['workflow'], $workflow_ids, TRUE)) {
  116. $moderated_bundles[] = $bundle_id;
  117. }
  118. }
  119. // If we have a list of moderated bundles, restrict the query to show only
  120. // entities in those bundles.
  121. if ($moderated_bundles) {
  122. $entity_base_table_alias = $this->relationship ?: $this->table;
  123. // The bundle field of an entity type is not revisionable so we need to
  124. // join the base table.
  125. $entity_base_table = $entity_type->getBaseTable();
  126. $entity_revision_base_table = $entity_type->isTranslatable() ? $entity_type->getRevisionDataTable() : $entity_type->getRevisionTable();
  127. if ($this->table === $entity_revision_base_table) {
  128. $configuration = [
  129. 'table' => $entity_base_table,
  130. 'field' => $entity_type->getKey('id'),
  131. 'left_table' => $entity_revision_base_table,
  132. 'left_field' => $entity_type->getKey('id'),
  133. 'type' => 'INNER',
  134. ];
  135. $join = Views::pluginManager('join')->createInstance('standard', $configuration);
  136. $entity_base_table_alias = $this->query->addRelationship($entity_base_table, $join, $entity_revision_base_table);
  137. }
  138. $bundle_condition = new Condition('AND');
  139. $bundle_condition->condition("$entity_base_table_alias.{$entity_type->getKey('bundle')}", $moderated_bundles, 'IN');
  140. }
  141. // Otherwise, force the query to return an empty result.
  142. else {
  143. $this->query->addWhereExpression($this->options['group'], '1 = 0');
  144. return;
  145. }
  146. }
  147. if ($this->operator === 'in') {
  148. $operator = "=";
  149. }
  150. else {
  151. $operator = "<>";
  152. }
  153. // The values are strings composed from the workflow ID and the state ID, so
  154. // we need to create a complex WHERE condition.
  155. $field = new Condition('OR');
  156. foreach ((array) $this->value as $value) {
  157. list($workflow_id, $state_id) = explode('-', $value, 2);
  158. $and = new Condition('AND');
  159. $and
  160. ->condition("$this->tableAlias.workflow", $workflow_id, '=')
  161. ->condition("$this->tableAlias.$this->realField", $state_id, $operator);
  162. $field->condition($and);
  163. }
  164. if ($bundle_condition) {
  165. // The query must match the bundle AND the workflow/state conditions.
  166. $bundle_condition->condition($field);
  167. $this->query->addWhere($this->options['group'], $bundle_condition);
  168. }
  169. else {
  170. $this->query->addWhere($this->options['group'], $field);
  171. }
  172. }
  173. /**
  174. * {@inheritdoc}
  175. */
  176. public function calculateDependencies() {
  177. $dependencies = parent::calculateDependencies();
  178. if ($workflow_ids = $this->getWorkflowIds()) {
  179. /** @var \Drupal\workflows\WorkflowInterface $workflow */
  180. foreach ($this->workflowStorage->loadMultiple($workflow_ids) as $workflow) {
  181. $dependencies[$workflow->getConfigDependencyKey()][] = $workflow->getConfigDependencyName();
  182. }
  183. }
  184. return $dependencies;
  185. }
  186. /**
  187. * {@inheritdoc}
  188. */
  189. public function onDependencyRemoval(array $dependencies) {
  190. // See if this handler is responsible for any of the dependencies being
  191. // removed. If this is the case, indicate that this handler needs to be
  192. // removed from the View.
  193. $remove = FALSE;
  194. // Get all the current dependencies for this handler.
  195. $current_dependencies = $this->calculateDependencies();
  196. foreach ($current_dependencies as $group => $dependency_list) {
  197. // Check if any of the handler dependencies match the dependencies being
  198. // removed.
  199. foreach ($dependency_list as $config_key) {
  200. if (isset($dependencies[$group]) && array_key_exists($config_key, $dependencies[$group])) {
  201. // This handlers dependency matches a dependency being removed,
  202. // indicate that this handler needs to be removed.
  203. $remove = TRUE;
  204. break 2;
  205. }
  206. }
  207. }
  208. return $remove;
  209. }
  210. /**
  211. * Gets the list of Workflow IDs configured for this filter.
  212. *
  213. * @return array
  214. * And array of workflow IDs.
  215. */
  216. protected function getWorkflowIds() {
  217. $workflow_ids = [];
  218. foreach ((array) $this->value as $value) {
  219. list($workflow_id) = explode('-', $value, 2);
  220. $workflow_ids[] = $workflow_id;
  221. }
  222. return array_unique($workflow_ids);
  223. }
  224. }