The hook functions you mention are so that you can have your code triggered when events happen. Retrieving a list is obviously a completely different pattern - there is no event there. There is also no need to provide custom functions for that. Products and APIs are just nodes in the Drupal database and so you retrieve them like you would any other 'node'.
e.g. this would retrieve a list of products:
$query = \Drupal::entityQuery('node');
$query->condition('type', 'product');
$query->condition('status', 1);
$nids = $query->execute();
foreach (array_chunk($nids, 50) as $chunk) {
$productNodes = Node::loadMultiple($chunk);
foreach ($productNodes as $productNode) {
// do something
}
}
The chunking in the above code is intentional, since you could have 100s of products, and you wouldn't want to try and load them all at once as PHP will blow up with out of memory errors.
For convenience in APIC v10 there is a helper function if you want:
use Drupal\product\Product;
$nids = Product::listProducts();
foreach (array_chunk($nids, 50) as $chunk) {
$productNodes = Node::loadMultiple($chunk);
foreach ($productNodes as $productNode) {
// do something
}
}
That listProducts function will enforce the ACL for you - so it will only return products that your current user has permission to access for example.
There's an equivalent for APIs too:
use Drupal\apic_api\Api;
$nids = Api::listApis();
foreach (array_chunk($nids, 50) as $chunk) {
$apiNodes = Node::loadMultiple($chunk);
foreach ($apiNodes as $apiNode) {
// do something
}
}
Hope that helps,
Chris
------------------------------
Chris Dudley
------------------------------
Original Message:
Sent: Thu December 01, 2022 08:45 PM
From: Syed Imtiyaz Alam
Subject: Fetching Product or Apis lists
I am trying to fetch the Product list or Apis list to show in some custom page.
Do anyone know if IBM APIC drupal code provides any services or any way that I can just call and retrieve these lists.
I see these hooks are available but not for fetching the lists.hook_product_create | |
hook_product_update | |
hook_product_delete |
Any suggestions, I am trying to avoid directly calling the DB where these are stored.
------------------------------
Syed Imtiyaz Alam
------------------------------