0

Hi I create a custom module that lets people who are logged in and have an attribute shows a button. I would like to do it all from the extension without touching the default.xml. I would like to reference the container and place a block inside of it but it does not seem to be working. Here is the block:

<?php

namespace WorldSynergy\LoginPortal\Block;

use Magento\Customer\Model\Url;
use Magento\Framework\App\Http\Context;
use Magento\Framework\View\Element\Template;
use Magento\Sales\Model\ResourceModel\Order\Customer\Collection;
{

class Portal extends Template
{
    protected $_customerUrl;
    protected $_customer;
    protected $httpContext;
    /**
     * Constructor
     *
     * @param \Magento\Framework\View\Element\Template\Context  $context
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Sales\Model\ResourceModel\Order\Customer\Collection $customer,
        Url $customerUrl,
        Context $httpContext,
        array $data = []
    ) {
        $this->_customer = $customer;
        $this->_customerUrl = $customerUrl;
        $this->httpContext = $httpContext;

        parent::__construct($context, $data);
    }

    /**
     * @return string
     */
    public function isLoggedIn()
    {
        if $this->$_customer->create()->addAttributeToSelect('is_allowed',array('eq'=>1)) {
            return $this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
        }
    }
}

here is the layout file located at vendor/namespace/view/frontend/layout

<?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="header.container">
            <block as="portal" class="vendor\namespace\Block\Portal" name="portal" template="vendor_namespce::portal.phtml"/>
        </referenceContainer>
    </body>
</page>

here is the custom .phtml file just saying hello now.

<?php
/**

 */
?>
<div>
    <?=// $block->isLoggedIn() ?>
    hello
</div>

for some reason it is not placing it in the container and was wondering if some could point me to the right place.

here is the defualt.xml

<?xml version="1.0"?>
<page layout="3columns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <update handle="default_head_blocks"/>
    <body>
        <!-- Inline scripts -->
        <referenceContainer name="before.body.end">
            <block class="Magento\Translation\Block\Js" name="translate" template="Magento_Translation::translate.phtml"/>
            <block class="Magento\Framework\View\Element\Js\Cookie" name="js_cookies" template="Magento_Theme::js/cookie.phtml"/>
            <block class="Magento\Theme\Block\Html\Notices" name="global_notices" template="html/notices.phtml"/>
            <block class="Magento\Framework\View\Element\Template" name="theme.js" before="-" template="Magento_Theme::jsconfigurator.phtml" />
            <block class="Magento\Framework\View\Element\Template" name="theme_child.js" before="-" template="Magento_Theme::jsconfigurator_child.phtml" />
        </referenceContainer>
        <!-- Inline scripts END -->

        <!-- Header. RD-Navbar -->
        <referenceContainer name="header.container">
            <container name="rd.navbar-wrap" htmlTag="div" htmlClass="rd-navbar">
                <container name="rd.navbar" htmlTag="div" htmlClass="rd-navbar-panel layout_1" />
            </container>
        </referenceContainer>

        <referenceBlock name="skip_to_content" remove="true" />
        <!-- Header. RD-Navbar END -->

        <!-- Navbar Collapse -->
        <block class="Magento\Framework\View\Element\Template" name="navbar.collapse" template="Magento_Theme::rd-navbar-collapse.phtml"/>
        <move element="navbar.collapse" destination="rd.navbar.collapse" before="-" />
        <!-- Navbar Collapse End -->

        <!-- Header Panel -->
        <referenceContainer name="header.panel" htmlClass="panel page-header__panel">
            <container name="header.panel.grid-container" htmlClass="container" htmlTag="div" before="-">
                <container name="header.switchers" htmlClass="header-switchers" htmlTag="div" before="-"/>
                <container name="panel.container.wrap" htmlClass="panel-container-wrap" htmlTag="div"/>
            </container>
        </referenceContainer>
        <!-- Header Panel END -->

        <!-- Header Content -->
        <referenceContainer name="header-wrapper" htmlClass="page-header__content">
            <container name="header-wrapper.grid-container" htmlClass="container" htmlTag="div" before="-"/>
        </referenceContainer>
        <!-- Header Content END -->

        <!-- Navbar Collapse -->
        <container name="rd.navbar.collapse" htmlClass="rd-navbar-collapse-container" htmlTag="div" after="-">
            <container name="rd.navbar.collapse.items" htmlClass="rd-navbar-collapse" htmlTag="div">
                <block class="Magento\Store\Block\Switcher" name="store_language_mobile" as="store_language" template="switch/languages_mobile.phtml"/>
                <block class="Magento\Directory\Block\Currency" name="currency_mobile" before="store_language" template="currency_mobile.phtml" after="store_language_mobile"/>
            </container>
        </container>

        <move element="top.links" destination="rd.navbar.collapse.items"/>
        <!-- Navbar Collapse End -->

        <referenceBlock name="navigation.sections" remove="true" />

Any help is appreciated and thanks!

  • Can you tell me where you want to add those things? – Dhiren Vasoya Sep 6 at 13:56
  • I would like to add it the header.container. Thanks! – WS_Marc Sep 6 at 14:15
  • Do you want to add your custom phtml file, right? – Dhiren Vasoya Sep 6 at 14:19
  • Then do the code into custom.phtml file instade of hi. – Dhiren Vasoya Sep 6 at 14:36
  • It is not showing the hi. I just put the hello there to test if would work. Its not displaying – WS_Marc Sep 6 at 14:37
0

Normally on the block, I usually this on top of my block:

namespace vendor\namespace\Block;

use Magento\Framework\View\Element\Template;
use Magento\Widget\Block\BlockInterface; 

class YOUR_CLASS_NAME extends Template implements BlockInterface {

And in your default.xml I didn't find where you call your block like:

<block class="Magento\Framework\View\Element\Template" name="portal" template="Vendor_Namespace::your_file.phtml">

And pay attention to capital letters because it makes a difference.

Are you sure you are not missing to add some file?

You can see this tutorial, it's very helpful.

  • Thanks, I will look into the implements. I am trying not to override the default.xml because i need this to be an extension i can move to multiple sites. Is there a way to do that? – WS_Marc Sep 6 at 14:57
  • @WS_Marc I didn't create extensions yet. I just create custom blocks and widgets to custom themes. – BraDev Sep 6 at 15:03
  • @WS_Marc reading the DevDoc on Magento website they suggest you to use theme inheritance to extend file (not override). devdocs.magento.com/guides/v2.3/frontend-dev-guide/themes/… – BraDev Sep 6 at 15:28
  • @WS_Marc if my tips help you please let me know to edit my answer, then you can check as the right answer. – BraDev Sep 6 at 15:31
  • @BraDev...Thanks for input but that does not do what I looking for. All the Best! – WS_Marc Sep 6 at 16:06
0

As my theme has the inheritance of Blank Magento Theme I created a default.xml on vendor/theme/Magento_Theme/layout and not reset or override everything.

My file looks like this:

<?xml version="1.0"?>
<page layout="3columns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="default_head_blocks"/>
    <body>
        <referenceContainer name="header.container" htmlClass="custom-header">
            <container name="custom_header_top_content" htmlTag="div" htmlClass="custom-header_top-content" before="-">
                <block class="Magento\Framework\View\Element\Template" name="custom_header_feefo_reviews" before="-" template="TheTrade_FeefoReviews::widget/service_simple.phtml">
                    <arguments>
                        <argument name="style" xsi:type="string">Service Simple</argument>
                    </arguments>
                </block>
            </container>
        </referenceContainer>
    </body>
</page>

Observe that I didn't duplicate the default default.xml. I'm just saying that I want to add and where I want to add this Feefo Review widget. And inside of the correspondent container, I set where I want to add using before/after in the element.

PS.: I know that in my case is a widget, but is a widget inside a block and the inheritance works quite similar.

I hope to help you now.

I used all link that I already share with you and this: https://www.mageplaza.com/devdocs/magento-2-create-widget/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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