Magento – Magento 2. Grid Action column link open in new window

actionmagento2ui

I've successfully created an admin grid using ui components.
I've successfully added an actions column with a link.
The link works but I want it to open in a new window or tab.

excerpt from ui_component

<actionsColumn name="actions" class="Vendor\Module\Ui\Component\Listing\Columns\Actions">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="indexField" xsi:type="string">id</item>
                <item name="sortOrder" xsi:type="number">1000</item>
            </item>
        </argument>
    </actionsColumn>

excerpt from Vendor\Module\Ui\Component\Listing\Columns\Actions

    /**
     * Prepare Data Source
     * The delete function will be added later
     *
     * @param array $dataSource
     * @return array
     */
    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as &$item) {
                if (isset($item[AlertInterface::ID])) {
                    $item[$this->getData('name')] = [
                        'view' => [
                            'href' => $this->urlBuilder->getUrl(
                                $this->dataSourceSolver->getViewActionPath(),
                                [
                                    'id' => $item[AlertInterface::ID]
                                ]
                            ),
                            'popup' => true,
                            'target' => '_blank',
                            'label' => __('Preview')
                        ]
                    ];
                }
            }
        }
        return $dataSource;
    }
}

The link works but does not open in new tab or window. There is an example in Marketing >Email Templates > Preview section but that code uses widgets and I'm using ui_components.

Any help is appreciated.

Best Answer

In the grid_index.xml add the below code Namespace\Modulename\view\adminhtml\ui_component\grid_index.xml

<actionsColumn class="Namespace\Modulename\Ui\Component\Listing\Column\GroupIcon" name="preview">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="sortOrder" xsi:type="number">29</item>
                <item name="indexField" xsi:type="string">{{increment_id}}</item>
                <item name="label" xsi:type="string" translate="true">Attachment</item>
            </item>
        </argument>
    </actionsColumn>

Namespace\Modulename\Ui\Component\Listing\Column Here GroupIcon.php file in the respective actionClass in the above defined grid_index.xml file Use the below code with dataSource function

public function prepareDataSource(array $dataSource)
{
    if (isset($dataSource['data']['items'])) {
        foreach ($dataSource['data']['items'] as & $item) {
            $url = $this->_storeManager->getStore()->getBaseUrl().'pub/media/faq/tmp/icon/'.$item['icon'];
            if (isset($item['faqgroup_id'])) {  //"faqgroup_id" primary key in the database table
                $item[$this->getData('name')] = [
                    'preview' => [
                        'href' => $this->urlBuilder->getUrl($url),
                        'target' => '_blank',
                        'label' => __('Preview')
                    ]
                ];
            }
        }
    }

    return $dataSource;
}
Related Topic