You are here

function commerce_invoice_entity_operation in Commerce Invoice 8.2

Implements hook_entity_operation().

File

./commerce_invoice.module, line 320
Defines the Invoice entity and associated features.

Code

function commerce_invoice_entity_operation(EntityInterface $entity) {

  // Only show the "Invoices" operation link for orders.
  if ($entity
    ->getEntityTypeId() !== 'commerce_order') {
    return;
  }

  /** @var \Drupal\commerce_order\Entity\OrderInterface $entity */

  // Do not show the "Invoices" operation for draft orders.
  if ($entity
    ->getState()
    ->getId() == 'draft') {
    return;
  }

  // Only show if the user has the "administer commerce_invoice" permission.
  if (!\Drupal::currentUser()
    ->hasPermission('administer commerce_invoice')) {
    return;
  }
  $operations['invoices'] = [
    'title' => t('Invoices'),
    'url' => $entity
      ->toUrl('invoices'),
    'weight' => 50,
  ];
  return $operations;
}