function ModerationStateFilter::opSimple

Same name and namespace in other branches
  1. 8.9.x core/modules/content_moderation/src/Plugin/views/filter/ModerationStateFilter.php \Drupal\content_moderation\Plugin\views\filter\ModerationStateFilter::opSimple()
  2. 10 core/modules/content_moderation/src/Plugin/views/filter/ModerationStateFilter.php \Drupal\content_moderation\Plugin\views\filter\ModerationStateFilter::opSimple()
  3. 11.x core/modules/content_moderation/src/Plugin/views/filter/ModerationStateFilter.php \Drupal\content_moderation\Plugin\views\filter\ModerationStateFilter::opSimple()

Overrides InOperator::opSimple

File

core/modules/content_moderation/src/Plugin/views/filter/ModerationStateFilter.php, line 117

Class

ModerationStateFilter
Provides a filter for the moderation state of an entity.

Namespace

Drupal\content_moderation\Plugin\views\filter

Code

protected function opSimple() {
    if (empty($this->value)) {
        return;
    }
    $this->ensureMyTable();
    $entity_type = $this->entityTypeManager
        ->getDefinition($this->getEntityType());
    $bundle_condition = NULL;
    if ($entity_type->hasKey('bundle')) {
        // Get a list of bundles that are being moderated by the workflows
        // configured in this filter.
        $workflow_ids = $this->getWorkflowIds();
        $moderated_bundles = [];
        foreach ($this->bundleInfo
            ->getBundleInfo($this->getEntityType()) as $bundle_id => $bundle) {
            if (isset($bundle['workflow']) && in_array($bundle['workflow'], $workflow_ids, TRUE)) {
                $moderated_bundles[] = $bundle_id;
            }
        }
        // If we have a list of moderated bundles, restrict the query to show only
        // entities in those bundles.
        if ($moderated_bundles) {
            $entity_base_table_alias = $this->relationship ?: $this->table;
            // The bundle field of an entity type is not revisionable so we need to
            // join the base table.
            $entity_base_table = $entity_type->getBaseTable();
            $entity_revision_base_table = $entity_type->isTranslatable() ? $entity_type->getRevisionDataTable() : $entity_type->getRevisionTable();
            if ($this->table === $entity_revision_base_table) {
                $configuration = [
                    'table' => $entity_base_table,
                    'field' => $entity_type->getKey('id'),
                    'left_table' => $entity_revision_base_table,
                    'left_field' => $entity_type->getKey('id'),
                    'type' => 'INNER',
                ];
                $join = Views::pluginManager('join')->createInstance('standard', $configuration);
                $entity_base_table_alias = $this->query
                    ->addRelationship($entity_base_table, $join, $entity_revision_base_table);
            }
            $bundle_condition = $this->view->query
                ->getConnection()
                ->condition('AND');
            $bundle_condition->condition("{$entity_base_table_alias}.{$entity_type->getKey('bundle')}", $moderated_bundles, 'IN');
        }
        else {
            $this->query
                ->addWhereExpression($this->options['group'], '1 = 0');
            return;
        }
    }
    if ($this->operator === 'in') {
        $operator = "=";
    }
    else {
        $operator = "<>";
    }
    // The values are strings composed from the workflow ID and the state ID, so
    // we need to create a complex WHERE condition.
    $field = $this->view->query
        ->getConnection()
        ->condition('OR');
    foreach ((array) $this->value as $value) {
        [
            $workflow_id,
            $state_id,
        ] = explode('-', $value, 2);
        $and = $this->view->query
            ->getConnection()
            ->condition('AND');
        $and->condition("{$this->tableAlias}.workflow", $workflow_id, '=')
            ->condition("{$this->tableAlias}.{$this->realField}", $state_id, $operator);
        $field->condition($and);
    }
    if ($bundle_condition) {
        // The query must match the bundle AND the workflow/state conditions.
        $bundle_condition->condition($field);
        $this->query
            ->addWhere($this->options['group'], $bundle_condition);
    }
    else {
        $this->query
            ->addWhere($this->options['group'], $field);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.