Hot answers tagged

78

In more recent versions of Magento2, the remove method is now: <referenceBlock name="block_name" remove="true"/> Example: <?xml version="1.0"?> <page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/...


69

Of course, it is possible: <referenceBlock name="copyright"> <action method="setTemplate"> <argument name="template" xsi:type="string">Dfr_Backend::page/copyright.phtml</argument> </action> </referenceBlock>


69

Try like this. For ex your block class is <?php namespace Company\Helloworld\Block; use Magento\Framework\View\Element\Template; class Main extends Template { public function getMyCustomMethod() { return '<b>I Am From MyCustomMethod</b>'; } } then in any phtml file you can use following code to get method of this block....


48

Action node is deprecated, but you can use block arguments <referenceBlock name="block_to_change"> <arguments> <argument name="template" xsi:type="string">[Vendor]_[Module]::/path/to/template.phtml</argument> </arguments> </referenceBlock>


44

The escaping methods in AbstractBlock all delegate calls to Magento\Framework\Escaper, so you'll find an overview there. Let's look at the public methods and their documentation: escapeHtml() /** * Escape string for HTML context. allowedTags will not be escaped, except the following: script, img, embed, * iframe, video, source, object, audio * * @...


43

The problem is that your constructor does not match the parent class constructor. To fix that you need to update your constructor: public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Customer\Model\Session $customerSession, \Magento\Framework\ObjectManagerInterface $objectManager, array $data = ...


40

There is a new move node in the layout XML that we have access to in M2. This node sets the declared block or container element as a child of another element in the specified order. Example: <move element="name.of.an.element" destination="name.of.destination.element" as="new_alias" after="name.of.element.after" before="name.of.element.before"/> In ...


34

To understand the difference between <arguments> and <action> you must understand how the constructors of Magento 2 objects work. If you override a constructor in Magento, you'll always get a $data-parameter which is an array. This is the data as provided in the XML files and translated to the internal $_data-array of \Magento\Framework\...


34

Although other answers are correct, they're not the recommended/proper solution either. Using the ObjectManager is absolutely prohibited in Magento 2. So please don't rely on this solution, but use proper DI to achieve this instead. To learn how to use DI in Magento 2, see this resource: http://devdocs.magento.com/guides/v2.0/extension-dev-guide/depend-inj....


33

You can try <reference name="needed block"> <action method="setCacheLifetime"><s>null</s></action> </reference> or <reference name="needed block"> <action method="setCacheLifetime" /> </reference> to set it to null. But probably it is better to use ajax. There is a ...


33

In a block In Magento 2, you can use the \Magento\Store\Model\StoreManagerInterface which is stored in an accessible variable $_storeManager for every class extending \Magento\Framework\View\Element\Template so most of the block classes (Template, Messages, Redirect block types but not Text nor TextList). This way in your block, you can directly type the ...


29

I would like to add my option as opposed to benmarks' answer. My approach is to use the append action: <block type="core/template" name="my_block" template="my/block.phtml" /> <reference name="head"> <action method="append" ifconfig="myblock/general/enabled"><block>my_block</block></action> </...


29

This message means that one of the blocks that is used in your Magento store is not on the whitelist. With Security Patch SUPEE-6788 and Magento CE 1.9.2.2 a new whitelist for blocks was introduced. Magento now includes a white list of allowed blocks or directives. If a module or extension uses variables like {{config path=”web/unsecure/base_url”}} and {{...


28

getBlockHtml('block_name_here') get's you the html of the block with name block_name_here if it finds it in the layout...anywhere. getChildHtml('block_name_here') get's you the html of the block with name block_name_here only if that block is a child of the current block. Here is an example. Consider the next layout section <block type="core/template" ...


27

It's hard to say with the information you provided, but here's how I'd debug this. You didn't mention which file you were having this problem in, but it's probably this part of the base widget grid container that's your problem #File: app/code/core/Mage/Adminhtml/Block/Widget/Grid/Container.php protected function _prepareLayout() { $this->setChild( '...


25

Create a static block for each language, all with the same identifier. Render the block with the cms/block block. It will automatically add the store ID to load the correct version of the block. Here's an easy way to load and render the block directly in the template file: <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('...


25

The most relevant parts for you in this case are: Template file (.phtml) and Blocks While the template files include the basic HTML output, which is a combination of PHP and HTML (thus .phtml), the blocks are interacting with the Magento system to retrieve data from models. Example: The file which creates the HTML output for the product pages is app/...


25

Magento 2.1 The block mentioned below is now Magento\Checkout\Block\Onepage\Success Magento 2.0 The only thing you can retrieve natively on this page is the order id using the getRealOrderId() method defined in Magento\Checkout\Block\Success Thus, to get the order id you can call the following in your template: $block->getRealOrderId(); However, I ...


24

You need to use the argument directly without the array. Instead of <arguments> <argument name="data" xsi:type="array"> <item name="text" xsi:type="string">About Us</item> </argument> </arguments> You need: <arguments> <argument translate="true" name="text" xsi:type="...


23

These are all located in your theme directory. If looks like you are talking specifically about the product details page, so the main one to change would be located in your themes catalog/product/view.phtml. Since you do seem to be brand new to Magento, I would recommend you get to understand the basic design concepts of Magento. This guide should help ...


23

Its almost the very same way <referenceBlock name="block.name.wantoberemoved" remove="true"/> Remove compare products from sidebar <referenceBlock name="catalog.compare.sidebar" remove="true" /> Remove Wishlist from sidebar <referenceBlock name="wishlist_sidebar" remove="true" />


23

Every block, or group of blocks is declared in the config.xml file of a module like this (inside the <global> tag). Here is an example from the catalog module <blocks><!-- marks definition of a block group --> <catalog><!-- unique alias for blocks in the module --> <class>Mage_Catalog_Block</...


21

<remove name="" /> operates in the global block scope of Mage_Core_Model_Layout. These are collected in Mage_Core_Model_Layout::generateXml() and used to prevent the block with the specified name from even being instantiated. Also, any child blocks declared for a <remove />ed block will also not be instantiated. <action method="" /> a.k.a ...


20

Based on the stack trace you posted, it looks like this is happening when Magento is loading the layout update xml files, and using them to create the blocks which will render the HTML for the page. These lines appear to be the problem #1 /home/xxxxxxx/public_html/app/code/core/Mage/Core/Model/Layout.php(437): Mage_Core_Model_Layout->_getBlockInstance(''...


20

OUTDATED: ANSWER REFERS TO MAGENTO 2 ALPHA I wish it was that easy. It depends on where you want to instantiate it from. If you want to create an instance from inside an other block do it like this: $this->getLayout()->createBlock('Full\Block\Class\Name\Here'); From inside a controller do this: $this->_view->getLayout()->createBlock('...


20

Override block: Create your own module under app/code folder. We can use preference to override the class in Magento 2. app/code/Vendor/Module/etc/di.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="...


20

Here I give example to show custom block above shipping method of checkout 1) Create di.xml at app/code/Vendor/Module/etc/frontend/di.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\...


19

This looks correct. Troubleshooting steps, in order; do each one independently: Clear cache. Disable compiler. var_dump(Mage::getConfig()->getBlockClassName('infeentyinvoice/form')); - If this returns "Mage_Infeentyinvoice_Block_Form", your module config XML is not being merged: Enable developer mode (Mage::setIsDeveloperMode(true)) and PHP error ...


19

The class you are trying to extend has this constructor: public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageHelper, array $data = [] ) { $this->_fileStorageHelper = $fileStorageHelper; parent::__construct($context, $data); } so you ...


18

If you look further in the code, in the \Magento\Framework\View\Layout\Generator\Block class, the createBlock function only adds data from the $arguements['data'] element. So, I think you should change your code to this: $block = $this->frameworkViewLayout ->createBlock( "Company\Module\Block\Hello", "block_name", [ ...


Only top voted, non community-wiki answers of a minimum length are eligible