Bolt\Legacy\Storage::getContentByTaxonomy PHP Method

getContentByTaxonomy() public method

Note: we can NOT sort on anything meaningful. Records are fetched from multiple content-types, so we can not do joins. Neither can we sort after fetching, because it would mean fetching _all_ records and _then_ doing the sorting. Instead, we do not sort here. If you need ordering, use the '|order()' in your templates.
public getContentByTaxonomy ( string $taxonomyslug, string $name, string $parameters = [] ) : array
$taxonomyslug string
$name string
$parameters string
return array
    public function getContentByTaxonomy($taxonomyslug, $name, $parameters = [])
    {
        $tablename = $this->getTablename('taxonomy');
        $slug = $this->app['slugify']->slugify($name);
        $limit = $parameters['limit'] ?: 9999;
        $page = $parameters['page'] ?: 1;
        $taxonomytype = $this->getTaxonomyType($taxonomyslug);
        // No taxonomytype, no possible content.
        if (empty($taxonomytype)) {
            return false;
        }
        $where = sprintf(' WHERE (taxonomytype = %s AND (slug = %s OR name = %s))', $this->app['db']->quote($taxonomytype['slug']), $this->app['db']->quote($slug), $this->app['db']->quote($name));
        // Make the query for the pager.
        $pagerquery = sprintf('SELECT COUNT(*) AS count FROM %s %s', $tablename, $where);
        $orderby = array_key_exists('has_sortorder', $taxonomytype) && $taxonomytype['has_sortorder'] === true ? 'sortorder' : 'id';
        // Sort on either 'ascending' or 'descending'
        // Make sure we set the order.
        $order = 'ASC';
        $taxonomysort = strtoupper($this->app['config']->get('theme/taxonomy_sort') ?: $this->app['config']->get('general/taxonomy_sort'));
        if ($taxonomysort == 'DESC') {
            $order = 'DESC';
        }
        // Add the limit
        $query = sprintf('SELECT * FROM %s %s ORDER BY %s %s', $tablename, $where, $orderby, $order);
        $query = $this->app['db']->getDatabasePlatform()->modifyLimitQuery($query, $limit, ($page - 1) * $limit);
        $taxorows = $this->app['db']->fetchAll($query);
        if (!empty($parameters['printquery'])) {
            // @todo formalize this
            echo nl2br(htmlentities($query));
        }
        $content = [];
        if (is_array($taxorows)) {
            foreach ($taxorows as $row) {
                $record = $this->getContent($row['contenttype'] . '/' . $row['content_id']);
                if ($record instanceof Content && !empty($record->id)) {
                    $content[] = $record;
                }
            }
        }
        // Set up the $pager array with relevant values.
        $rowcount = $this->app['db']->executeQuery($pagerquery)->fetch();
        $pagefor = $taxonomytype['singular_slug'] . '_' . $slug;
        /** @var \Bolt\Pager\PagerManager $manager */
        $manager = $this->app['pager'];
        $manager->createPager($pagefor)->setCount($rowcount['count'])->setTotalpages(ceil($rowcount['count'] / $limit))->setCurrent($page)->setShowingFrom(($page - 1) * $limit + 1)->setShowingTo(($page - 1) * $limit + count($taxorows));
        return $content;
    }