link-in - רשומות קוד

חפש את הקוד שלך >

בנית שדות באדמין

עדכון : יצירה באמצעות mage2gen.com!!!

בנית קובץ XML להצגת השדות

vendor/module/etc/adminhtml/system.xml

הגדשים :
tab id צריך להיות שווה ל section id

group id ו field id חשובים לשם המשתנה

קישור לדוקומנטציה

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <tab id="newblocktab" translate="label" sortOrder="10">
            <label>Module New Block</label>
        </tab>
        <section id="newblocktab" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
            <class>separator-top</class>
            <label>Add new block</label>
            <tab>newblocktab</tab>
            <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>General Configuration</label>
                <field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Module Enable</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="text_12" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Text filed</label>
                    <comment>Add your text to display</comment>
                </field>
            </group>
        </section>
    </system>
</config>

הצגת הבלוק עם תנאי ישר וללא

vendor/module/view/frontend/layout/CLASSNAME_index_index.xml

 

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd" layout="1column">
    <body>

        <referenceContainer name="content">
            <block class="Anagal\SlickSliderDebug\Block\SlickSliderDebug" ifconfig="newblocktab/general/enable" name="anagal.slicksliderdebug"  template="SlickSliderDebug.phtml" />
            <block class="Anagal\SlickSliderDebug\Block\SlickSliderDebug" name="anagal.slicksliderdebug"  template="Anagal_SlickSliderDebug::SlickSliderDebug.phtml" />
        </referenceContainer>
    </body>
</page>

ifconfig="newblocktab/general/enable"

מאפשר הכנסת של תנאי רק לטובת הצגה בצורה מקוצרת

CLASS:

<?php
namespace Anagal\SlickSliderDebug\Block;
use Magento\Framework\App\Config\ScopeConfigInterface;
class SlickSliderDebug extends \Magento\Framework\View\Element\Template{

    public function getConfigValue($value){
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $scopeConfig = $objectManager->get(ScopeConfigInterface::class);
        return $scopeConfig->getValue($value);
    }
    public function isEnabled(){
        return $this->getConfigValue('newblocktab/general/enable');
    }
    public function getDisplayText($filed){
        return $this->getConfigValue($filed);
    }
}

הדגשים חשובים :

  • use Magento\Framework\App\Config\ScopeConfigInterface;
  • objectManager

תצוגה

vendor/module/view/frontend/templates/SlickSliderDebug.phtml
<?php
/* @var $block /Anagal/SlickSliderDebug/Block/SlickSliderDebug */
?>
<h1>הצגת התנאי מ ifconfig="newblocktab/general/enable" </h1>

<?php
if($block->isEnabled()):
    echo "View the new module from admin select filed";
    if($displayText = $block->getDisplayText('newblocktab/general/text_12')):
        echo "<hr>".$displayText;
    endif;
endif;