3

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?

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

4 Answers 4

4
<?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();

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

0

The easiest method will be by adding UUID by editing views of entity listing.

Eg: if you want to find the UUID of content,

  1. Edit the view admin/content
  2. Add Field UUID
  3. Save View

Navigate to admin/content to see uuid for corresponding content entity

0

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.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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