1

I've gotten to a point where I'm having trouble connecting the dots with D8. As a result, I'm not sure I'm even asking the right question.

At this moment, I'm trying to figure out how to add a field to a class that implements EntityTraitInterface.

As an example, Commerce License provides an Entity Trait for the commerce_product_variation entity type.

If the trait is enabled, a field (license_type) is added to the product variation form - this is done within PurchasableEntityLicensed.php. I want to add an additional field to that, something like:

$fields['license_duration'] = BundleFieldDefinition::create('boolean')
  ->setLabel(t('License Duration'))
  ->setCardinality(1)
  ->setRequired(FALSE)
  ->setDisplayOptions('form', [
    'type' => 'boolean_checkbox',
    'weight' => 21,
  ]);

If I do that, and add the field to the form (in LicenseCreateForm), check the box on the variation for License Duration I get the following error when trying to save the variation:

Recoverable fatal error: Argument 1 passed to Drupal\commerce_license\Plugin\Commerce\LicenseType\Base::validateConfigurationForm() must be of the type array, null given

I'm not sure if I don't understand plugins well enough, or specifically, field plugins, or maybe Commerce. I don't know what I don't know.

So, to be specific: how am I supposed to add this field so that it works?

  • This is Commerce specific, core doesn't use an entity trait. I've added this tag. – 4k4 Jul 24 '17 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.