1

OK SO I am making a custom module for our magento 2 store.

I have created a module which displays a " Download Product PDF Link " on every product page. But I need to make the link open a new page in a new tab when it is clicked.

My Layout XML located : Modzinc/dynamicpdf/View/frontend/layout/catalogue_product_view.xml

<?xml version="1.0"?>
<body>
<referenceContainer name="product.info.social">
    <block class="Modzinc\Productpdf\Block\Product\View\Link"
           name="modzinc.dynamicpdf.link"
           template="Modzinc_Productpdf::product/view/link.phtml"
           after="-">
    </block>
</referenceContainer>
</body>

MY Template File located at :

<?php



$linkTitle = $this->helper('Modzinc\Productpdf\Helper\Data')->getConfig('dynamicpdf/general/linkText');
$linkTextColor = $this->helper('Modzinc\Productpdf\Helper\Data')->getConfig('dynamicpdf/general/linkTextColor');
$linkBGColor = $this->helper('Modzinc\Productpdf\Helper\Data')->getConfig('dynamicpdf/general/linkBGColor');
$linkImage  = $this->helper('Modzinc\Productpdf\Helper\Data')->getConfig('dynamicpdf/general/linkImage');

###TODO make this more dynmic
$linkImage = "http://www.modzinc.co.uk/pub/media/modzinc/dynamicpdf/" .$linkImage;

##style the link and image properly

echo "<a class='action primary' style='background-color:$linkBGColor;color:$linkTextColor;'>";
if($linkImage!='') {
    echo "<img src='$linkImage'/>";
}

echo "$linkTitle </a>";

This all works great and produces the following in the following link as shown here : enter image description here

Now this is where I am stuck. I need to create a new page opens as "target_blank" in the browser and then I will use my custom code to generate a nice dynamic pdf for the product details.

If this was just a pure PHP application I would do something like this

"Download Product PDF";

And then I would generate the PDF on the pdfMaker.php page .

So I guess my question is how can I make my link open a new page in magento and also install this page when my module is uploaded.

Any pointers will be greatly appreciated

1 Answer 1

0

use target='_blank' will open link in new tab

echo "<a class='action primary' style='background-color:$linkBGColor;color:$linkTextColor;' target='_blank'>";
if($linkImage!='') {
    echo "<img src='$linkImage'/>";
}

echo "$linkTitle </a>";

For creating new page

Create frontend router

app/code/QaisarSatti/HelloWorld/etc/frontend/routes.xml

<?xml version="1.0"?>
<!--/**
* Simple Hello World Module
*
* @category QaisarSatti
* @package QaisarSatti_HelloWorld
* @author Muhammad Qaisar Satti
* @Email [email protected]
*
*/ -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="helloworld" frontName="helloworld">
<module name="QaisarSatti_HelloWorld" />
</route>
</router>
</config>

Create controller

app/code/QaisarSatti/HelloWorld/Controller/Index/Index.php

<?php
/**
* Simple Hello World Module
*
* @category QaisarSatti
* @package QaisarSatti_HelloWorld
* @author Muhammad Qaisar Satti
* @Email [email protected]
*
*/
namespace QaisarSatti\HelloWorld\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
public function execute()
{
echo "dsfds";
}
}

How to create CUSTOM extension in magento 2.1.6?

3
  • Thankyou , I know the target_blank will open it into a new tab but my problem is how to I create the new php page ( www.mysite.com/dynamicpfd.php ) in my module May 10, 2017 at 12:29
  • @BarryConnolly add the code for creating controller with router. May 10, 2017 at 12:33
  • Worked Perfectly Qaisar thanks for your help May 14, 2017 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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