protected function _prepareBlocks()
    {
        $cacheKey = "{$this->_View->theme}_{$this->_machineName}";
        $blocks = TableRegistry::get('Block.Blocks')
            ->find('all')
            ->cache($cacheKey, 'blocks')
            ->contain(['Roles', 'BlockRegions'])
            ->matching('BlockRegions', function ($q) {
                return $q->where([
                    'BlockRegions.theme' => $this->_View->theme(),
                    'BlockRegions.region' => $this->_machineName,
                ]);
            })
            ->where(['Blocks.status' => 1])
            ->order(['BlockRegions.ordering' => 'ASC']);

        $blocks->sortBy(function ($block) {
            return $block->region->ordering;
        }, SORT_ASC);

        // remove blocks that cannot be rendered based on current request.
        $blocks = $blocks->filter(function ($block) {
            return $this->_filterBlock($block) && $block->renderable();
        });

        $this->blocks($blocks);
    }