0

I have tried to add Image Slider into my home page using following the steps using the following link https://magenticians.com/customize-homepage-magento-2-add-image-slider/

When I clicked save after configuring the things, I an gettting an error

Notice: unserialize(): Error at offset 0 of 1208 bytes in C:\xampp\htdocs\magento\app\code\Ves\Productlist\Model\Magento\Widget\Instance.php on line 35

Image Slider Error

Please llok instance.php code below

class Instance extends \Magento\Widget\Model\Widget\Instance
{
    /**
     * Getter
     * Unserialize if serialized string setted
     *
     * @return array
     */
    public function getWidgetParameters()
    {

        if (is_string($this->getData('widget_parameters'))) {
            $params = unserialize($this->getData('widget_parameters'));

            $field_pattern = ["pretext","pretext_html","shortcode","html","raw_html","content","tabs","latestmod_desc","custom_css","block_params"];
            $widget_types = ["Ves\BaseWidget\Block\Widget\Accordionbg"];

            $is_custom_params = false;

            foreach ($params as $k => $v) {
                if(0 < strpos($k, 'class') || 0 < strpos($k, 'Class')) {
                    continue;
                }
                if(is_array($params[$k]) || !$this->isBase64Encoded($params[$k])) {
                    if(in_array($k, $field_pattern) || preg_match("/^tabs(.*)/", $k) || preg_match("/^content_(.*)/", $k) || (preg_match("/^header_(.*)/", $k) && in_array($type, $widget_types))) {
                        if(is_array($params[$k])){
                            $params[$k] = base64_encode(serialize($params[$k]));
                        }elseif(!$this->isBase64Encoded($params[$k])){
                            $params[$k] = base64_encode($params[$k]);
                        }
                        $is_custom_params = true;
                    }
                }

            }
            if($is_custom_params) {
                $this->setData('widget_parameters', $params);
            }

        }

        return parent::getWidgetParameters();
    }
    public function isBase64Encoded($data) {
        if(base64_encode(base64_decode($data)) === $data){
            return true;
        }
        return false;
    }
}

I am new to Magento. Kindly help me to resolve this issue.

  • Share/Copy app\code\Ves\Productlist\Model\Magento\Widget\Instance.php file in your question I will help you. – Prince Patel Jan 6 '18 at 8:51
  • @PrincePatel updated the question with the code, please check it – Santhucool Jan 6 '18 at 14:45
  • In Magento 2.2 you cannot direct unserialize value. You should first construct an object. Refer my answer for how use unserialize: magento.stackexchange.com/a/195302/35758 – Prince Patel Jan 6 '18 at 15:01
  • @PrincePatel I am new to php please guide me how can i solve this issue. post as an answer i will accept. – Santhucool Jan 6 '18 at 15:03
  • You should accept an answer to help other developers to find their right solution – Prince Patel Jan 8 '18 at 6:02
0

Your final code like this:

app\code\Ves\Productlist\Model\Magento\Widget\Instance.php

class Instance extends \Magento\Widget\Model\Widget\Instance
{
    protected $serialize;

    public function __construct(
        \Magento\Framework\Serialize\Serializer\Json $serialize
    ) {
        $this->serialize = $serialize;
    }

    /**
     * Getter
     * Unserialize if serialized string setted
     *
     * @return array
     */
    public function getWidgetParameters()
    {

        if (is_string($this->getData('widget_parameters'))) {
            $params = $this->serialize->unserialize($this->getData('widget_parameters'));

            $field_pattern = ["pretext","pretext_html","shortcode","html","raw_html","content","tabs","latestmod_desc","custom_css","block_params"];
            $widget_types = ["Ves\BaseWidget\Block\Widget\Accordionbg"];

            $is_custom_params = false;

            foreach ($params as $k => $v) {
                if(0 < strpos($k, 'class') || 0 < strpos($k, 'Class')) {
                    continue;
                }
                if(is_array($params[$k]) || !$this->isBase64Encoded($params[$k])) {
                    if(in_array($k, $field_pattern) || preg_match("/^tabs(.*)/", $k) || preg_match("/^content_(.*)/", $k) || (preg_match("/^header_(.*)/", $k) && in_array($type, $widget_types))) {
                        if(is_array($params[$k])){
                            $params[$k] = base64_encode(serialize($params[$k]));
                        }elseif(!$this->isBase64Encoded($params[$k])){
                            $params[$k] = base64_encode($params[$k]);
                        }
                        $is_custom_params = true;
                    }
                }

            }
            if($is_custom_params) {
                $this->setData('widget_parameters', $params);
            }

        }

        return parent::getWidgetParameters();
    }
    public function isBase64Encoded($data) {
        if(base64_encode(base64_decode($data)) === $data){
            return true;
        }
        return false;
    }
}

Now remove \var and \generated folder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.