You are here

public function EntityTraitManager::installTrait in Commerce Core 8.2

Installs the given trait for the given entity type and bundle.

Installing a trait attaches any fields that the trait provides to the given bundle.

Parameters

\Drupal\commerce\Plugin\Commerce\EntityTrait\EntityTraitInterface $trait: The trait.

string $entity_type_id: The entity type id.

string $bundle: The bundle.

Overrides EntityTraitManagerInterface::installTrait

File

src/EntityTraitManager.php, line 95

Class

EntityTraitManager
Manages discovery and instantiation of entity trait plugins.

Namespace

Drupal\commerce

Code

public function installTrait(EntityTraitInterface $trait, $entity_type_id, $bundle) {

  // The fields provided by an entity trait are maintained as locked
  // configurable fields, for simplicity.
  foreach ($trait
    ->buildFieldDefinitions() as $field_name => $field_definition) {
    $field_definition
      ->setTargetEntityTypeId($entity_type_id);
    $field_definition
      ->setTargetBundle($bundle);
    $field_definition
      ->setName($field_name);
    $this->configurableFieldManager
      ->createField($field_definition);
  }
}