0

In my module, I use the Listing (grid) UI component to render data from an array. The data for the grid is provided by a DataProvider.php class getData() method.
For some reason, when I use a simple testing array, the data is properly rendered. When I use the actual array that I want to use, the grid displays the message: We couldn't find any records.

I am very sure that both arrays have the same format, so both should work. Using print_r() to print the arrays, they look identical in format. gettype() returns array for both arrays.

My DataProvider.php:

public function getData()
    {
        $orderIds = $this->_registry->registry("orderids");

        foreach ((array)$orderIds as $orderId) {

            $order = $this->_order->load($orderId);

            $orderInfo[] = [ // This array doesn't work
                "entity_id" => $order->getId(),
                "customer_firstname" => $order->getCustomerFirstname(),
                "customer_lastname" => $order->getCustomerLastname(),
                "customer_email" => $order->getCustomerEmail(),
            ];
        }

        $test[] = [ // This test array works
            "entity_id" => 1,
            "customer_firstname" => "Foo",
            "customer_lastname" => "Bar",
            "customer_email" => "foo@bar.com",
        ];

        $data = [
            'totalRecords' => count($orderInfo), // $test works, $orderInfo doesn't
            'items' => array_values($orderInfo), // $test works, $orderInfo doesn't
        ];

        /** @var ModifierInterface $modifier */
        foreach ($this->modifiersPool->getModifiersInstances() as $modifier) {
            $data = $modifier->modifyData($data);
        }
        return $data;
    }

Short description of the code: Order IDs are stored in the registry, a foreach() loop loops over every order ID and loads the order objects for those IDs, order data is stored in the $orderInfo array, which should be used to provide data to the grid.

4
  • Only diff I see in your array is entity_id being string. Try changing "entity_id" => $order->getId(), to "entity_id" => (int)$order->getId(), and see if that helps. – Adarsh Khatri Mar 22 at 0:34
  • Hello, @AdarshKhatri. Unfortunately, it doesn't help. I just noticed that in the browser console I get the warning "JQMIGRATE: jQuery.parseJSON requires a valid JSON string". Do you think it has anything to do with this? – Audio-Engineer Mar 22 at 8:55
  • Could be, try adding more than 1 $test array. – Adarsh Khatri Mar 22 at 22:29
  • I found the solution... It works when using Magento\Framework\App\Request\DataPersistor instead of the Registry to fetch the order IDs. I will post a full answer later today! But thank you for your time and help. – Audio-Engineer Mar 23 at 5:58
0

I figured it out! The problem was that I was using Registry to pass the order IDs from my controller to the data provider.
Using Magento\Framework\App\Request\DataPersistor instead to pass the data is a working solution.
You can set the order ID array in the controller with $this->_dataPersistor->set("orderids", $orderIds);
And fetch them in the data provider with $orderIds = $this->_dataPersistor->get("orderids");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.