Shawn Northrop
Shawn Northrop

Reputation: 6016

Magento 2: How do I override layouts.xml

I am trying to override the file app/code/Magento/Theme/view/frontend/layouts.xml

I do not want to extend this file, I want to override it so that some of the base design layouts are not available in the admin.

Upvotes: 1

Views: 1379

Answers (1)

Shawn Northrop
Shawn Northrop

Reputation: 6016

Instead of overriding layouts.xml I ended up doing the following

Create a new Module: app/code/<Vendor>/Cms

Create the file: app/code/<Vendor>/Cms/Model/PageLayout.php

<?php

namespace <Vendor>\Cms\Model;

use Magento\Cms\Model\Page\Source\PageLayout as BasePageLayout;

class PageLayout extends BasePageLayout{

    public function toOptionArray()
    {
        $options = parent::toOptionArray();
        $remove = [
            "empty",
            "1column",
            "2columns-left",
            "2columns-right",
            "3columns",
        ];

        foreach($options as $key => $layout){
            if(in_array($layout["value"], $remove)){
                unset($options[$key]);
            }
        }

        return $options;
    }
}

This will get the $options and then remove any that are in the $remove array based on the $option['value']

In order to have this run, you need to override part of app/code/Magento/Cms/view/adminhtml/ui_component/cms_page_form.xml

To do this create the file: app/code/<Vendor>/Cms/view/adminhtml/ui_component/cms_page_form.xml

<?xml version="1.0" encoding="UTF-8" ?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="design">
        <field name="page_layout">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object"><Vendor>\Cms\Model\PageLayout</item>
            </argument>
        </field>
    </fieldset>
</form>

We are now telling that ui_component field to use our new Model to retrieve the options.

You can also create the file app/code/<Vendor>/Cms/view/adminhtml/ui_component/cms_page_listing.xml

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <columns name="cms_page_columns">
        <column name="page_layout">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object"><Vendor>\Cms\Model\PageLayout</item>
            </argument>
        </column>
    </columns>
</listing>

Upvotes: 1

Related Questions