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

Issue #3379824 by TomTech: Add support for commerce invoice (2.x)

parent 885214ff
No related branches found
No related tags found
No related merge requests found
Showing
with 1373 additions and 0 deletions
This diff is collapsed.
access group_invoice overview:
title: 'Access group invoice overview'
description: 'Access the overview of all group invoices, regardless of type'
name: 'Group Commerce Invoice'
description: 'Enables Group functionality for Commerce Invoices'
package: 'Group Commerce'
type: 'module'
core_version_requirement: ^9.5 || ^10
dependencies:
- 'commerce_invoice:commerce_invoice'
- 'gcommerce:gcommerce'
group_content.group_invoice_add_page:
route_name: 'entity.group_content.group_invoice_add_page'
title: 'Add existing invoice'
appears_on:
- 'view.group_invoices.page_1'
group_content.group_invoice_create_page:
route_name: 'entity.group_content.group_invoice_create_page'
title: 'Add new invoice'
appears_on:
- 'view.group_invoices.page_1'
<?php
/**
* @file
* Enables Group functionality for the Commerce Invoices.
*/
use Drupal\commerce_invoice\Entity\InvoiceTypeInterface;
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_invoice_commerce_invoice_type_insert(InvoiceTypeInterface $invoice_type) {
\Drupal::service('group_relation_type.manager')->clearCachedDefinitions();
}
/**
* Implements hook_entity_operation().
*/
function gcommerce_invoice_entity_operation(EntityInterface $entity) {
$operations = [];
if ($entity->getEntityTypeId() === 'group' && \Drupal::moduleHandler()->moduleExists('views')) {
assert($entity instanceof GroupInterface);
if ($entity->hasPermission('access group_invoice overview', \Drupal::currentUser())) {
$router = \Drupal::service('router.no_access_checks');
assert($router instanceof RouterInterface);
if ($router->getRouteCollection()->get('view.group_invoices.page_1') !== NULL) {
$operations['invoices'] = [
'title' => t('Invoices'),
'weight' => 20,
'url' => Url::fromRoute('view.group_invoices.page_1', ['group' => $entity->id()]),
];
}
}
}
return $operations;
}
services:
gcommerce_invoice.route_subscriber:
class: 'Drupal\gcommerce_invoice\Routing\RouteSubscriber'
tags:
- { name: 'event_subscriber' }
group.relation_handler.permission_provider.group_invoice:
class: 'Drupal\gcommerce_invoice\Plugin\Group\RelationHandler\GroupInvoicePermissionProvider'
arguments: ['@group.relation_handler.permission_provider']
shared: false
<?php
namespace Drupal\gcommerce_invoice\Plugin\Group\Relation;
use Drupal\Core\Form\FormStateInterface;
use Drupal\group\Plugin\Group\Relation\GroupRelationBase;
/**
* Provides a group relation type for commerce invoices.
*
* @GroupRelationType(
* id = "group_invoice",
* label = @Translation("Group commerce invoice"),
* description = @Translation("Adds commerce invoices to groups both publicly and privately."),
* entity_type_id = "commerce_invoice",
* entity_access = TRUE,
* reference_label = @Translation("Title"),
* reference_description = @Translation("The title of the commerce invoice to add to the group"),
* deriver = "Drupal\gcommerce_invoice\Plugin\Group\Relation\GroupInvoiceDeriver",
* )
*/
class GroupInvoice 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_invoice.commerce_invoice_type.' . $this->getRelationType()->getEntityBundle();
return $dependencies;
}
}
<?php
namespace Drupal\gcommerce_invoice\Plugin\Group\Relation;
use Drupal\commerce_invoice\Entity\InvoiceType;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\group\Plugin\Group\Relation\GroupRelationTypeInterface;
/**
* Provides a deriver for group_invoice.
*/
class GroupInvoiceDeriver extends DeriverBase {
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
assert($base_plugin_definition instanceof GroupRelationTypeInterface);
$this->derivatives = [];
foreach (InvoiceType::loadMultiple() as $name => $entity_type) {
$label = $entity_type->label();
$this->derivatives[$name] = clone $base_plugin_definition;
$this->derivatives[$name]->set('entity_bundle', $name);
$this->derivatives[$name]->set('label', $this->t('Group commerce invoice (@type)', ['@type' => $label]));
$this->derivatives[$name]->set('description', $this->t('Adds %type commerce invoices to groups both publicly and privately.', ['%type' => $label]));
}
return $this->derivatives;
}
}
<?php
namespace Drupal\gcommerce_invoice\Plugin\Group\RelationHandler;
use Drupal\group\Plugin\Group\RelationHandler\PermissionProviderInterface;
use Drupal\group\Plugin\Group\RelationHandler\PermissionProviderTrait;
/**
* Provides group permissions for the group_invoice relation plugin.
*/
class GroupInvoicePermissionProvider 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_invoice\Routing;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
/**
* Subscriber for Group Commerce Invoice 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}/invoices/create');
$copy->setDefault('base_plugin_id', 'group_invoice');
$collection->add('entity.group_content.group_invoice_create_page', $copy);
}
if ($route = $collection->get('entity.group_content.add_page')) {
$copy = clone $route;
$copy->setPath('group/{group}/invoices/add');
$copy->setDefault('base_plugin_id', 'group_invoice');
$collection->add('entity.group_content.group_invoice_add_page', $copy);
}
}
}
<?php
namespace Drupal\Tests\gcommerce_invoice\Functional;
use Drupal\Tests\group\Functional\EntityOperationsTest as GroupEntityOperationsTest;
/**
* Tests that entity operations (do not) show up on the group overview.
*
* @see gcommerce_invoice_entity_operation()
*
* @group gcommerce_invoice
*/
class EntityOperationsTest extends GroupEntityOperationsTest {
/**
* {@inheritdoc}
*/
protected static $modules = ['gcommerce_invoice'];
/**
* {@inheritdoc}
*/
public function provideEntityOperationScenarios() {
$scenarios['withoutAccess'] = [
[],
['group/1/invoices' => 'Invoices'],
];
$scenarios['withAccess'] = [
['group/1/invoices' => 'Invoices'],
[],
[
'view group',
'access group_invoice overview',
],
];
$scenarios['withAccessAndViews'] = [
['group/1/invoices' => 'Invoices'],
[],
[
'view group',
'access group_invoice overview',
],
['views'],
];
return $scenarios;
}
}
<?php
namespace Drupal\Tests\gcommerce_invoice\Kernel;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
/**
* Tests that all config provided by this module passes validation.
*
* @group gcommerce_invoice
*/
class GroupInvoiceConfigTest extends EntityKernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'address',
'commerce',
'commerce_invoice',
'commerce_price',
'commerce_store',
'entity',
'entity_reference_revisions',
'flexible_permissions',
'gcommerce',
'gcommerce_invoice',
'group',
'options',
'profile',
'state_machine',
'variationcache',
'views',
];
/**
* Tests that the module's config installs properly.
*/
public function testConfig(): void {
$this->installConfig(['gcommerce_invoice']);
}
}
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