garyconstable
12/6/2018 - 12:33 PM

Magento - before, during, after

Magento - before, during, after

<?php

namespace Kodal\BindingShape\Plugin;

if(!function_exists("d")){
  function d($data = array(), $die = true){
    echo '<pre>'.print_r($data, true).'</pre>';
    if($die){
      die();
    }
  }
}


class FilterRenderer
{
    /*
    //$filterRenderer = Amasty\Shopby\Block\Navigation\FilterRenderer\Interceptor
    public function beforeRender( $filterRenderer, $attributes  )
    {
        return [
            'some' => 'value'
        ];
    }

    //$filterRenderer = Amasty\Shopby\Block\Navigation\FilterRenderer\Interceptor
    public function aroundRender( $filterRenderer, $closure, $returnFromBefore )
    {
        d( get_class_methods($filterRenderer));
        $settings = $filterRenderer->getFilterSetting();
        $template = $this->getTemplateByFilterSetting($settings);
        d($template);
        return '<p>html</p>';        
    }
    */

    //$filterRenderer = Amasty\Shopby\Block\Navigation\FilterRenderer\Interceptor
    public function afterRender( $filterRenderer, $html, $attributes )
    {
        $settings = $filterRenderer->getFilterSetting();
        $template = $this->getTemplateByFilterSetting($settings);
        d('==> tpl 2: ' . $template, 0);
        return $html;
    }


    public function getTemplateByFilterSetting( $filterSetting )
    {
        switch ($filterSetting->getDisplayMode()) {
            case \Amasty\Shopby\Model\Source\DisplayMode::MODE_SLIDER:
                $template = "layer/filter/slider.phtml";
                break;
            case \Amasty\Shopby\Model\Source\DisplayMode::MODE_DROPDOWN:
                $template = "layer/filter/dropdown.phtml";
                break;
            case \Amasty\Shopby\Model\Source\DisplayMode::MODE_FROM_TO_ONLY:
                $template = "layer/widget/fromto.phtml";
                break;
            default:
                $template = "layer/filter/default.phtml";
                break;
        }
        return $template;
    }

}