You are here

protected function SubscriptionListBuilder::getDefaultOperations in Commerce Recurring Framework 8

Gets this list's default operations.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity the operations are for.

Return value

array The array structure is identical to the return value of self::getOperations().

Overrides EntityListBuilder::getDefaultOperations

File

src/SubscriptionListBuilder.php, line 69

Class

SubscriptionListBuilder
List builder for subscriptions.

Namespace

Drupal\commerce_recurring

Code

protected function getDefaultOperations(EntityInterface $entity) {
  $operations = parent::getDefaultOperations($entity);

  // For users with the 'update own commerce_subscription' permission, allow
  // the Edit operation only if the 'customer' form mode exists for the
  // subscription type. We do this so we never show the Edit operation using
  // the default form display (where all the fields are usually editable) to
  // customers.
  if (isset($operations['edit']) && $this->currentUser
    ->hasPermission('update own commerce_subscription') && !$this->currentUser
    ->hasPermission('update any commerce_subscription')) {
    $customer_form_mode_exists = \Drupal::entityQuery('entity_form_display')
      ->condition('id', "{$entity->getEntityTypeId()}.{$entity->bundle()}.customer")
      ->condition('status', TRUE)
      ->accessCheck(FALSE)
      ->range(0, 1)
      ->count()
      ->execute();
    if ($customer_form_mode_exists && $entity
      ->hasLinkTemplate('customer-edit-form')) {
      $operations['edit']['url'] = $this
        ->ensureDestination($entity
        ->toUrl('customer-edit-form'));
    }
    else {
      unset($operations['edit']);
    }
  }
  if ($entity
    ->access('cancel') && $entity
    ->hasLinkTemplate('cancel-form')) {
    $operations['cancel'] = [
      'title' => $this
        ->t('Cancel'),
      'weight' => 10,
      'url' => $this
        ->ensureDestination($entity
        ->toUrl('cancel-form')),
    ];
  }
  return $operations;
}