테마 템플릿 내부에서 Store Config를 가져 오는 방법
-
29-09-2020 - |
문제
상점> 구성> 일반> 일반> 정보 을 내 테마의 헤더에 추가하려고합니다.모듈에서는 getValue()
에서 \Magento\Framework\App\Config\ScopeConfigInterface
를 사용하여 수행 할 수 있지만 테마 내부에서 사용하는 방법을 볼 수는 없습니다.지금까지 나는 이것을 default.xml
<referenceContainer name="header-wrapper">
<block class="Magento\Framework\View\Element\Template" name="store.phone.number" template="Magento_Theme::phone.phtml" />
</referenceContainer>
.
그러나 나는 phone.phtml
내에서 전화 번호를 얻는 방법을 모른다
해결책
Magento\Framework\View\Element\Template
클래스를 확장하는 자신만의 블록을 만드는 것이 좋습니다.
Magento\Framework\App\Config\ScopeConfigInterface
는 Magento\Framework\View\Element\AbstractBlock
에서 선언 된 Template
($_scopeConfig
클래스의 부모)의 일부인 경우 사용자 정의 블록에 다음 함수를 추가 할 수 있습니다.
public function getConfig()
{
return $this->_scopeConfig;
}
.
다음 템플릿에서 수행 할 수 있습니다 :
$block->getConfig()->getValue('value/you/need');
.
다음과 같이 레이아웃을 업데이트하는 것을 잊지 마십시오.
<referenceContainer name="header-wrapper">
<block class="Vendor\Module\Block\View\Element\Template" name="store.phone.number" template="Magento_Theme::phone.phtml" />
</referenceContainer>
. 다른 팁
Magento\Framework\App\Config\ScopeConfig
의 인스턴스를 가져와 템플릿에서 Store Config를 직접 가져올 수 있습니다.
\Magento\Framework\App\ObjectManager::getInstance()
->get('Magento\Framework\App\Config\ScopeConfigInterface')
->getValue('value/you/need');
.
기술적으로 누군가 Magento\Framework\App\Config\ScopeConfigInterface
의 인스턴스를 묻는 경우 Magento\Framework\App\Config\ScopeConfig
의 인스턴스를 제공합니다.
예를 들어 그리드 또는 목록 모드의 기본 설정을 얻을 수 있습니다.
$productListMode = \Magento\Framework\App\ObjectManager::getInstance()
->get('Magento\Framework\App\Config\ScopeConfigInterface')
->getValue('catalog/frontend/list_mode');
.
참고 : 객체 관리자를 직접 사용하는 것을 방지합니다 .우리는 템플릿을 깨끗하게 유지해야합니다.블록에 구성을 추가하십시오.@raphael 답변을 따라야합니다.
블록에서 이것을 사용해보십시오, 그것은 많은 검색 후에 나를 위해 일하고 있습니다
$ IsEnabled=Magento \ framework \ app \ objectmanager :: getInstance ()
-> get ( 'magento \ framework \ app \ config \ scopeconfiginterface') -> getValue ( '섹션 / 그룹 / 필드');