public function months($render = 'form')
    {
        $this->viewBuilder()->setTemplate(sprintf('months_as_%s', $render));

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

        $query = $this->Posts->find('active');
        $time = $query->func()->date_format(['created' => 'identifier', "'%Y/%m'" => 'literal']);
        $months = $query->select([
                'month' => $time,
                'post_count' => $query->func()->count($time),
            ])
            ->distinct(['month'])
            ->formatResults(function (ResultSet $results) {
                return $results->indexBy('month')
                    ->map(function (Post $post) {
                        list($year, $month) = explode('/', $post->month);
                        $post->month = (new FrozenDate())->day(1)->month($month)->year($year);

                        return $post;
                    });
            })
            ->order(['month' => 'DESC'])
            ->cache('widget_months', $this->Posts->getCacheName())
            ->all();

        $this->set(compact('months'));
    }
public function months($render = 'form')
    {
        $this->viewBuilder()->setTemplate(sprintf('months_as_%s', $render));

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

        $query = $this->Posts->find('active');
        $time = $query->func()->date_format(['created' => 'identifier', "'%Y/%m'" => 'literal']);
        $months = $query->select([
                'month' => $time,
                'post_count' => $query->func()->count($time),
            ])
            ->distinct(['month'])
            ->formatResults(function (ResultSet $results) {
                return $results->indexBy('month')
                    ->map(function (Post $post) {
                        list($year, $month) = explode('/', $post->month);
                        $post->month = (new FrozenDate())->day(1)->month($month)->year($year);

                        return $post;
                    });
            })
            ->order(['month' => 'DESC'])
            ->cache('widget_months', $this->Posts->getCacheName())
            ->all();

        $this->set(compact('months'));
    }
public function months ( $ render = 'form' ) { $ this -> viewBuilder ( ) -> setTemplate ( sprintf ( 'months_as_%s' , $ render ) ) ; //Returns on posts index if ( $ this -> request -> isUrl ( [ '_name' => 'posts' ] ) ) { return ; } $ query = $ this -> Posts -> find ( 'active' ) ; $ time = $ query -> func ( ) -> date_format ( [ 'created' => 'identifier' , "'%Y/%m'" => 'literal' ] ) ; $ months = $ query -> select ( [ 'month' => $ time , 'post_count' => $ query -> func ( ) -> count ( $ time ) , ] ) -> distinct ( [ 'month' ] ) -> formatResults ( function ( ResultSet $ results ) { return $ results -> indexBy ( 'month' ) -> map ( function ( Post $ post ) { list ( $ year , $ month ) = explode ( '/' , $ post -> month ) ; $ post -> month = ( new FrozenDate ( ) ) -> day ( 1 ) -> month ( $ month ) -> year ( $ year ) ; return $ post ; } ) ; } ) -> order ( [ 'month' => 'DESC' ] ) -> cache ( 'widget_months' , $ this -> Posts -> getCacheName ( ) ) -> all ( ) ; $ this -> set ( compact ( 'months' ) ) ; }
Posts by month widget
@param string $render Render type (`form` or `list`)
@return void
Posts by month widget