Magento 2 Theme Header – Customizing Welcome Message

headermagento2xml

I've been learning the ropes in theming in magento 2, I've started to get the hang of it with my own theme using Snowdog as my parent and styling in sass.

My question is, I came to remove the welcome message yesterday and couldn't find any xml related to this. So I went in search and found it in the header.phtml file.

 <?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/**
 * @var \Magento\Theme\Block\Html\Header $block
 */
$welcomeMessage = $block->getWelcome();
?>
<?php switch ($block->getShowPart()):
    case 'welcome': ?>
        <li class="greet welcome" data-bind="scope: 'customer'">
            <!-- ko if: customer().fullname  -->
            <span data-bind="text: new String('<?php echo $block->escapeHtml(__('Welcome, %1!', '%1'));?>').replace('%1', customer().firstname)">
            </span>
            <!-- /ko -->
            <!-- ko ifnot: customer().fullname  -->
            <span data-bind="html:'<?=$block->escapeHtml($welcomeMessage) ?>'"></span>
            <!-- /ko -->
        </li>
        <script type="text/x-magento-init">
        {
            "*": {
                "Magento_Ui/js/core/app": {
                    "components": {
                        "customer": {
                            "component": "Magento_Customer/js/view/customer"
                        }
                    }
                }
            }
        }
        </script>
    <?php break; ?>

    <?php case 'other': ?>
        <?php echo $block->getChildHtml(); ?>
    <?php break; ?>

<?php endswitch; ?>

But then i realised there isn't any other php or html inside of this file, the whole of the header is built with xml.

Why is it built with xml? Are there any benefits to this? Should I continue to use xml, or should i look at doing it with php – html?

Thanks.

Best Answer

With regards to the XML, that block is called header and is set in

\vendor\magento\module-theme\view\frontend\layout\default.xml

You can remove it via a theme override at

\app\design\frontend\YOURNAME\YOURTHEME\Magento_Theme\layout\default.xml

with the following xml:

<referenceBlock name="header" remove="true"/>

This seems to be the only thing that block is used for, though I do have a slight wariness that it's called "header" rather than "welcome" or something that suggests such a simple purpose. That said, a code search does appear to confirm that this is the only use.

As for "why is it built using xml" - I'd suggest a read of the official documentation - http://devdocs.magento.com/guides/v2.0/frontend-dev-guide/layouts/layout-overview.html

Dividing the page up into lots of individual XML blocks is a tidier solution than large PHP includes, even if it is fiddler to learn than, for example, the messier, more forgiving, approach of Wordpress.

IMPORTANT UPDATE: I've noticed an oddity in that removing this and the breadcrumbs seems to stop the meta tags working. See: Removing welcome message and breadcrumbs stops the title tag appearing on category page This may mean that overriding the template file is the better approach (or just use css to hide it), depending on the cause.