Jump to content

Are we getting modules overrides?


NemoPS

Recommended Posts

EDIT: my tutorial: http://nemops.com/override-prestashop-modules-core/#.VLzfbkejOr1

 

Hey everyone.

Razaro, another user of the forum, just noticed some code in module.php where overrides are mentioned, this:

	public static function getInstanceByName($module_name)
	{
		if (!Validate::isModuleName($module_name))
		{
			if (_PS_MODE_DEV_)
				die(Tools::displayError(Tools::safeOutput($module_name).' is not a valid module name.'));
			return false;
		}
		if (!isset(self::$_INSTANCE[$module_name]))
		{
			if (Tools::file_exists_no_cache(_PS_MODULE_DIR_.$module_name.'/'.$module_name.'.php'))
			{
				include_once(_PS_MODULE_DIR_.$module_name.'/'.$module_name.'.php');
				if (Tools::file_exists_no_cache(_PS_OVERRIDE_DIR_.'modules/'.$module_name.'/'.$module_name.'.php'))
				{
					include_once(_PS_OVERRIDE_DIR_.'modules/'.$module_name.'/'.$module_name.'.php');
					$override = $module_name.'Override';
					if (class_exists($override, false))
						return self::$_INSTANCE[$module_name] = new $override;
				}
				if (class_exists($module_name, false))
					return self::$_INSTANCE[$module_name] = new $module_name;
			}
			return false;
		}
		return self::$_INSTANCE[$module_name];
	}

In presta .11. It was not there in .09 so do we have to presume we can now override modules' php files as well? I didn't test .11 yet so that's why i'm asking, in case anyone did. Of course it's not mentioned in the changelog

Link to comment
Share on other sites

  • 2 weeks later...

Hello Nemo

 

I followed your tutorial but I don't get it to work. I want to override blocktopmenu. My Code is:

<?php
 
if (!defined('_CAN_LOAD_FILES_'))
    exit;
 
class BlockTopMenuOverride extends BlockTopMenu
{
 
 
 private function getCMSMenuItems($parent, $depth = 1, $id_lang = false)
	{
		$id_lang = $id_lang ? (int)$id_lang : (int)Context::getContext()->language->id;

		if ($depth > 3)
			return;

		$categories = $this->getCMSCategories(false, (int)$parent, (int)$id_lang);
		$pages = $this->getCMSPages((int)$parent);

		if (count($categories) || count($pages))
		{
			$this->_menu .= '<ul>';

			foreach ($categories as $category)
			{
				$cat = new CMSCategory((int)$category['id_cms_category'], (int)$id_lang);
				
				/* Genzo Change */
				
				if ($cat->id==5 ) {
				$this->_menu .= '<li>';
					if ($_SERVER['SERVER_NAME']=="www.chesspoint.ch" or "www.webpoint-schiendorfer.ch") { /* man benötigt diese Condition zur Zeit eigentlich nicht, aber es könnte hilfreich sein, wenn man mal mit mehreren Shops arbeitet */
					$this->_menu .= '<a href="/blog">'.$category['name'].'</a>';
				}
				
				
				
				
				else { $this->_menu .= '<a href="#">'.$category['name'].'</a>'; }
					
				$this->getCMSMenuItems($category['id_cms_category'], (int)$depth + 1);
				$this->_menu .= '</li>';
				
				}
				else {
				/* End Genzo Change */
				
				$this->_menu .= '<li>';
				$this->_menu .= '<a href="'.Tools::HtmlEntitiesUTF8($cat->getLink()).'">'.$category['name'].'</a>';
				$this->getCMSMenuItems($category['id_cms_category'], (int)$depth + 1);
				$this->_menu .= '</li>';
				}
			
			/* Genzo Change */
			}
			/* End Genzo Change */
			
			
			
/* Genzo Change Original 
			foreach ($pages as $page)
			{
				$cms = new CMS($page['id_cms'], (int)$id_lang);
				$links = $cms->getLinks((int)$id_lang, array((int)$cms->id));

				$selected = ($this->page_name == 'cms' && ((int)Tools::getValue('id_cms') == $page['id_cms'])) ? ' class="sfHoverForce"' : '';
				$this->_menu .= '<li '.$selected.'>';
				$this->_menu .= '<a href="'.$links[0]['link'].'">'.$cms->meta_title.'</a>';
				$this->_menu .= '</li>';
			}*/
			
			/* Genzo Change */
			foreach ($pages as $page)
			{
				$cms = new CMS($page['id_cms'], (int)$id_lang);
				$links = $cms->getLinks((int)$id_lang, array((int)$cms->id));

				$selected = ($this->page_name == 'cms' && ((int)Tools::getValue('id_cms') == $page['id_cms'])) ? ' class="sfHoverForce"' : '';
               
			   	$this->_menu .= '<li '.$selected.'>';
			    
				if ($cms->id==11){
				$this->_menu .= '<a href="/blog/chesspoint">'.$cms->meta_title.'</a>';
				}
				
				elseif ($cms->id==12){
				$this->_menu .= '<a href="/blog/schachturniere">'.$cms->meta_title.'</a>';
				} 
				
				elseif ($cms->id==13){
				$this->_menu .= '<a href="/blog/schachgeschichte">'.$cms->meta_title.'</a>';
				}
				
				elseif ($cms->id==7){
				$this->_menu .= '<a href="/faq">'.$cms->meta_title.'</a>';
				}
				
				else {
                $this->_menu .= '<a href="'.$links[0]['link'].'">'.$cms->meta_title.'</a>';
				 }
				
				$this->_menu .= '</li>';   
			}
			/* End Genzo Change */

			$this->_menu .= '</ul>';
		}
	}
}

Whats wrong with this override? I doesn't make anything :(

I tried to delete class_index.php, but this file always comes back. Is this normal?

Link to comment
Share on other sites

  • 3 weeks later...

ah! goot to know I'm not the only one...

 

Overriding blocktopmenu function generateCategoriesMenu with this code does not work

class BlocktopmenuOverride extends Blocktopmenu
{
	public function generateCategoriesMenu($categories, $is_children = 0)
	{
            die( var_dump ('hello I am an override') );
	}
}

if I comment the parent function it works as expected

 

I've asked why in this post but so far nobody can tell why...

 

https://www.prestashop.com/forums/topic/420461-cant-override-blocktopmenu-function-generatecategoriesmenu/

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...