Skip to content
Snippets Groups Projects
Unverified Commit 3f1c30c8 authored by Tom Ashe's avatar Tom Ashe Committed by Tom Ashe
Browse files

Issue #3379825 by TomTech: Add support for commerce license (2.x)

parent a0536e90
No related branches found
No related tags found
No related merge requests found
Showing
with 1348 additions and 0 deletions
This diff is collapsed.
access group_license overview:
title: 'Access group license overview'
description: 'Access the overview of all group licenses, regardless of type'
name: 'Group Commerce License'
description: 'Enables Group functionality for Commerce Licenses'
package: 'Group Commerce'
type: 'module'
core_version_requirement: ^9.5 || ^10
dependencies:
- 'commerce_license:commerce_license'
- 'gcommerce:gcommerce'
group_content.group_license_add_page:
route_name: 'entity.group_content.group_license_add_page'
title: 'Add existing license'
appears_on:
- 'view.group_licenses.page_1'
group_content.group_license_create_page:
route_name: 'entity.group_content.group_license_create_page'
title: 'Add new license'
appears_on:
- 'view.group_licenses.page_1'
<?php
/**
* @file
* Enables Group functionality for the Commerce Licenses.
*/
//use Drupal\commerce_license\Entity\LicenseTypeInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
use Drupal\group\Entity\GroupInterface;
use Symfony\Component\Routing\RouterInterface;
/**
* Implements hook_ENTITY_TYPE_insert().
*/
//function gcommerce_license_commerce_license_type_insert(LicenseTypeInterface $license_type) {
// \Drupal::service('group_relation_type.manager')->clearCachedDefinitions();
//}
/**
* Implements hook_entity_operation().
*/
function gcommerce_license_entity_operation(EntityInterface $entity) {
$operations = [];
if ($entity->getEntityTypeId() === 'group' && \Drupal::moduleHandler()->moduleExists('views')) {
assert($entity instanceof GroupInterface);
if ($entity->hasPermission('access group_license overview', \Drupal::currentUser())) {
$router = \Drupal::service('router.no_access_checks');
assert($router instanceof RouterInterface);
if ($router->getRouteCollection()->get('view.group_licenses.page_1') !== NULL) {
$operations['licenses'] = [
'title' => t('Licenses'),
'weight' => 20,
'url' => Url::fromRoute('view.group_licenses.page_1', ['group' => $entity->id()]),
];
}
}
}
return $operations;
}
services:
gcommerce_license.route_subscriber:
class: 'Drupal\gcommerce_license\Routing\RouteSubscriber'
tags:
- { name: 'event_subscriber' }
group.relation_handler.permission_provider.group_license:
class: 'Drupal\gcommerce_license\Plugin\Group\RelationHandler\GroupLicensePermissionProvider'
arguments: ['@group.relation_handler.permission_provider']
shared: false
<?php
namespace Drupal\gcommerce_license\Plugin\Group\Relation;
use Drupal\Core\Form\FormStateInterface;
use Drupal\group\Plugin\Group\Relation\GroupRelationBase;
/**
* Provides a group relation type for commerce licenses.
*
* @GroupRelationType(
* id = "group_license",
* label = @Translation("Group commerce license"),
* description = @Translation("Adds commerce licenses to groups both publicly and privately."),
* entity_type_id = "commerce_license",
* entity_access = TRUE,
* reference_label = @Translation("Title"),
* reference_description = @Translation("The title of the commerce license to add to the group"),
* deriver = "Drupal\gcommerce_license\Plugin\Group\Relation\GroupLicenseDeriver",
* )
*/
class GroupLicense extends GroupRelationBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
$config = parent::defaultConfiguration();
$config['entity_cardinality'] = 1;
return $config;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
// Disable the entity cardinality field as the functionality of this module
// relies on a cardinality of 1. We don't just hide it, though, to keep a UI
// that's consistent with other group relations.
$info = $this->t("This field has been disabled by the plugin to guarantee the functionality that's expected of it.");
$form['entity_cardinality']['#disabled'] = TRUE;
$form['entity_cardinality']['#description'] .= '<br /><em>' . $info . '</em>';
return $form;
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
$dependencies = parent::calculateDependencies();
$dependencies['config'][] = 'commerce_license.commerce_license_type.' . $this->getRelationType()->getEntityBundle();
return $dependencies;
}
}
<?php
namespace Drupal\gcommerce_license\Plugin\Group\Relation;
//use Drupal\commerce_license\Entity\LicenseType;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\group\Plugin\Group\Relation\GroupRelationTypeInterface;
/**
* Provides a deriver for group_license.
*/
class GroupLicenseDeriver extends DeriverBase {
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
assert($base_plugin_definition instanceof GroupRelationTypeInterface);
$this->derivatives = [];
/** @var \Drupal\commerce_license\LicenseTypeManager $licenseTypeManager */
$licenseTypeManager = \Drupal::service('plugin.manager.commerce_license_type');
$license_types = $licenseTypeManager->getDefinitions();
foreach ($license_types as $bundle => $license_type) {
$label = $license_type['label'];
$this->derivatives[$bundle] = clone $base_plugin_definition;
$this->derivatives[$bundle]->set('entity_bundle', $bundle);
$this->derivatives[$bundle]->set('label', $this->t('Group commerce license (@type)', ['@type' => $label]));
$this->derivatives[$bundle]->set('description', $this->t('Adds %type commerce licenses to groups both publicly and privately.', ['%type' => $label]));
}
return $this->derivatives;
}
}
<?php
namespace Drupal\gcommerce_license\Plugin\Group\RelationHandler;
use Drupal\group\Plugin\Group\RelationHandler\PermissionProviderInterface;
use Drupal\group\Plugin\Group\RelationHandler\PermissionProviderTrait;
/**
* Provides group permissions for the group_license relation plugin.
*/
class GroupLicensePermissionProvider implements PermissionProviderInterface {
use PermissionProviderTrait;
/**
* Constructs a new GroupMembershipPermissionProvider.
*
* @param \Drupal\group\Plugin\Group\RelationHandler\PermissionProviderInterface $parent
* The parent permission provider.
*/
public function __construct(PermissionProviderInterface $parent) {
$this->parent = $parent;
}
/**
* {@inheritdoc}
*/
public function getPermission($operation, $target, $scope = 'any') {
// Backwards compatible permission name for 'any' scope.
if ($operation === 'view unpublished' && $target === 'entity' && $scope === 'any') {
return "$operation $this->pluginId $target";
}
return $this->parent->getPermission($operation, $target, $scope);
}
}
<?php
namespace Drupal\gcommerce_license\Routing;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
/**
* Subscriber for Group Commerce License routes.
*/
class RouteSubscriber extends RouteSubscriberBase {
/**
* {@inheritdoc}
*/
protected function alterRoutes(RouteCollection $collection) {
if ($route = $collection->get('entity.group_content.create_page')) {
$copy = clone $route;
$copy->setPath('group/{group}/licenses/create');
$copy->setDefault('base_plugin_id', 'group_license');
$collection->add('entity.group_content.group_license_create_page', $copy);
}
if ($route = $collection->get('entity.group_content.add_page')) {
$copy = clone $route;
$copy->setPath('group/{group}/licenses/add');
$copy->setDefault('base_plugin_id', 'group_license');
$collection->add('entity.group_content.group_license_add_page', $copy);
}
}
}
<?php
namespace Drupal\Tests\gcommerce_license\Functional;
use Drupal\Tests\group\Functional\EntityOperationsTest as GroupEntityOperationsTest;
/**
* Tests that entity operations (do not) show up on the group overview.
*
* @see gcommerce_license_entity_operation()
*
* @group gcommerce_license
*/
class EntityOperationsTest extends GroupEntityOperationsTest {
/**
* {@inheritdoc}
*/
protected static $modules = ['gcommerce_license'];
/**
* {@inheritdoc}
*/
public function provideEntityOperationScenarios() {
$scenarios['withoutAccess'] = [
[],
['group/1/licenses' => 'Licenses'],
];
$scenarios['withAccess'] = [
['group/1/licenses' => 'Licenses'],
[],
[
'view group',
'access group_license overview',
],
];
$scenarios['withAccessAndViews'] = [
['group/1/licenses' => 'Licenses'],
[],
[
'view group',
'access group_license overview',
],
['views'],
];
return $scenarios;
}
}
<?php
namespace Drupal\Tests\gcommerce_license\Kernel;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
/**
* Tests that all config provided by this module passes validation.
*
* @group gcommerce_license
*/
class GroupLicenseConfigTest extends EntityKernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'address',
'commerce',
'commerce_license',
'commerce_price',
'commerce_store',
'entity',
'entity_reference_revisions',
'flexible_permissions',
'gcommerce',
'gcommerce_license',
'group',
'options',
'profile',
'state_machine',
'variationcache',
'views',
];
/**
* Tests that the module's config installs properly.
*/
public function testConfig(): void {
$this->installConfig(['gcommerce_license']);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment