Jump to content

Override Rss Module


Kateriine

Recommended Posts

Hello,

 

I'm starting to discover Prestashop and I'm trying to override the rss module (it can currently be displayed only in Left column hook and header hook). I want it to be displayed in the home hook.

 

Everything works perfectly with 

public function hookHome($params) {
    return $this->hookLeftColumn($params);
}

Anyway, I want the home_hook to display a different template than the template linked to hookLeftColumn (which I'd like to keep intact, it might be useful for later); also, I don't want any other rss module than the home_hook one to be displayed at this time so I "unhooked" the other ones. I copied-pasted the code from function hookLeftColumn into function hookHome => that doesn't work.

 

It seems that there's an issue with 

foreach (self::$xmlFields as $xmlField)
  $xmlValues[$xmlField] = $item->__get($xmlField);

which returns an internal server error

 

Notes:

 

I imported the necessary files PEAR.php and Parser.php

 

Any idea?

Edited by Kateriine (see edit history)
Link to comment
Share on other sites

  • 2 weeks later...

Hi

 

And  welcome to the forum. 

 

First thing about internal server error, maybe you know this but just to check. You can turn debug mode on 

when you see that error to see exactly what is wrong 

https://www.prestashop.com/forums/topic/224525-how-to-turn-on-error-reporting-for-debug-information-blank-page-500-internal-server-error/

 

But you are right it is something with code you mention. Variable $xmlFields is defined as private, and explanation you can find here

http://build.prestashop.com/howtos/module/how-to-override-modules/

 

 

This is a design issue. Core modules were written long before the introduction of module overrides. Difference is that private scope “locks down” class features, while protected scope is less strict and allows overrides by a child class.

 

but here is solution, maybe not elegant but it works. Used new variable.

<?php

if (!defined('_PS_VERSION_'))
	exit;

include_once(_PS_CLASS_DIR_.'../tools/pear/PEAR.php');
include_once(_PS_PEAR_XML_PARSER_PATH_.'Parser.php');

class BlockrssOverride extends Blockrss
{
	protected static $xmlFieldsHome = array('title', 'guid', 'description', 'author', 'comments', 'pubDate', 'source', 'link', 'content');
	public function hookDisplayHome()
	{
		$title = strval(Configuration::get('RSS_FEED_TITLE'));
		$url = strval(Configuration::get('RSS_FEED_URL'));
		$nb = (int)(Configuration::get('RSS_FEED_NBR'));
		$cacheId = $this->getCacheId($this->name.'-'.date("YmdH"));
		if (!$this->isCached('blockrss-home.tpl', $cacheId))
		{
			// Getting data
			$rss_links = array();
			if ($url && ($contents = Tools::file_get_contents($url)))
				try
				{
					if (@$src = new XML_Feed_Parser($contents))
						for ($i = 0; $i < ($nb ? $nb : 5); $i++)
							if (@$item = $src->getEntryByOffset($i))
							{
								$xmlValues = array();
								foreach (self::$xmlFieldsHome as $xmlField)
									$xmlValues[$xmlField] = $item->__get($xmlField);
								$xmlValues['enclosure'] = $item->getEnclosure();
								// First image
								if (isset($xmlValues['content']) && $xmlValues['content'])
								{
									preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/', $xmlValues['content'], $image);
									if (array_key_exists(1, $image) && $image[1])
									{
										if ($image[1][0] == '/')
											$image[1] = 'http:'.$image[1];
										// Try if distant image exist : timeout 0.3s
										$context = stream_context_create(array('http' => array('timeout' => 0.3)));
										if (file_get_contents($image[1], false, $context, -1, 1) !== false)
											$xmlValues['image'] = $image[1];
									}
								}
								// Compatibility
								$xmlValues['url'] = $xmlValues['link'];
								$rss_links[] = $xmlValues;
							}
				}
				catch (XML_Feed_Parser_Exception $e)
				{
					Tools::dieOrLog(sprintf($this->l('Error: invalid RSS feed in "blockrss" module: %s'), $e->getMessage()), false);
				}
			// Display smarty
			$this->smarty->assign(array('title' => ($title ? $title : $this->l('RSS feed')), 'rss_links' => $rss_links));
		}
 	 	return $this->display(__FILE__, 'blockrss-home.tpl', $cacheId);
	}
}

This is in override/modules/blockrss/blockrss.php.  Changes are, I put variable xmlFieldsHome as protected with same values, and changed tpl to blockrss-home.

Placed that file tpl file in theme/modules/blockrss folder and hooked module in back office to displayHome hook. This worked for me, if you have cache enabled clear

cache in advanced parameters > performance . 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...