0

I need to add phtml file for the following path.

\app\code\Abc\Mkt\view\adminhtml\templates\validation.phtml

How do I add this?

Validation.phtml

<?php $enable  = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('abc_auth/abc_settings/expiry_date');
$helper = $this->helper('Abc\Mkt\Helper\Data');
$expiry = $helper->checkExpiry();
if($enable == "failure" || $enable == "invalid" || $enable == "" || !$expiry ){ ?>
<style>
li.admin__page-nav-item.item._last{ display: none; }
</style>
<?php } ?>
  • Where you want to add this phtml file ? Any particular section ? – Kishan Savaliya Nov 13 '19 at 7:41
  • Updated my comment. I need to add css for config section – Shomita Nov 13 '19 at 7:43
  • @Shiwani, can u pls elaborate , its not cleared.. – jafar pinjar Nov 13 '19 at 7:47
  • just need to add this CSS for admin config fields – Shomita Nov 13 '19 at 7:50
  • 1
    yes right, I need for system configuration fields – Shomita Nov 13 '19 at 7:52
2

You need to create below files in your custom module to add your validation.phtml file in your custom module's system configuration. If you follow below steps then your validation file will not call in any other module's system configuration.

So first of all create events.xml file here

app/code/Vendor/Module/etc/adminhtml/events.xml

Content for this file is..

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="layout_load_before">
        <observer name="custom_config_layout_load_before" instance="Vendor\Module\Observer\Adminhtml\LayoutHandler" />
    </event>
</config>

Now you need to create one LayoutHandler.php Observer file here

app/code/Vendor/Module/Observer/Adminhtml/LayoutHandler.php

Content for this file is ..

<?php

namespace Vendor\Module\Observer\Adminhtml;

class LayoutHandler implements \Magento\Framework\Event\ObserverInterface
{
    public function __construct(\Magento\Framework\App\Request\Http\Proxy $request)
    {
        $this->request = $request;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $params = $this->request->getParams();

        if (! empty($params['section'])) {
            $moduleName = $this->getModuleName();
            if ($params['section'] == "custom_section_id") { //Here you can use your section ID which is available in your adminhtml/system.xml file's <section id='whatever'>
                $layout = $observer->getData('layout');
                $layout->getUpdate()->addHandle('adminhtml_system_config_edit_section_custom_handler');
            }
        }
    }

    private function getModuleName()
    {
        $class = get_class($this);
        $moduleName = strtolower(
            str_replace('\\', '_', substr($class, 0, strpos($class, '\\Observer')))
        );
        return (string) $moduleName;
    }
}
  • In above file, You just need to change your section ID in if condition like if I'm adding css for General Configuration then I'll use general, so you can use your custom module's system config's section ID here.

Now you need to create one layout xml file here

app/code/Vendor/Module/view/adminhtml/layout/adminhtml_system_config_edit_section_custom_handler.xml

Content for this file is..

<?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">
    <head>
        <css src="Vendor_Module::css/custom.css"/>
    </head>
    <body>
        <referenceContainer name="content">
            <block class="Magento\Backend\Block\Template" name="custom_validation" template="Vendor_Module::system/config/validation.phtml"/>
        </referenceContainer>
    </body>
</page>
  • Here I've included one custom.css CSS file and one template file called validation.phtml.

So you need to create above both files in this location..

CSS

app/code/Vendor/Module/view/adminhtml/web/css/custom.css

phtml

app/code/Vendor/Module/view/adminhtml/templates/system/config/validation.phtml

You can add whatever content here based on your requirement.

Hope this will help you!

| improve this answer | |
  • 1
    Yes am checking and update you – Shomita Nov 13 '19 at 9:15
  • Did you tried this ? – Kishan Savaliya Nov 13 '19 at 9:57
  • 1
    just checking. gimme 10 mins – Shomita Nov 13 '19 at 10:10
0

You can set class in the field by <frontend_class> tag. Like this below code:

<field id="country_id" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
    <label>Country</label>
    <frontend_class>countries</frontend_class>
    <source_model>Magento\Directory\Model\Config\Source\Country</source_model>
</field>

Reference: vendor/magento/module-shipping/etc/adminhtml/system.xml

| improve this answer | |

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.