Set template from override controller with Prestashop 1.7 - php

I overrieded a controller in Prestashop 1.7 like this :
/override/controllers/front/MyAccountController.php
class MyAccountController extends MyAccountControllerCore
{
/**
* Assign template vars related to page content
* #see FrontController::initContent()
*/
public function initContent()
{
$this->context->smarty->assign([
'logout_url' => $this->context->link->getPageLink('index', true, null, 'mylogout')
]);
parent::initContent();
$this->setTemplate("module:configurateur/views/templates/front/my-account.tpl");
}
}
So I'm trying to call a view in my custom module "configurateur" with this line :
$this->setTemplate("module:configurateur/views/templates/front/my-account.tpl");
This file exists and is in the right folder (I think) :
\modules\configurateur\views\templates\front\my-account.tpl
When I try to load the page, I have this error :
No template found for module:configurateur/views/templates/front/my-account.tpl
at line 68 in file classes/Smarty/TemplateFinder.php
Can anyone tell my what's wrong please ?

The syntax "module:..." is only for ModuleFrontController objects, not for FrontController :
In your case your should use the hook DisplayOverrideTemplate or redirect the page myaccount to a module controller.

Related

Template not rendered from controller drupal 8

I have trouble rendering a template from a controller in a custom Drupal 8 module.
I am calling this controller method :
public function displayEngineUI() {
$build['#theme'] = 'bretagnecom-search-engine';
return $build;}
There is no problem reaching controller, i can var_dump inside. But the content of the template is not rendered.
My module file structure look like this :
bretagnecom_search_engine.module src
./src:
Controller
./src/Controller:
DefaultController.php
./templates:
bretagnecom-search-engine.html.twig
Any idea about what i'm doing wrong? I usually render a few html directly from controller with inline-template but i would like to isolate my html in his template file this time.
Thank's for the help everyone!
I guess template is not defined in hook_theme().
First just change hyphens to underscores:
public function displayEngineUI() {
$build['#theme'] = 'bretagnecom_search_engine';
return $build;
}
and in bretagnecom_search_engine.module add:
/**
* Implements hook_theme().
*/
function bretagnecom_search_engine_theme() {
$themes = [
'bretagnecom_search_engine' => [
'variables' => [
'your_custom_variable_1' => NULL,
'your_custom_variable_2' => NULL
]
];
If you do not have variables, just remove that part of code.
You can find more info here: https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-for-custom-module

Prestashop 1.7 set-template with header and footer

I am using prestashop 1.7 and have created a front-controller for my module. When I am using setTemplate, it does not include the header and footer just a blank page. I have assigned a page (in backoffice) to the module controller, and in the module, I am using following code:
/modules/somemodules/controllers/front/moduleslist.php:
class somemodulesmoduleslistModuleFrontController extends ModuleFrontController
{
public function initContent(){
$this->context->smarty->assign(array(
'id' => 1,
));
$this->setTemplate('module:somemodules/views/templates/front/find-modules.tpl');
}
}
What I have tried in the template file:
/modules/somemodules/views/templates/front/find-modules.tpl:
{extends file='page.tpl'}
{block name='page_content'}
{{$id}}
{/block}
But now errors is like, undifined language, undifined page etc.
Is there a better way of doing this, rather than re-defining all of these?
You also have to call the parent method so that all standard variables get initialized.
public function initContent()
{
parent::initContent();
$this->context->smarty->assign(array(
'id' => 1,
));
$this->setTemplate('module:somemodules/views/templates/front/find-modules.tpl');
}

Magento2 - Newbie Select product

I have a site running Magento 2.2.1. I need to create a very simple PHP page that will look up a given product. I want to look up the product based on the SKU and just print the price and product URL out.
I have NO idea how to even start this. I have tried using this to test loading a product with ID = 1
//Get Object Manager Instance
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
//Load product by product id
$product = $objectManager->create('Magento\Catalog\Model\Product')->load(1);
but all that does is throw an exeception that ObjectManager is not found. So I tried including the /app/bootstrap.php file beforehand, and that throws an error that the ObjectManager isn't initialized.
Can anyone provide me with a simple example that I can drop into the root of my site that will allow me to look up a single product by sku? Or point me in the direction of some useful documentation?
The solution to load a product programmatically in simple PHP file by using ObjectManager, but this solution is not recommended by Magento 2.
<?php
include('app/bootstrap.php');
use Magento\Framework\App\Bootstrap;
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$productId = 1;
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
echo $product->getName();
?>
Recommended Solution (Magento 2)
In Magento 2, the recommended way of loading product is by using ProductRepository and ProductFactory in a proper custom module instead of simple PHP file. Well, by using below (recommended) code, you can load product in your custom block.
ProductFactory Solution
<?php
namespace [Vendor_Name]\[Module_Name]\Block;
use Magento\Catalog\Model\ProductFactory;
class Product extends \Magento\Framework\View\Element\Template
{
protected $_productloader;
public function __construct(
ProductFactory $_productloader
) {
$this->_productloader = $_productloader;
}
public function getLoadProduct($id)
{
return $this->_productloader->create()->load($id);
}
}
In Magento 2.1
ProductRepository Solution
namespace [Vendor_Name]\[Module_Name]\Block;
use Magento\Catalog\Api\ProductRepositoryInterface;
class Product extends \Magento\Framework\View\Element\Template
{
protected $_productRepository;
public function __construct(
ProductRepositoryInterface $productRepository
) {
$this->_productRepository = $productRepository;
}
public function getProduct($id)
{
return $product = $this->productRepository->getById($id);
}
}
and, your .phtml file should look like this:
$productId = 1;
$product = $this->getLoadProduct($productId);
echo $product->getName();
I hope, you already know how to create a custom module in Magento 2 or if you want then just read this blog post How to create a basic module in Magento 2
You Cant just Load a Page By simple PHP file in magento Here is the procedure
1)create a layout file in you theme
2)register it inside layout.xml
3)add phtml to your layout file
4)add you code(in your question ) in that phtml file
the 2nd way is quite complecated
create module and in module controller render your code

