Search code examples
xmlfrontendmagento2

How to fix error when displaying slideshow in Magento 2


I am trying to display a slideshow on my homepage in Magento 2. The slideshow is a feature of my theme extension, "Fastest". When I try to put a slideshow on my homepage, I get an error on the frontend. "We're sorry, an error has occurred while generating this content." Error Message

I think this may be because of the XML identifier asked for in the slideshow homepage. XML-Identifier

I know very little XML, and I am not sure how the XML identifier is supposed to be formatted. I put the sequence "id0001" as my identifier and Magento accepted this when I clicked save, but I am not sure if this is correct. If this is incorrect I would like to know how to format an XML-Identifier, otherwise I would like advice on how to fix a frontend error for a slideshow.

Update:

I searched the error logs using tail -f to tell when a new error would be generated by trying to load this slideshow. When I did, I found this message.

Warning: A non-numeric value encontered in {my-folder-name}/app/code/Codazon/Slideshow/Block/Widget/Slideshow.php on line 78

The Slideshow.php file looks like this. Line 78 is marked below.

<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

namespace Codazon\Slideshow\Block\Widget;

/**
 * Codazon slideshow content 
 */
class Slideshow extends \Magento\Framework\View\Element\Template 
implements \Magento\Widget\Block\BlockInterface
{


    /**
     * Slideshow factory
     *
     * @var \Codazon\Slideshow\Model\SlideshowFactory
     */
    protected $_slideshowFactory;

    protected $_objectSlideshow;

    protected $_isFullHtml;
    /**
     * Construct
     *
     * @param \Magento\Framework\View\Element\Context $context    
     * @param \Codazon\Slideshow\Model\BlockFactory $blockFactory
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,     
        \Codazon\Slideshow\Model\SlideshowFactory 
$slideshowFactory,
        \Magento\Framework\App\Http\Context $httpContext,
        array $data = []
    ) {
        parent::__construct($context, $data);     
        $this->httpContext = $httpContext;   
        $this->_slideshowFactory = $slideshowFactory;
        $this->_urlBuilder = $context->getUrlBuilder();      
        $this->addData([
            'cache_lifetime' => 86400,
            'cache_tags' => ['CDZ_SLIDESHOW',
        ], ]);        
    }

    public function getMediaUrl() {
        return $this->_urlBuilder->getBaseUrl(['_type' => 
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA]);
    }

    /**
     * Prepare Content HTML
     *
     * @return string
     */
    public function getSlideshowData()
    {
        $slideshowId = $this->getSlideshowId();            
    
        if ($slideshowId) {                        
            $slideshow = $this->_slideshowFactory->create();
            $slideshow->load($slideshowId,'identifier');            
            if ($slideshow->isActive()) {
                return $slideshow;
            }            
        }          
    }

    public function getPadding(){
        $slideshow = $this->getSlideshowData();
        if($slideshow){
            $params = $slideshow->getParameters();
            $params = json_decode($params);
            return $params->height/2;  *****This is line 78*****
        }
    }

    /**
     * Get key pieces for caching block content
     *
     * @return array
     */
    public function getCacheKeyInfo()
    {
        $slideshow = serialize($this->getData());
        
        return [
            'CODAZON_SLIDESHOW',
            $this->_storeManager->getStore()->getId(),
            $this->_design->getDesignTheme()->getId(),
            $this->httpContext- 
>getValue(\Magento\Customer\Model\Context::CONTEXT_GROUP),                    
            $slideshow
        ];
    }

    public function getTemplate()
    {
        if ($this->isFullHtml()) {
            $template = "slideshow.phtml";
            return $template;
        } else {
            return 'Codazon_Slideshow::ajax.phtml';
        }
    }

    public function isFullHtml()
    {
        if ($this->_isFullHtml === null) {
            $ajaxBlog = 0;//$this->getThemeHelper()- 
>getConfig('pages/blog/use_ajax_blog');
            $this->_isFullHtml = ($this->getData('full_html')) || 
 (!$ajaxBlog);
        }
        return $this->_isFullHtml;
    }

    public function getFilterData()
    {
        $data = $this->getData();
        unset($data['type']);
        unset($data['module_name']);
        unset($data['title']);
        return $data;
    }

    /**
     * Return identifiers for produced content
     *
     * @return array
     */
    public function getIdentities()
    {
        return [\Codazon\Slideshow\Model\Slideshow::CACHE_TAG . '_' 
 . $this->getSlideshowId()];
    }
  
}

I tried debugging this code, but I could not figure out what was causing the error. It seemed like it was the variable height that was causing the non-numeric value, but I could not find where this came from, and I also wasn't sure why it didn't have a $ sign. I tried putting a $ behind it, but that didn't fix the error.


Solution

  • To resolve slideshow display errors in Magento 2, check the module configuration, ensuring correct image uploads and file permissions are set properly. Clear the cache in the Magento Admin Panel and address JavaScript conflicts with other themes or modules. Also, verify theme compatibility, update Magento and the slideshow extension, review server logs for error messages, and enable debug mode for more information. If issues persist, contact extension support or the developer community, and remember to back up your site before making changes to avoid data loss.