The getBundleInfo method returns only the machine name and label for bundles. How do I get the UUID of each bundle, or of all bundles?
-
Looks like I'm able to get a list of my bundles and their UUIDs via a GET request to a jsonapi endpoint. I still wonder how it might be done in PHP.– arnoldbirdMay 3, 2018 at 12:35
-
Can you say which REST API listing UUID?– Ajay ReddyMay 3, 2018 at 13:10
-
@Ajay, the endpoint I used was... example.com/myentity_type/myentity_type -- if you install the jsonapi_extras module you will be able to find the relevant endpoint listed at admin/config/services/jsonapi– arnoldbirdMay 8, 2018 at 14:01
-
Can you check my answer and see if that suits you.– Ajay ReddyMay 8, 2018 at 17:37
4 Answers
<?php
namespace Drupal\uuid\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\ContentEntityType;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Class UUIDController.
*
* @package Drupal\uuid\Controller
*/
class UUIDController extends ControllerBase {
public function uuidGet() {
$uuid = [];
$entity_type_definations = \Drupal::entityTypeManager()->getDefinitions();
/* @var $definition \Drupal\Core\Entity\EntityTypeInterface */
foreach ($entity_type_definations as $definition) {
if ($definition instanceof ContentEntityType) {
$content_types = \Drupal::entityManager()->getBundleInfo($definition->id());
$entity_type = $definition->getBundleEntityType();
if ($entity_type && $content_types) {
foreach ($content_types as $type_id => $type) {
$uuid[$entity_type][$type_id] = \Drupal::entityTypeManager()->getStorage($entity_type)->load($type_id)->uuid();
}
}
}
}
return new JsonResponse($uuid);
}
}
Above code will give UUID of all bundles and you can call this controller anywhere in your code like below, this will give all UUID's for bundles presented.
Drupal\entity_type_clone\Controller\UUIDController::uuidGet()
To get a single UUID for specific bundle try below code:
Drupal::entityTypeManager()->getStorage("ENTITY_TYPE")->load("BUNDLE_MACHINE_NAME")->uuid();
-
A controller should return a Response. Why all of this instead of
Drupal::entityTypeManager()->getStorage("node_type")->load("BUNDLE_MACHINE_NAME")->uuid();
?– KevinMay 3, 2018 at 17:15 -
1@Kevin Updated answer for controller response thanks for suggestion. As arnoldbird clearly mentioned he needs UUID of each bundle or all bundle irrespective of "Entity Type". That's I suggested a controller and listed there all UUID of each entity type with bundle. May 3, 2018 at 17:29
-
Replace BUNDLE_MACHINE_NAME
with your bundle machine name.
Drupal::entityTypeManager()->getStorage("node_type")->load("BUNDLE_MACHINE_NAME")->uuid();
Or you can also load it like this:
Drupal\node\Entity\NodeType::load("BUNDLE_MACHINE_NAME")->uuid();
Entities based on EntityInterface
will have the uuid()
method.
The easiest method will be by adding UUID by editing views of entity listing.
Eg: if you want to find the UUID of content,
- Edit the view admin/content
- Add Field UUID
- Save View
Navigate to admin/content to see uuid for corresponding content entity
If you have devel module installed, you can see the UUID by inspecting the development output of such content from the admin UI or debugging a variable.