Need pass dynamic url param value in massAction url of ui-component grid
-
13-12-2020 - |
Question
I have a massAction on a admin grid which need to pass URL parameter coming on the grid page like
http://ashish.local/admin/package_module/rsvp/index/event_id/21/key/e964d1eb362b02a9d6b3bf1281e5401966e71fb2b1fbb644b67db08f8aeef673/
I need to pass the dynamic param event_id value as like 21 but dynamic as like coming URL of the grid
As I can pass <param name="event_id">*</param>
then how to pass the url event_id to the ui-component massAction.
The * in the param is not converting to dynamic url as like below code of datasource of my grid
<dataSource name="rsvp_listing_data_source">
<argument name="dataProvider" xsi:type="configurableObject">
<argument name="class" xsi:type="string">Package\Module\Ui\Component\RsvpDataProvider</argument>
<argument name="name" xsi:type="string">rsvp_listing_data_source</argument>
<argument name="primaryFieldName" xsi:type="string">event_rsvp_id</argument>
<argument name="requestFieldName" xsi:type="string">id</argument>
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="update_url" xsi:type="url" path="mui/index/render"/>
<item name="filter_url_params" xsi:type="array">
<item name="event_id" xsi:type="string">*</item>
<item name="status" xsi:type="string">1,0</item><!-- 1 = RSVP confirm -->
</item>
</item>
</argument>
</argument>
<argument name="data" xsi:type="array">
<item name="js_config" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
</item>
</argument>
</dataSource>
Please help
Find below code for massAction:
<action name="confirm">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="type" xsi:type="string">confirm</item>
<item name="label" xsi:type="string" translate="true">Confirm All</item>
<item name="url" xsi:type="url" path="omnyfy_events/rsvp/massConfirm">
<param name="event_id">*</param>
</item>
<item name="confirm" xsi:type="array">
<item name="title" xsi:type="string" translate="true">Confirm events RSVP</item>
<item name="message" xsi:type="string" translate="true">Are you sure you want to confirm selected events RSVP?</item>
</item>
</item>
</argument
</action>
For reference please check below link: http://devdocs.magento.com/guides/v2.0/ui-components/ui-secondary-treemass.html
Here they have used static id in <param name="status">2</param>
I need to fill this param with grid url param value dynamically.
Solution
In my dataProvider
class Package\Module\Ui\Component\RsvpDataProvider
in below segment:
<argument name="class" xsi:type="string">Package\Module\Ui\Component\RsvpDataProvider</argument>
I have inherited prepareUpdateUrl
function of \Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider
class like below:
protected function prepareUpdateUrl() {
if (!isset($this->data['config']['filter_url_params'])) {
return;
}
$this->objectManager->get('Psr\Log\LoggerInterface')->critical(print_r($this->data['config']['filter_url_params'], true));
foreach ($this->data['config']['filter_url_params'] as $paramName => $paramValue) {
if ('*' == $paramValue) {
$paramValue = $this->request->getParam($paramName);
}
if ($paramValue) {
$this->data['config']['update_url'] = sprintf(
'%s%s/%s', $this->data['config']['update_url'], $paramName, $paramValue
);
$this->objectManager->get('Psr\Log\LoggerInterface')->critical('$paramName: ' . $paramName . ' $paramValue:' . $paramValue);
if ($paramName == 'status') {
$paramValue = explode(',', $paramValue);
$this->addFilter(
$this->filterBuilder->setField($paramName)->setValue($paramValue)->setConditionType('in')->create()
);
} else {
$this->objectManager->get('Psr\Log\LoggerInterface')->critical('rechead other');
$this->addFilter(
$this->filterBuilder->setField($paramName)->setValue($paramValue)->setConditionType('eq')->create()
);
}
}
}
}
I have customize my code by adding below condition
if ($paramName == 'status')
Definitely this is not the best way to resolve the issue but as I am new to Magento2, I have no other way for resolve the issue. Please let me know if anyone having better solution/suggestion for this