getEntityTypeFromDefaults example

public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, AdminContext $admin_context$entity_repository = NULL) {
    parent::__construct($entity_type_manager$entity_repository);

    $this->configFactory = $config_factory;
    $this->adminContext = $admin_context;
  }

  /** * {@inheritdoc} */
  public function convert($value$definition$name, array $defaults) {
    $entity_type_id = $this->getEntityTypeFromDefaults($definition$name$defaults);
    if (!$this->entityTypeManager->hasDefinition($entity_type_id)) {
      return NULL;
    }
    // If the entity type is dynamic, confirm it to be a config entity. Static     // entity types will have performed this check in self::applies().     if (str_starts_with($definition['type'], 'entity:{')) {
      $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
      if (!$entity_type->entityClassImplements(ConfigEntityInterface::class)) {
        return parent::convert($value$definition$name$defaults);
      }
    }

    

  public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository) {
    $this->entityTypeManager = $entity_type_manager;
    $this->entityRepository = $entity_repository;
  }

  /** * {@inheritdoc} */
  public function convert($value$definition$name, array $defaults) {
    $entity_type_id = $this->getEntityTypeFromDefaults($definition$name$defaults);

    // If the entity type is revisionable and the parameter has the     // "load_latest_revision" flag, load the active variant.     if (!empty($definition['load_latest_revision'])) {
      $entity = $this->entityRepository->getActive($entity_type_id$value);

      if (
        !empty($definition['bundle']) &&
        $entity instanceof EntityInterface &&
        !in_array($entity->bundle()$definition['bundle'], TRUE)
      ) {
        

  public function setLanguageManager(LanguageManagerInterface $language_manager) {
    $this->languageManager = $language_manager;
  }

  /** * {@inheritdoc} */
  public function convert($value$definition$name, array $defaults) {
    $entity_type_id = $this->getEntityTypeFromDefaults($definition$name$defaults);
    $uuid_key = $this->entityTypeManager->getDefinition($entity_type_id)
      ->getKey('uuid');
    if ($storage = $this->entityTypeManager->getStorage($entity_type_id)) {
      if (!$entities = $storage->loadByProperties([$uuid_key => $value])) {
        return NULL;
      }
      $entity = reset($entities);
      // If the entity type is translatable, ensure we return the proper       // translation object for the current context.       if ($entity instanceof TranslatableInterface && $entity->isTranslatable()) {
        // @see https://www.drupal.org/project/drupal/issues/2624770

  public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository) {
    $this->entityTypeManager = $entity_type_manager;
    $this->entityRepository = $entity_repository;
  }

  /** * {@inheritdoc} */
  public function convert($value$definition$name, array $defaults) {
    $entity_type_id = $this->getEntityTypeFromDefaults($definition$name$defaults);
    $entity = $this->entityTypeManager->getStorage($entity_type_id)->loadRevision($value);

    // If the entity type is translatable, ensure we return the proper     // translation object for the current context.     if ($entity instanceof EntityInterface && $entity instanceof TranslatableInterface) {
      $entity = $this->entityRepository->getTranslationFromContext($entity, NULL, ['operation' => 'entity_upcast']);
    }

    return $entity;
  }

  
Home | Imprint | This part of the site doesn't use cookies.