Download the PHP package zero1/layout-xml-plus without Composer
On this page you can find all versions of the php package zero1/layout-xml-plus. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download zero1/layout-xml-plus
More information about zero1/layout-xml-plus
Files in zero1/layout-xml-plus
Package layout-xml-plus
Short Description Intercept block html output with layout xml, removing the need to override templates in most cases.
License GPL-3.0-or-later
Informations about the package layout-xml-plus
Zero1_LayoutXmlPlus
This module is aimed at reducing the need to override templates for blocks. For example, changing a single class for a button shouldn't require you to override the template. However for significant dom structure changes we would still recommend overriding the template.
Installation
Actions
The functionality has been split into "actions" different actions that can be taken on the output of a blocks html before it is passed to a user.
Common Parameters
action
: id of the action you wish to carry outxpath
: an xpath expressing to identify elements within the template. For compatibilty all templates are rendered inside a<root>
node, so if you wanted the first div you would want something like(/root/div)[1]
or 2nd div:(/root/div)[2]
or all divs/root//div
.
AttributeValueReplace
id: attribute_value_replace
Replace a value within an attributes value, a good example would be replacing a class with another class.
example
catalog_category_view.xml
AttributeValueSet
id: attribute_value_set
Completely override the value of an attribute
example
catalog_category_view.xml
AttributeValueAppend
id: attribute_value_append
Add an item to an attributes list of values (e.g add a class)
example
catalog_product_view.xml
example
catalog_product_view.xml
AttributeValueRemove
id: attribute_value_remove
Remove a value from an attributes list of values
example
catalog_product_view.xml
AttributeRemove
id: attribute_remove
Completely remove an attribute
example
catalog_product_view.xml
ChildHtml
id: child_html
Insert a childs html into a specific part of the output. This requires you to add the child block in layout xml.
Valid targets
start
: at the begining of the nodes content (before current children)end
: at the end of the nodes content (after current children)before
: before the current nodeafter
: after the current nodereplace
: replace the target node with the content of the block
example
default.xml
Remove
id: remove
Remove an element from the dom.
example
default.xml
XPath Cheat Sheet
- select all labels with a class of 'swatch-option'
/root//label[contains(@class, 'swatch-option')]
Other Recommendations
When it comes to stopping blocks outputting content, using layout xml can often remove the requirement to change the template. N.B when referencing a block you must use it's name and not it's alias Example dont do
do
CLI Commands
Module Status
Disabling the module is a quick way to debug if layout-xml is responsible for a change you are seeing.
- Show module status:
php bin/magento dev:layout-xml-plus:status
- Disable module:
php bin/magento dev:layout-xml-plus:status --disable
- Enable module:
php bin/magento dev:layout-xml-plus:status --enable
Logging
Enabling logging will cause the module to log out all changes to output, as well as fails to change output (i.e when the xpath doesn't match anything in the content)
- Show logging status:
php bin/magento dev:layout-xml-plus:logging
- Disable logging:
php bin/magento dev:layout-xml-plus:logging --disable
- Enable logging:
php bin/magento dev:layout-xml-plus:logging --enable
When enabled logging will output all blocks that have any layout-xml-plus directives into var/layout-xml-plus/logging/*
Each block will be stored as:
NAME_IN_LAYOUT.orig.html
- the original content of the blockNAME_IN_LAYOUT.new.html
- the content after modification
Collection / Evaluation
The module also includes as way to find template overides that can be replaced with layout xml directives.
-
Magento setup ``bash php bin/magento deploy:mode:set developer \ && php bin/magento cache:enable \ && php bin/magento cache:flush bash php bin/magento dev:layout-xml-plus:collect --with-theme --clear bash php bin/magento dev:layout-xml-plus:collect --without-theme --clear \ && php bin/magento cache:flush bash find ./app/design/frontend/ -type f -name '*.phtml' -exec rm "{}" \; bash php bin/magento dev:layout-xml-plus:collect --without-theme --clear \ && php bin/magento cache:flush
1 exception(s): Exception #0 (Magento\Framework\Exception\ValidatorException): Invalid template file: 'Magento_Cms::static-blocks/pagetop.phtml' in module: '' block's name: 'pagetop'
git checkout app/design/frontend/VENDOR/THEME/Magento_Cms/templates/static-blocks/pagetop.phtml bash php bin/magento dev:layout-xml-plus:collect --disable bash git checkout app/design/frontend bash php bin/magento dev:layout-xml-plus:analyse
var xpathExists = (xpath) => !!document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
pathExists('//header/div//div[@class="flex gap-4 items-center"]/div[@class="relative hidden lg:inline-block mr-4"][1]/a')
Before moving this into your layout file be sure to prepend the XPath value with '/root'
## Roadmap
- [x] make module disable-able
- [x] make log flag setable/env'able
- [x] record blocks and output, with/without theme
- [ ] generate analysis report.
- [ ] make play nice with hyva when prod mode (ccs classes)
- [ ] unit tests
- [ ] coding standards
- [ ] initial release
**Potentials**
- [ ] profiler with autowarning when block takes excessive time?
SDS testing
https://www-sdslondon-co-uk-21843.54.mdoq.dev/
https://www-sdslondon-co-uk-21843.54.mdoq.dev/british-made-bronze-door-knobs.html
https://www-sdslondon-co-uk-21843.54.mdoq.dev/bronze-beehive-morticerim-door-knob-50-mm.html
enable with theme
php bin/magento deploy:mode:set developer \
&& php bin/magento cache:enable \
&& php bin/magento cache:flush \
&& git checkout app/design/frontend \
&& php bin/magento dev:layout-xml-plus:collect --with-theme --clear
enabled without theme
php bin/magento deploy:mode:set developer \
&& php bin/magento cache:enable \
&& php bin/magento cache:flush \
&& php bin/magento dev:layout-xml-plus:collect --without-theme --clear \
&& find ./app/design/frontend/ -type f -name '*.phtml' -exec rm "{}" \; \
&& git checkout app/design/frontend/z1/sds_hyva/Magento_Cms/templates/static-blocks/pagetop.phtml \
&& git checkout app/design/frontend/z1/sds_hyva/Magento_Theme/templates/html/header/menu/C-desktop.phtml \
&& git checkout app/design/frontend/z1/sds_hyva/Magento_Theme/templates/html/header/menu/C-desktop-item.phtml \
&& git checkout app/design/frontend/z1/sds_hyva/Magento_Cms/templates/static-blocks/usps.phtml \
&& git checkout app/design/frontend/z1/sds_hyva/Magento_Theme/templates/html/tradewidget.phtml \
&& git checkout app/design/frontend/z1/sds_hyva/Klaviyo_Reclaim/templates/product/viewed_hyva.phtml \
&& git checkout app/design/frontend/z1/sds_hyva/Hyva_Checkout/templates/section/custom-summary-header.phtml
disable
php bin/magento deploy:mode:set developer \
&& php bin/magento cache:enable \
&& php bin/magento cache:flush \
&& git checkout app/design/frontend \
&& php bin/magento dev:layout-xml-plus:collect --disable
Analyze
php bin/magento dev:layout-xml-plus:analyse