src/Attribute/TranslatedTags.php
protected function getValueIds($ids, $usedOnly, &$count = null)
    {
        if ([] === $ids) {
            return [];
        }

        $aliasColName = $this->getAliasColumn();

        // First off, we need to determine the option ids in the foreign table.
        $queryBuilder = $this->getConnection()->createQueryBuilder();

        if (null !== $ids) {
            $statement = $this->getConnection()->createQueryBuilder()
                ->select('COUNT(source.' . $this->getIdColumn() . ') AS mm_count')
                ->addSelect('source.' . $this->getIdColumn())
                ->addSelect('source.' . $aliasColName)
                ->from($this->getTagSource(), 'source')
                ->leftJoin(
                    'source',
                    'tl_metamodel_tag_relation',
                    'rel',
                    $queryBuilder->expr()->andX()->add('rel.att_id=:att')->add(
                        'rel.value_id=source.' . $this->getIdColumn()
                    )
                )
                ->where('rel.item_id IN (:items)')
                ->setParameter('att', $this->get('id'))
                ->setParameter('items', $ids, Connection::PARAM_STR_ARRAY);

            if ($this->getTagSortSourceTable()) {
                $statement
                    ->addSelect($this->getTagSortSourceTable() . '.*')
                    ->join(
                        'source',
                        $this->getTagSortSourceTable(),
                        'sort',
                        'source.' . $this->getIdColumn() . '=sort.id'
                    );

                if ($this->getTagSortSourceColumn()) {
                    $statement->orderBy($this->getTagSortSourceColumn('sort'));
                }
            }

            if ($this->getWhereColumn()) {
                $statement->andWhere('(' . $this->getWhereColumn() . ')');
            }

            $statement
                ->groupBy('source.' . $this->getIdColumn())
                ->addOrderBy('source.' . $this->getSortingColumn());
        } elseif ($usedOnly) {
            $statement = $this->getConnection()->createQueryBuilder()
                ->select('COUNT(value_id) AS mm_count')
                ->addSelect('value_id AS ' . $this->getIdColumn())
                ->addSelect($this->getTagSource() . '.' . $aliasColName)
                ->from('tl_metamodel_tag_relation', 'rel')
                ->rightJoin(
                    'rel',
                    $this->getTagSource(),
                    'source',
                    'rel.value_id=source.' . $this->getIdColumn()
                )
                ->where('rel.att_id=:att')
                ->setParameter('att', $this->get('id'));

            if ($this->getTagSortSourceTable()) {
                $statement
                    ->addSelect($this->getTagSortSourceTable() . '.*')
                    ->join(
                        'source',
                        $this->getTagSortSourceTable(),
                        'sort',
                        'source.' . $this->getIdColumn() . '=sort.id'
                    );

                if ($this->getTagSortSourceColumn()) {
                    $statement->orderBy($this->getTagSortSourceColumn('sort'));
                }
            }

            if ($this->getWhereColumn()) {
                $statement->andWhere('(' . $this->getWhereColumn() . ')');
            }

            $statement
                ->groupBy('rel.value_id')
                ->addOrderBy('source.' . $this->getSortingColumn());
        } else {
            $statement = $this->getConnection()->createQueryBuilder()
                ->select('COUNT(source.' . $this->getIdColumn() . ') AS mm_count')
                ->addSelect('source.' . $this->getIdColumn())
                ->addSelect('source.' . $aliasColName)
                ->from($this->getTagSource(), 'source');

            if ($this->getTagSortSourceTable()) {
                $statement
                    ->addSelect($this->getTagSortSourceTable() . '.*')
                    ->join(
                        'source',
                        $this->getTagSortSourceTable(),
                        'sort',
                        'source.' . $this->getIdColumn() . '=sort.id'
                    );

                if ($this->getTagSortSourceColumn()) {
                    $statement->orderBy($this->getTagSortSourceColumn('sort'));
                }
            }

            if ($this->getWhereColumn()) {
                $statement->where('(' . $this->getWhereColumn() . ')');
            }

            $statement
                ->groupBy('source.' . $this->getIdColumn())
                ->addOrderBy($this->getSortingColumn());
        }

        return $this->convertValueIds($statement->execute(), $count);
    }
