0

I can not render my custom block within my custom template,

Point is on newsletter subscriber, I extend newsletter_subscriber_form.xml,

Add container within my block but it says that template is invalid,

When is added in existing reference block nothing happened,

When I put a template in an existing block of this layout

(e.q. Magento\Backend\Block\Widget\Grid\Massaction)
It renders but I need my custom functionality.

Layout block

 <container name="content">
            <block class="Pool\Module\Block\Adminhtml\Subscriber\Button" name="button" template="Pool_Module:button.phtml" />
 </container>

path of template is on Pool/Module/view/adminhtml/templates/button.phtml

Block

<?php
namespace Pool\Module\Block\Adminhtml\Subscriber;

use Magento\Framework\View\Element\Template;

class Button extends Template
{
    public function __construct(Template\Context $context, array $data = [])
    {
        parent::__construct($context, $data);
    }

    public function addNewSubscriber()
    {
        var_dump('hi');
    }
}

2 Answers 2

0

please try to use below code.

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Pool\Module\Block\Adminhtml\Subscriber\Button" name="button" template="Pool_Module:button.phtml" />
        </referenceContainer>
    </body>
</page>
1
  • Exception #0 (Magento\Framework\Exception\ValidatorException): Invalid template file. NOTE: <container name="content"> is added by me, he is not existing in certain layout.
    – Newb
    Feb 7, 2019 at 12:08
0
  1. Update your layout XML file (newsletter_subscriber_form.xml) to include your custom block correctly:

    <container name="content">
         <block class="Pool\Module\Block\Adminhtml\Subscriber\Button" name="button" template="Pool_Module::button.phtml" />
    </container>
    

    Make sure that your XML file is located in the correct directory: view/adminhtml/layout/newsletter_subscriber_form.xml.

  2. Update your block class to extend \Magento\Framework\View\Element\Template and implement the getAddNewSubscriberUrl method:

    <?php
    
       namespace Pool\Module\Block\Adminhtml\Subscriber;
    
       use Magento\Framework\View\Element\Template;
    
       class Button extends Template
       {
          /**
           * Get URL for adding a new subscriber
           *
           * @return string
           */
         public function getAddNewSubscriberUrl()
         {
            return $this->getUrl('pool_module/subscriber/add');
         }
       }
    
  3. Create the button.phtml template file in the correct directory: view/adminhtml/templates/button.phtml

    <button type="button" onclick="location.href='<?php echo $block->escapeUrl($block->getAddNewSubscriberUrl()) ?>';">
    

In this example, I've used a button that redirects to a custom URL (pool_module/subscriber/add). You should replace it with the URL you want to use.

Your Answer

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

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