setHandlerClass example

$this->entityTypeManager = $this->container->get('entity_type.manager');
    $this->entityFieldManager = $this->container->get('entity_field.manager');

    $this->installConfig(['content_moderation']);
  }

  /** * @covers ::entityBaseFieldInfo */
  public function testEntityBaseFieldInfo() {
    $definition = $this->entityTypeManager->getDefinition('entity_test');
    $definition->setHandlerClass('moderation', ModerationHandler::class);

    $this->enableModeration('entity_test', 'entity_test');
    $base_fields = $this->entityTypeInfo->entityBaseFieldInfo($definition);

    $this->assertFalse($base_fields['moderation_state']->isReadOnly());
    $this->assertTrue($base_fields['moderation_state']->isComputed());
    $this->assertTrue($base_fields['moderation_state']->isTranslatable());
  }

  /** * Tests the correct entity types have moderation added. * * @covers ::entityTypeAlter * * @dataProvider providerTestEntityTypeAlter */

  protected function addModerationToEntityType(ContentEntityTypeInterface $type) {
    if (!$type->hasHandlerClass('moderation')) {
      $handler_class = !empty($this->moderationHandlers[$type->id()]) ? $this->moderationHandlers[$type->id()] : ModerationHandler::class;
      $type->setHandlerClass('moderation', $handler_class);
    }

    if (!$type->hasLinkTemplate('latest-version') && $type->hasLinkTemplate('canonical')) {
      $type->setLinkTemplate('latest-version', $type->getLinkTemplate('canonical') . '/latest');
    }

    $providers = $type->getRouteProviderClasses() ?: [];
    if (empty($providers['moderation'])) {
      $providers['moderation'] = EntityModerationRouteProvider::class;
      $type->setHandlerClass('route_provider', $providers);
    }

    
$this->installEntitySchema('entity_test');
  }

  /** * Tests that the target entity is not unnecessarily loaded. */
  public function testTargetEntityNoLoad() {
    // Setup a test entity type with an entity reference field to itself. We use     // a special storage class throwing exceptions when a load operation is     // triggered to be able to detect them.     $entity_type = clone $this->entityTypeManager->getDefinition('entity_test_update');
    $entity_type->setHandlerClass('storage', '\Drupal\entity_test\EntityTestNoLoadStorage');
    $this->state->set('entity_test_update.entity_type', $entity_type);
    $definitions = [
      'target_reference' => BaseFieldDefinition::create('entity_reference')
        ->setSetting('target_type', $entity_type->id())
        ->setSetting('handler', 'default'),
    ];
    $this->state->set('entity_test_update.additional_base_field_definitions', $definitions);
    $this->entityTypeManager->clearCachedDefinitions();
    $this->installEntitySchema($entity_type->id());

    // Create the target entity.
Home | Imprint | This part of the site doesn't use cookies.