setTargetBundle example

/** * {@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.     $this->applyEntityUpdates('entity_test_mulrev');
  }
/** @var \Drupal\Core\Field\BaseFieldDefinition[] $field_storage_definitions */
    $field_storage_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions('entity_test_update');

    if ($revisionable) {
      // The 'langcode' is already available for the 'entity_test_update' entity       // type because it has the 'langcode' entity key defined.       $field_storage_definitions['langcode']->setRevisionable(TRUE);

      $field_storage_definitions['revision_id'] = BaseFieldDefinition::create('integer')
        ->setName('revision_id')
        ->setTargetEntityTypeId('entity_test_update')
        ->setTargetBundle(NULL)
        ->setLabel(new TranslatableMarkup('Revision ID'))
        ->setReadOnly(TRUE)
        ->setSetting('unsigned', TRUE);

      $field_storage_definitions['revision_default'] = BaseFieldDefinition::create('boolean')
        ->setName('revision_default')
        ->setTargetEntityTypeId('entity_test_update')
        ->setTargetBundle(NULL)
        ->setLabel(new TranslatableMarkup('Default revision'))
        ->setDescription(new TranslatableMarkup('A flag indicating whether this was a default revision when it was saved.'))
        ->setStorageRequired(TRUE)
        
// @todo Mock FieldDefinitionInterface once it exposes a proper provider     // setter. See https://www.drupal.org/node/2225961.     $field_definition = $this->prophesize(BaseFieldDefinition::class);

    // We expect two calls as the field definition will be returned from both     // base and bundle entity field info hook implementations.     $field_definition->getProvider()->shouldBeCalled();
    $field_definition->setProvider($module)->shouldBeCalledTimes(2);
    $field_definition->setName(0)->shouldBeCalledTimes(2);
    $field_definition->setTargetEntityTypeId('test_entity_type')->shouldBeCalled();
    $field_definition->setTargetBundle(NULL)->shouldBeCalled();
    $field_definition->setTargetBundle('test_bundle')->shouldBeCalled();

    $this->moduleHandler->invokeAllWith(Argument::type('string'), Argument::any())
      ->will(function D$arguments) use ($field_definition$module) {
        [$hook$callback] = $arguments;
        $callback(
          function D) use ($field_definition) {
            return [$field_definition->reveal()];
          },
          $module,
        );
      });
$reflection = new \ReflectionProperty($mock, 'bundle');
    $reflection->setValue($mock, 'test');
    $this->assertEquals('test', $mock->getTargetBundle());
  }

  /** * @covers ::setTargetBundle */
  public function testSetTargetBundle() {
    $mock = $this->getMockForAbstractClass('\Drupal\Core\Entity\EntityDisplayBase', [], '', FALSE);
    $reflection = new \ReflectionProperty($mock, 'bundle');
    $mock->setTargetBundle('test');
    $this->assertEquals('test', $reflection->getValue($mock));
  }

}
$this->assertEquals($label$definition->getLabel());
  }

  /** * @covers ::setTargetBundle * @covers ::getTargetBundle * @dataProvider factoryTypeProvider */
  public function testBundle($factory_name) {
    $definition = $this->initializeFieldUsingFactory($factory_name);
    $bundle = $this->randomMachineName();
    $definition->setTargetBundle($bundle);
    $this->assertEquals($bundle$definition->getTargetBundle());
  }

  /** * @covers ::getDescription * @dataProvider factoryTypeProvider */
  public function testFieldDescription($factory_name) {
    $definition = $this->initializeFieldUsingFactory($factory_name);
    $description = $this->randomMachineName();
    $definition->setDescription($description);
    
$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])) {
        throw new \LogicException("The $field_name field definition does not exist and it is used as $key entity key.");
      }
/** * {@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) {
    $original = $this->getEntityType($entity_type->id());

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