public function popular(
        $limit = 10,
        $prefix = '#',
        $render = 'cloud',
        $shuffle = true,
        $style = ['maxFont' => 40, 'minFont' => 12]
    ) {
        $this->viewBuilder()->setTemplate(sprintf('popular_as_%s', $render));

        //Returns on tags index
        if ($this->request->isUrl(['_name' => 'postsTags'])) {
            return;
        }

        //Sets default maximum and minimun font sizes we want to use
        $maxFont = $minFont = 0;

        //Sets the initial cache name
        $cache = sprintf('widget_tags_popular_%s', $limit);

        if ($style && is_array($style)) {
            //Updates maximum and minimun font sizes we want to use
            list($maxFont, $minFont) = $this->getFontSizes($style);

            //Updates the cache name
            $cache = sprintf('%s_max_%s_min_%s', $cache, $maxFont, $minFont);
        }

        $tags = $this->Tags->find()
            ->select(['tag', 'post_count'])
            ->limit($limit)
            ->order([
                sprintf('%s.post_count', $this->Tags->getAlias()) => 'DESC',
                sprintf('%s.tag', $this->Tags->getAlias()) => 'ASC',
            ])
            ->formatResults(function (ResultSet $results) use ($style, $maxFont, $minFont) {
                $results = $results->indexBy('slug');

                if (!$results->count() || !$style || !$maxFont || !$minFont) {
                    return $results;
                }

                //Highest and lowest numbers of occurrences and their difference
                $minCount = $results->last()->post_count;
                $diffCount = $results->first()->post_count - $minCount;
                $diffFont = $maxFont - $minFont;

                return $results->map(function (Tag $tag) use ($minCount, $diffCount, $maxFont, $minFont, $diffFont) {
                    $tag->size = $diffCount ? round((($tag->post_count - $minCount) / $diffCount * $diffFont) + $minFont) : $maxFont;

                    return $tag;
                });
            })
            ->cache($cache, $this->Tags->getCacheName())
            ->all();

        if ($shuffle) {
            $tags = $tags->shuffle();
        }

        $this->set(compact('prefix', 'tags'));
    }
public function popular(
        $limit = 10,
        $prefix = '#',
        $render = 'cloud',
        $shuffle = true,
        $style = ['maxFont' => 40, 'minFont' => 12]
    ) {
        $this->viewBuilder()->setTemplate(sprintf('popular_as_%s', $render));

        //Returns on tags index
        if ($this->request->isUrl(['_name' => 'postsTags'])) {
            return;
        }

        //Sets default maximum and minimun font sizes we want to use
        $maxFont = $minFont = 0;

        //Sets the initial cache name
        $cache = sprintf('widget_tags_popular_%s', $limit);

        if ($style && is_array($style)) {
            //Updates maximum and minimun font sizes we want to use
            list($maxFont, $minFont) = $this->getFontSizes($style);

            //Updates the cache name
            $cache = sprintf('%s_max_%s_min_%s', $cache, $maxFont, $minFont);
        }

        $tags = $this->Tags->find()
            ->select(['tag', 'post_count'])
            ->limit($limit)
            ->order([
                sprintf('%s.post_count', $this->Tags->getAlias()) => 'DESC',
                sprintf('%s.tag', $this->Tags->getAlias()) => 'ASC',
            ])
            ->formatResults(function (ResultSet $results) use ($style, $maxFont, $minFont) {
                $results = $results->indexBy('slug');

                if (!$results->count() || !$style || !$maxFont || !$minFont) {
                    return $results;
                }

                //Highest and lowest numbers of occurrences and their difference
                $minCount = $results->last()->post_count;
                $diffCount = $results->first()->post_count - $minCount;
                $diffFont = $maxFont - $minFont;

                return $results->map(function (Tag $tag) use ($minCount, $diffCount, $maxFont, $minFont, $diffFont) {
                    $tag->size = $diffCount ? round((($tag->post_count - $minCount) / $diffCount * $diffFont) + $minFont) : $maxFont;

                    return $tag;
                });
            })
            ->cache($cache, $this->Tags->getCacheName())
            ->all();

        if ($shuffle) {
            $tags = $tags->shuffle();
        }

        $this->set(compact('prefix', 'tags'));
    }
public function popular ( $ limit = 10 , $ prefix = '#' , $ render = 'cloud' , $ shuffle = true , $ style = [ 'maxFont' => 40 , 'minFont' => 12 ] ) { $ this -> viewBuilder ( ) -> setTemplate ( sprintf ( 'popular_as_%s' , $ render ) ) ; //Returns on tags index if ( $ this -> request -> isUrl ( [ '_name' => 'postsTags' ] ) ) { return ; } //Sets default maximum and minimun font sizes we want to use $ maxFont = $ minFont = 0 ; //Sets the initial cache name $ cache = sprintf ( 'widget_tags_popular_%s' , $ limit ) ; if ( $ style && is_array ( $ style ) ) { //Updates maximum and minimun font sizes we want to use list ( $ maxFont , $ minFont ) = $ this -> getFontSizes ( $ style ) ; //Updates the cache name $ cache = sprintf ( '%s_max_%s_min_%s' , $ cache , $ maxFont , $ minFont ) ; } $ tags = $ this -> Tags -> find ( ) -> select ( [ 'tag' , 'post_count' ] ) -> limit ( $ limit ) -> order ( [ sprintf ( '%s.post_count' , $ this -> Tags -> getAlias ( ) ) => 'DESC' , sprintf ( '%s.tag' , $ this -> Tags -> getAlias ( ) ) => 'ASC' , ] ) -> formatResults ( function ( ResultSet $ results ) use ( $ style , $ maxFont , $ minFont ) { $ results = $ results -> indexBy ( 'slug' ) ; if ( ! $ results -> count ( ) || ! $ style || ! $ maxFont || ! $ minFont ) { return $ results ; } //Highest and lowest numbers of occurrences and their difference $ minCount = $ results -> last ( ) -> post_count ; $ diffCount = $ results -> first ( ) -> post_count - $ minCount ; $ diffFont = $ maxFont - $ minFont ; return $ results -> map ( function ( Tag $ tag ) use ( $ minCount , $ diffCount , $ maxFont , $ minFont , $ diffFont ) { $ tag -> size = $ diffCount ? round ( ( ( $ tag -> post_count - $ minCount ) / $ diffCount * $ diffFont ) + $ minFont ) : $ maxFont ; return $ tag ; } ) ; } ) -> cache ( $ cache , $ this -> Tags -> getCacheName ( ) ) -> all ( ) ; if ( $ shuffle ) { $ tags = $ tags -> shuffle ( ) ; } $ this -> set ( compact ( 'prefix' , 'tags' ) ) ; }
Popular tags widgets
@param int $limit Limit
@param string $prefix Prefix for each tag. This works only with the cloud
@param string $render Render type (`cloud`, `form` or `list`)
@param bool $shuffle Shuffles tags
@param array|bool $style Style for tags. Array with `maxFont` and
`minFont` keys or `false` to disable
@return void
@uses getFontSizes()
Popular tags widgets