How to override bundle resources in micro-kernel Symfony?

I've got micro-kernel Symfony project with custom catalog structure.
I used this: https://github.com/ikoene/symfony-micro
How can I override e.g. Twig Resources (Exception views)?
Cookbook says that I should create a directory called TwigBundle in my Resources directory.
I made \AppBundle\Resources\TwigBundle\views\Exception directory. Overriding view does not seem to work.
Thanks for using the microkernel setup. Here's how to override exception views.
1. Create a custom ExceptionController
First off, we're gonna create our own ExceptionController which extends the base ExceptionController. This will allow us to overwrite the template path.
<?php
namespace AppBundle\Controller\Exception;
use Symfony\Bundle\TwigBundle\Controller\ExceptionController as BaseExceptionController;
use Symfony\Component\HttpFoundation\Request;
class ExceptionController extends BaseExceptionController
{
/**
* #param Request $request
* #param string $format
* #param int $code
* #param bool $showException
*
* #return string
*/
protected function findTemplate(Request $request, $format, $code, $showException)
{
$name = $showException ? 'exception' : 'error';
if ($showException && 'html' == $format) {
$name = 'exception_full';
}
// For error pages, try to find a template for the specific HTTP status code and format
if (!$showException) {
$template = sprintf('AppBundle:Exception:%s%s.%s.twig', $name, $code, $format);
if ($this->templateExists($template)) {
return $template;
}
}
// try to find a template for the given format
$template = sprintf('#Twig/Exception/%s.%s.twig', $name, $format);
if ($this->templateExists($template)) {
return $template;
}
// default to a generic HTML exception
$request->setRequestFormat('html');
return sprintf('#Twig/Exception/%s.html.twig', $showException ? 'exception_full' : $name);
}
}
2. Create the error templates
Create templates for the different error codes:
error.html.twig
error403.html.twig
error404.html.twig
In this example, the exception templates would be placed in AppBundle/Resources/views/Exception/
3. Override the default ExceptionController
Now let's point to our new exception controller in the configuration.
twig:
exception_controller: app.exception_controller:showAction
I really like your solution, but I found another way how to do it without custom exception controller.
I realized that automatical additional check for overrided templates happens in directory Resources in the directory when you store your kernel class.
So, for structure in your repo it's:
/Resources/TwigBundle/views/Exception/
Finally I changed a little bit the directory structure to have a 'app' directory with kernel file inside. Just like in the default Symfony project.