protected function getValueIds($ids, $usedOnly, &$count = null)
    {
        if ([] === $ids) {
            return [];
        }

        $aliasColName = $this->getAliasColumn();

        // First off, we need to determine the option ids in the foreign table.
        $queryBuilder = $this->getConnection()->createQueryBuilder();

        if (null !== $ids) {
            $statement = $this->getConnection()->createQueryBuilder()
                ->select('COUNT(source.' . $this->getIdColumn() . ') AS mm_count')
                ->addSelect('source.' . $this->getIdColumn())
                ->addSelect('source.' . $aliasColName)
                ->from($this->getTagSource(), 'source')
                ->leftJoin(
                    'source',
                    'tl_metamodel_tag_relation',
                    'rel',
                    $queryBuilder->expr()->andX()->add('rel.att_id=:att')->add(
                        'rel.value_id=source.' . $this->getIdColumn()
                    )
                )
                ->where('rel.item_id IN (:items)')
                ->setParameter('att', $this->get('id'))
                ->setParameter('items', $ids, Connection::PARAM_STR_ARRAY);

            if ($this->getTagSortSourceTable()) {
                $statement
                    ->addSelect($this->getTagSortSourceTable() . '.*')
                    ->join(
                        'source',
                        $this->getTagSortSourceTable(),
                        'sort',
                        'source.' . $this->getIdColumn() . '=sort.id'
                    );

                if ($this->getTagSortSourceColumn()) {
                    $statement->orderBy($this->getTagSortSourceColumn('sort'));
                }
            }

            if ($this->getWhereColumn()) {
                $statement->andWhere('(' . $this->getWhereColumn() . ')');
            }

            $statement
                ->groupBy('source.' . $this->getIdColumn())
                ->addOrderBy('source.' . $this->getSortingColumn());
        } elseif ($usedOnly) {
            $statement = $this->getConnection()->createQueryBuilder()
                ->select('COUNT(value_id) AS mm_count')
                ->addSelect('value_id AS ' . $this->getIdColumn())
                ->addSelect($this->getTagSource() . '.' . $aliasColName)
                ->from('tl_metamodel_tag_relation', 'rel')
                ->rightJoin(
                    'rel',
                    $this->getTagSource(),
                    'source',
                    'rel.value_id=source.' . $this->getIdColumn()
                )
                ->where('rel.att_id=:att')
                ->setParameter('att', $this->get('id'));

            if ($this->getTagSortSourceTable()) {
                $statement
                    ->addSelect($this->getTagSortSourceTable() . '.*')
                    ->join(
                        'source',
                        $this->getTagSortSourceTable(),
                        'sort',
                        'source.' . $this->getIdColumn() . '=sort.id'
                    );

                if ($this->getTagSortSourceColumn()) {
                    $statement->orderBy($this->getTagSortSourceColumn('sort'));
                }
            }

            if ($this->getWhereColumn()) {
                $statement->andWhere('(' . $this->getWhereColumn() . ')');
            }

            $statement
                ->groupBy('rel.value_id')
                ->addOrderBy('source.' . $this->getSortingColumn());
        } else {
            $statement = $this->getConnection()->createQueryBuilder()
                ->select('COUNT(source.' . $this->getIdColumn() . ') AS mm_count')
                ->addSelect('source.' . $this->getIdColumn())
                ->addSelect('source.' . $aliasColName)
                ->from($this->getTagSource(), 'source');

            if ($this->getTagSortSourceTable()) {
                $statement
                    ->addSelect($this->getTagSortSourceTable() . '.*')
                    ->join(
                        'source',
                        $this->getTagSortSourceTable(),
                        'sort',
                        'source.' . $this->getIdColumn() . '=sort.id'
                    );

                if ($this->getTagSortSourceColumn()) {
                    $statement->orderBy($this->getTagSortSourceColumn('sort'));
                }
            }

            if ($this->getWhereColumn()) {
                $statement->where('(' . $this->getWhereColumn() . ')');
            }

            $statement
                ->groupBy('source.' . $this->getIdColumn())
                ->addOrderBy($this->getSortingColumn());
        }

        return $this->convertValueIds($statement->execute(), $count);
    }
Fetch the ids of options optionally limited to the items with the provided ids.

NOTE: this does not take the actual availability of an value in the current or
fallback languages into account.
This method is mainly intended as a helper for TranslatedTags::getFilterOptions().

@param string[]|null $ids      A list of item ids that the result shall be limited to.

@param bool          $usedOnly Do only return ids that have matches in the real table.

@param null          $count    Array to where the amount of items per tag shall be stored. May be null to return
nothing.

@return int[] a list of all matching value ids.

@see TranslatedTags::getFilterOptions().

@SuppressWarnings(PHPMD.ExcessiveMethodLength)
@SuppressWarnings(PHPMD.NPathComplexity)
@SuppressWarnings(PHPMD.CyclomaticComplexity)