I need a button in stores > configuration that retrieves product data for the store view selected in the "scope" of the configuration. I created the button but I cannot retrieve the store (or the website) chosen via the "scope" dropdown.
I tried retrieving the current store id via StoreManager, which of course doesn't work because the current store is the admin store and not the system/config scoped store; I also tried using the http request but it doesn't work either:
/* \Magento\Framework\App\Request\Http $request */
$scopeId = (int) $this->request->getParam('store', 0);
---
/* request in controller */
$this->_request->getParam('store');
$this->getRequest()->getParam('store');
I also tried using "store_id" in lieu of "store" as the request parameter but none of the above attempts were successful.
So, is there a way to retrieve the scoped store id of the system/config?
This is my button:
- Vendor/Module/etc/adminhtml/system.xml
<?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>
<section ...>
<group ...>
<field id="btn" translate="label" type="button" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Button</label>
<frontend_model>Vendor\Module\Block\System\Config\Button</frontend_model>
</field>
...
</group>
</section>
</system>
</config>
- Vendor/Module/Block/System/Config/Button.php
<?php
class Button extends Field
{
protected $_template = 'Vendor_Module::system/config/button.phtml';
public function __construct(Context $context, array $data = [])
{
parent::__construct($context, $data);
}
public function render(AbstractElement $element)
{
$element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
return parent::render($element);
}
protected function _getElementHtml(AbstractElement $element)
{
return $this->_toHtml();
}
public function getAjaxUrl()
{
return $this->getUrl('route/system_config/action');
}
public function getButtonHtml()
{
$button = $this->getLayout()->createBlock('Magento\Backend\Block\Widget\Button')->setData(['id' => 'btn_id', 'label' => __('Button'),]);
return $button->toHtml();
}
}
- Vendor/Module/Controller/Adminhtml/System/Config/Action.php
...
/* \Magento\Framework\App\Request\Http $request */
$storeId = (int) $this->request->getParam('store', 0); // this returns 0
$result = $this->resultJsonFactory->create();
return $result->setData(['success' => true]);
Does anyone know how to get the scoped store id (or website) for this button?
Thanks