Loading commerce_file.module +21 −0 Changes for commerce_file.module: 21 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -5,11 +5,14 @@ * Extends Commerce License with the ability to sell access to files. */ use Drupal\commerce_file\ComputedLicensedFiles; use Drupal\commerce_license\Entity\LicenseInterface; use Drupal\commerce_product\Entity\ProductVariationTypeInterface; use Drupal\Core\Access\AccessResult; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Link; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountInterface; Loading Loading @@ -283,3 +286,21 @@ function commerce_file_form_commerce_license_form_alter(&$form, FormStateInterfa } } } /** * Implements hook_entity_bundle_field_info(). */ function commerce_file_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) { if ($entity_type->id() === 'commerce_license' && $bundle === 'commerce_file') { $fields['licensed_files'] = BaseFieldDefinition::create('file') ->setLabel(t('Licensed files')) ->setDescription(t('Product applications referencing the product categories that are referenced by the product')) ->setSetting('target_type', 'file') ->setComputed(TRUE) ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED) ->setDisplayConfigurable('view', TRUE) ->setClass(ComputedLicensedFiles::class); return $fields; } } config/install/core.entity_view_display.commerce_license.commerce_file.default.yml +17 −8 Changes for config/install/core.entity_view_display.commerce_license.commerce_file.default.yml: 17 added lines, 8 removed lines. Original line number Diff line number Diff line Loading @@ -4,6 +4,7 @@ dependencies: module: - commerce - commerce_license - file - state_machine id: commerce_license.commerce_file.default targetEntityType: commerce_license Loading @@ -18,14 +19,14 @@ content: custom_date_format: '' timezone: '' third_party_settings: { } weight: 3 weight: 4 region: content expiration_type: type: commerce_plugin_item_default label: inline settings: { } third_party_settings: { } weight: 6 weight: 7 region: content expires: type: commerce_license_expiration Loading @@ -35,7 +36,7 @@ content: custom_date_format: '' timezone: '' third_party_settings: { } weight: 7 weight: 8 region: content granted: type: timestamp Loading @@ -45,7 +46,15 @@ content: custom_date_format: '' timezone: '' third_party_settings: { } weight: 4 weight: 5 region: content licensed_files: type: file_default label: above settings: use_description_as_link_text: true third_party_settings: { } weight: 1 region: content originating_order: type: entity_reference_label Loading @@ -53,7 +62,7 @@ content: settings: link: true third_party_settings: { } weight: 1 weight: 2 region: content product_variation: type: entity_reference_label Loading @@ -71,7 +80,7 @@ content: custom_date_format: '' timezone: '' third_party_settings: { } weight: 5 weight: 6 region: content state: type: state_transition_form Loading @@ -80,7 +89,7 @@ content: require_confirmation: false use_modal: false third_party_settings: { } weight: 8 weight: 9 region: content uid: type: entity_reference_label Loading @@ -88,5 +97,5 @@ content: settings: link: true third_party_settings: { } weight: 2 weight: 3 region: content src/ComputedLicensedFiles.php 0 → 100644 +35 −0 Changes for src/ComputedLicensedFiles.php: 35 added lines, 0 removed lines. Original line number Diff line number Diff line <?php namespace Drupal\commerce_file; use Drupal\commerce_license\Entity\LicenseInterface; use Drupal\Core\Field\EntityReferenceFieldItemList; use Drupal\Core\TypedData\ComputedItemListTrait; /** * Provides a computed field returning the licensed files. */ final class ComputedLicensedFiles extends EntityReferenceFieldItemList { use ComputedItemListTrait; /** * {@inheritdoc} */ protected function computeValue() { $license = $this->getEntity(); assert($license instanceof LicenseInterface); if ($license->bundle() !== 'commerce_file') { return; } $purchased_entity = $license->getPurchasedEntity(); if (!$purchased_entity || $purchased_entity->get('commerce_file')->isEmpty()) { return; } foreach ($purchased_entity->get('commerce_file') as $delta => $item) { $this->list[$delta] = $item; } } } Loading
commerce_file.module +21 −0 Changes for commerce_file.module: 21 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -5,11 +5,14 @@ * Extends Commerce License with the ability to sell access to files. */ use Drupal\commerce_file\ComputedLicensedFiles; use Drupal\commerce_license\Entity\LicenseInterface; use Drupal\commerce_product\Entity\ProductVariationTypeInterface; use Drupal\Core\Access\AccessResult; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Link; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountInterface; Loading Loading @@ -283,3 +286,21 @@ function commerce_file_form_commerce_license_form_alter(&$form, FormStateInterfa } } } /** * Implements hook_entity_bundle_field_info(). */ function commerce_file_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) { if ($entity_type->id() === 'commerce_license' && $bundle === 'commerce_file') { $fields['licensed_files'] = BaseFieldDefinition::create('file') ->setLabel(t('Licensed files')) ->setDescription(t('Product applications referencing the product categories that are referenced by the product')) ->setSetting('target_type', 'file') ->setComputed(TRUE) ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED) ->setDisplayConfigurable('view', TRUE) ->setClass(ComputedLicensedFiles::class); return $fields; } }
config/install/core.entity_view_display.commerce_license.commerce_file.default.yml +17 −8 Changes for config/install/core.entity_view_display.commerce_license.commerce_file.default.yml: 17 added lines, 8 removed lines. Original line number Diff line number Diff line Loading @@ -4,6 +4,7 @@ dependencies: module: - commerce - commerce_license - file - state_machine id: commerce_license.commerce_file.default targetEntityType: commerce_license Loading @@ -18,14 +19,14 @@ content: custom_date_format: '' timezone: '' third_party_settings: { } weight: 3 weight: 4 region: content expiration_type: type: commerce_plugin_item_default label: inline settings: { } third_party_settings: { } weight: 6 weight: 7 region: content expires: type: commerce_license_expiration Loading @@ -35,7 +36,7 @@ content: custom_date_format: '' timezone: '' third_party_settings: { } weight: 7 weight: 8 region: content granted: type: timestamp Loading @@ -45,7 +46,15 @@ content: custom_date_format: '' timezone: '' third_party_settings: { } weight: 4 weight: 5 region: content licensed_files: type: file_default label: above settings: use_description_as_link_text: true third_party_settings: { } weight: 1 region: content originating_order: type: entity_reference_label Loading @@ -53,7 +62,7 @@ content: settings: link: true third_party_settings: { } weight: 1 weight: 2 region: content product_variation: type: entity_reference_label Loading @@ -71,7 +80,7 @@ content: custom_date_format: '' timezone: '' third_party_settings: { } weight: 5 weight: 6 region: content state: type: state_transition_form Loading @@ -80,7 +89,7 @@ content: require_confirmation: false use_modal: false third_party_settings: { } weight: 8 weight: 9 region: content uid: type: entity_reference_label Loading @@ -88,5 +97,5 @@ content: settings: link: true third_party_settings: { } weight: 2 weight: 3 region: content
src/ComputedLicensedFiles.php 0 → 100644 +35 −0 Changes for src/ComputedLicensedFiles.php: 35 added lines, 0 removed lines. Original line number Diff line number Diff line <?php namespace Drupal\commerce_file; use Drupal\commerce_license\Entity\LicenseInterface; use Drupal\Core\Field\EntityReferenceFieldItemList; use Drupal\Core\TypedData\ComputedItemListTrait; /** * Provides a computed field returning the licensed files. */ final class ComputedLicensedFiles extends EntityReferenceFieldItemList { use ComputedItemListTrait; /** * {@inheritdoc} */ protected function computeValue() { $license = $this->getEntity(); assert($license instanceof LicenseInterface); if ($license->bundle() !== 'commerce_file') { return; } $purchased_entity = $license->getPurchasedEntity(); if (!$purchased_entity || $purchased_entity->get('commerce_file')->isEmpty()) { return; } foreach ($purchased_entity->get('commerce_file') as $delta => $item) { $this->list[$delta] = $item; } } }