2

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. – arnoldbird May 3 '18 at 12:35
  • Can you say which REST API listing UUID? – Ajay Reddy May 3 '18 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 – arnoldbird May 8 '18 at 14:01
  • Can you check my answer and see if that suits you. – Ajay Reddy May 8 '18 at 17:37
3

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.

See EntityInterface::uuid

2
<?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();? – Kevin May 3 '18 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. – Ajay Reddy May 3 '18 at 17:29
  • I missed the part about getting all of the bundles, so +1. – Beebee May 3 '18 at 18:38

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.