You are here

public function Invoice::toUrl in Commerce Invoice 8.2

Gets the URL object for the entity.

The entity must have an id already. Content entities usually get their IDs by saving them.

URI templates might be set in the links array in an annotation, for example:


links = {
  "canonical" = "/node/{node}",
  "edit-form" = "/node/{node}/edit",
  "version-history" = "/node/{node}/revisions"
}

or specified in a callback function set like:


uri_callback = "comment_uri",

If the path is not set in the links array, the uri_callback function is used for setting the path. If this does not exist and the link relationship type is canonical, the path is set using the default template: entity/entityType/id.

Parameters

string $rel: The link relationship type, for example: canonical or edit-form.

array $options: See \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() for the available options.

Return value

\Drupal\Core\Url The URL object.

Throws

\Drupal\Core\Entity\EntityMalformedException

\Drupal\Core\Entity\Exception\UndefinedLinkTemplateException

Overrides EntityBase::toUrl

File

src/Entity/Invoice.php, line 85

Class

Invoice
Defines the invoice entity class.

Namespace

Drupal\commerce_invoice\Entity

Code

public function toUrl($rel = 'canonical', array $options = []) {

  // For better UX, the collection URL of invoices that are created for a
  // single order should be the list of invoices for that specific order.
  $invoice_collection_url = NULL;
  if ($rel === 'collection' && count($this
    ->get('orders')) === 1) {
    $order = $this
      ->get('orders')
      ->first()->entity;
    if ($this
      ->bundle() === 'default') {
      $invoice_collection_url = $order
        ->toUrl('invoices', $options);
    }
    elseif ($this
      ->bundle() === 'credit_memo') {
      $invoice_collection_url = $order
        ->toUrl('credit-memos', $options);
    }
  }
  return $invoice_collection_url ?? parent::toUrl($rel, $options);
}