setTargetEntityTypeId example


  protected function addBundleField($type = 'string', $revisionable = FALSE, $translatable = FALSE) {
    $definitions['new_bundle_field'] = FieldStorageDefinition::create($type)
      ->setName('new_bundle_field')
      ->setLabel(t('A new bundle field'))
      ->setTargetEntityTypeId('entity_test_update')
      ->setRevisionable($revisionable)
      ->setTranslatable($translatable);
    $this->state->set('entity_test_update.additional_field_storage_definitions', $definitions);
    $this->state->set('entity_test_update.additional_bundle_field_definitions.test_bundle', $definitions);
  }

  /** * Modifies the new bundle field from 'string' to 'text'. */
  protected function modifyBundleField() {
    $this->addBundleField('text');
  }

  public function testDefinitionEvents() {
    /** @var \Drupal\entity_test\EntityTestDefinitionSubscriber $event_subscriber */
    $event_subscriber = $this->container->get('entity_test.definition.subscriber');
    $event_subscriber->enableEventTracking();
    $event_subscriber->enableLiveDefinitionUpdates();

    // Test field storage definition events.     $storage_definition = FieldStorageDefinition::create('string')
      ->setName('field_storage_test')
      ->setLabel(new TranslatableMarkup('Field storage test'))
      ->setTargetEntityTypeId('entity_test_rev');

    $this->assertFalse($event_subscriber->hasEventFired(FieldStorageDefinitionEvents::CREATE), 'Entity type create was not dispatched yet.');
    \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionCreate($storage_definition);
    $this->assertTrue($event_subscriber->hasEventFired(FieldStorageDefinitionEvents::CREATE), 'Entity type create event successfully dispatched.');
    $this->assertTrue($event_subscriber->hasDefinitionBeenUpdated(FieldStorageDefinitionEvents::CREATE), 'Last installed field storage definition was created before the event was fired.');

    // Check that the newly added field can be retrieved from the live field     // storage definitions.     $field_storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions('entity_test_rev');
    $this->assertArrayHasKey('field_storage_test', $field_storage_definitions);

    


  /** * {@inheritdoc} */
  public function installFieldableEntityType(EntityTypeInterface $entity_type, array $field_storage_definitions) {
    $this->clearCachedDefinitions();
    foreach ($field_storage_definitions as $name => $field_storage_definition) {
      if ($field_storage_definition instanceof BaseFieldDefinition) {
        $field_storage_definition
          ->setName($name)
          ->setTargetEntityTypeId($entity_type->id())
          ->setProvider($entity_type->getProvider())
          ->setTargetBundle(NULL);
      }
    }
    $this->entityTypeListener->onFieldableEntityTypeCreate($entity_type$field_storage_definitions);
  }

  /** * {@inheritdoc} */
  public function updateFieldableEntityType(EntityTypeInterface $entity_type, array $field_storage_definitions, array &$sandbox = NULL) {
    

          $base_field_definitions[$field_name] = $definition;
        }
      }
    );

    // Automatically set the field name, target entity type and bundle     // for non-configurable fields.     foreach ($base_field_definitions as $field_name => $base_field_definition) {
      if ($base_field_definition instanceof BaseFieldDefinition) {
        $base_field_definition->setName($field_name);
        $base_field_definition->setTargetEntityTypeId($entity_type_id);
        $base_field_definition->setTargetBundle(NULL);
      }
    }

    // Invoke alter hook.     $this->moduleHandler->alter('entity_base_field_info', $base_field_definitions$entity_type);

    // Ensure defined entity keys are there and have proper revisionable and     // translatable values.     foreach (array_intersect_key($keysarray_flip(['id', 'revision', 'uuid', 'bundle'])) as $key => $field_name) {
      if (!isset($base_field_definitions[$field_name])) {
        
if ($entity_type->isRevisionable()) {
          $field->setRevisionable(TRUE);
        }
        if ($entity_type->isTranslatable()) {
          $field->setTranslatable(TRUE);
        }
        break;
    }

    $field
      ->setName($field_name)
      ->setTargetEntityTypeId($entity_type_id)
      ->setProvider($entity_type->getProvider());

    // Build up a map of expected primary keys depending on the entity type     // configuration.     $id_key = $entity_type->getKey('id');
    $revision_key = $entity_type->getKey('revision');
    $langcode_key = $entity_type->getKey('langcode');

    $expected = [];
    $expected[$entity_type->getBaseTable()] = [$id_key];
    if ($entity_type->isRevisionable()) {
      
public static function createFromFieldStorageDefinition(FieldStorageDefinitionInterface $definition) {
    return static::create($definition->getType())
      ->setCardinality($definition->getCardinality())
      ->setConstraints($definition->getConstraints())
      ->setCustomStorage($definition->hasCustomStorage())
      ->setDescription($definition->getDescription())
      ->setLabel($definition->getLabel())
      ->setName($definition->getName())
      ->setProvider($definition->getProvider())
      ->setRevisionable($definition->isRevisionable())
      ->setSettings($definition->getSettings())
      ->setTargetEntityTypeId($definition->getTargetEntityTypeId())
      ->setTranslatable($definition->isTranslatable());
  }

  /** * {@inheritdoc} */
  public static function createFromItemType($item_type) {
    // The data type of a field item is in the form of "field_item:$field_type".     $parts = explode(':', $item_type, 2);
    return static::create($parts[1]);
  }

  
protected static $modules = ['filter', 'text', 'entity_test', 'field'];

  /** * Tests creation of sample values. * * @covers ::generateSampleValue * @dataProvider providerTextFieldSampleValue */
  public function testTextFieldSampleValue($max_length) {
    // Create a text field.     $field_definition = BaseFieldDefinition::create('text')
      ->setTargetEntityTypeId('foo');

    // Ensure testing of max_lengths from 1 to 3 because generateSampleValue     // creates a sentence with a maximum number of words set to 1/3 of the     // max_length of the field.     $field_definition->setSetting('max_length', $max_length);
    $sample_value = TextItemBase::generateSampleValue($field_definition);
    $this->assertEquals($max_lengthstrlen($sample_value['value']));
  }

  /** * Data provider for testTextFieldSampleValue. */
protected EntityDefinitionUpdateManagerInterface $entityDefinitionUpdateManager;

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    // Setup some fields for entity_test_extra to create.     $definitions['multivalued_base_field'] = BaseFieldDefinition::create('string')
      ->setName('multivalued_base_field')
      ->setTargetEntityTypeId('entity_test_mulrev')
      ->setTargetBundle('entity_test_mulrev')
      ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
      // Base fields are non-translatable and non-revisionable by default, but       // we explicitly set these values here for extra clarity.       ->setTranslatable(FALSE)
      ->setRevisionable(FALSE);
    $this->state->set('entity_test_mulrev.additional_base_field_definitions', $definitions);

    $this->tableMapping = $this->entityTypeManager->getStorage('entity_test_mulrev')->getTableMapping();

    // Ensure that the tables for the new field are created.

  public function testGetFieldStorageDefinitions() {
    $field_definition = $this->setUpEntityWithFieldDefinition(TRUE);
    $field_storage_definition = $this->prophesize(FieldStorageDefinitionInterface::class);
    $field_storage_definition->getName()->willReturn('field_storage');

    $base_field_definition = $this->prophesize(BaseFieldDefinition::class);
    $base_field_definition->setProvider('example_module')->shouldBeCalled();
    $base_field_definition->setName('base_field')->shouldBeCalled();
    $base_field_definition->setTargetEntityTypeId('test_entity_type')->shouldBeCalled();

    $definitions = [
      'base_field' => $base_field_definition->reveal(),
      'field_storage' => $field_storage_definition->reveal(),
    ];

    $this->moduleHandler->invokeAllWith('entity_base_field_info', Argument::any());
    $this->moduleHandler->invokeAllWith('entity_field_storage_info', Argument::any())
      ->will(function D$arguments) use ($definitions) {
        [$hook$callback] = $arguments;
        $callback(
          
$this->installEntitySchema('base_field_override');
  }

  /** * @covers ::getClass * * @dataProvider getClassTestCases */
  public function testGetClass($field_type$base_field_class$expected_override_class) {
    $base_field = BaseFieldDefinition::create($field_type)
      ->setName('Test Field')
      ->setTargetEntityTypeId('entity_test');
    if ($base_field_class) {
      $base_field->setClass($base_field_class);
    }
    $override = BaseFieldOverride::createFromBaseFieldDefinition($base_field, 'test_bundle');
    $this->assertEquals($expected_override_classltrim($override->getClass(), '\\'));
  }

  /** * Test cases for ::testGetClass. */
  public function getClassTestCases() {
    
Home | Imprint | This part of the site doesn't use cookies.