Override Prestashop Module Changes not Visible

I am attempting to override a module in Prestashop but the changes are just not appearing.
I have overridden a template file and a controller so I have added the following files:
\override\modules\blockwishlist\views\templates\front\mywishlist.tpl
\override\modules\blockwishlist\controllers\front\mywishlist.php
These are very simple changes where I add another column to a table that contains a button. When the button is clicked the controller generates a CSV file.
Any idea why these changes are just not being shown?
Note: I have turned on 'Force Compile' and turned of caching.
Edit:
Re overridding a controller is it:
class BlockWishListMyWishListModuleFrontController extends BlockWishListMyWishListModuleFrontControllerCore // extends ModuleFrontController
or
class BlockWishListMyWishListModuleFrontControllerOverride extends BlockWishListMyWishListModuleFrontController
okay, I did some code research (maybe thre is exists easiest way, not sure), so:
in code Dispatcher class we have
// Dispatch module controller for front office
case self::FC_MODULE :
$module_name = Validate::isModuleName(Tools::getValue('module')) ? Tools::getValue('module') : '';
$module = Module::getInstanceByName($module_name);
$controller_class = 'PageNotFoundController';
if (Validate::isLoadedObject($module) && $module->active) {
$controllers = Dispatcher::getControllers(_PS_MODULE_DIR_.$module_name.'/controllers/front/');
if (isset($controllers[strtolower($this->controller)])) {
include_once(_PS_MODULE_DIR_.$module_name.'/controllers/front/'.$this->controller.'.php');
$controller_class = $module_name.$this->controller.'ModuleFrontController';
}
}
$params_hook_action_dispatcher = array('controller_type' => self::FC_FRONT, 'controller_class' => $controller_class, 'is_module' => 1);
break;
where we see that controllers loaded without overrides, but from other side below in code we see hook execution:
// Loading controller
$controller = Controller::getController($controller_class);
// Execute hook dispatcher
if (isset($params_hook_action_dispatcher)) {
Hook::exec('actionDispatcher', $params_hook_action_dispatcher);
}
so one of possible solution (without overriding core class) :
how to override module and hope you have core version >= 1.6.0.11
in blockwishlist.php in install() method add
this->registerHook('actionDispatcher')
to condition with other hooks, so it will looks like ... !this->registerHook('actionDispatcher') || ... because this hook not registered by default and we can't just place module there.
create method (can't beautify code here)
public function hookActionDispatcher($params)
{
if ('blockwishlistmywishlistModuleFrontController' == $params['controller_class']) {
include_once(_PS_OVERRIDE_DIR_ . 'modules/' . $this->name . '/controllers/front/mywishlist.php');
$controller = Controller::getController('BlockWishListMyWishListModuleFrontControllerOverride');
}
}
you already have override/modules/blockwishlist/controllers/front/mywishlist.php file by this path
reinstall module.
it works!
more about overriding some behaviors in docs
Turns out that to override a module you dont place your files in:
~/overrides/module/MODULE_NAME
Instead you place it in:
~/themes/MY_THEME/modules/MODULE_NAME
Now the changes are exhibiting themselves.
Does anyone know if/when the module is auto-updated, will my changes get lost/overrwritten?

Categories