You are here

public function DownloadFile::buildPaneForm in Commerce File 8.2

Builds the pane form.

Parameters

array $pane_form: The pane form, containing the following basic properties:

  • #parents: Identifies the position of the pane form in the overall parent form, and identifies the location where the field values are placed within $form_state->getValues().

\Drupal\Core\Form\FormStateInterface $form_state: The form state of the parent form.

array $complete_form: The complete form structure.

Overrides CheckoutPaneInterface::buildPaneForm

File

src/Plugin/Commerce/CheckoutPane/DownloadFile.php, line 23

Class

DownloadFile
Provides the file download checkout pane.

Namespace

Drupal\commerce_file\Plugin\Commerce\CheckoutPane

Code

public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
  $pane_form = [];

  // Create an array that will hold all the active file licenses found.
  $license_ids = [];

  // Get all licenced products with file download.
  foreach ($this->order
    ->getItems() as $order_item) {
    if (!$order_item
      ->hasField('license') || $order_item
      ->get('license')
      ->isEmpty()) {
      continue;
    }

    /** @var \Drupal\commerce_license\Entity\LicenseInterface $license */
    $license = $order_item
      ->get('license')->entity;

    // Only show download links for activated file licenses.
    if ($license
      ->bundle() !== 'commerce_file' || $license
      ->getState()
      ->getId() !== 'active') {
      continue;
    }
    $purchased_entity = $order_item
      ->getPurchasedEntity();
    if (!$purchased_entity
      ->hasField('commerce_file') || $purchased_entity
      ->get('commerce_file')
      ->isEmpty()) {
      continue;
    }
    $license_ids[] = $license
      ->id();
  }
  if ($license_ids) {
    $pane_form['files'] = [
      '#type' => 'view',
      '#name' => 'commerce_file_my_files',
      '#display_id' => 'checkout_complete',
      '#arguments' => [
        implode('+', $license_ids),
      ],
      '#embed' => TRUE,
    ];
  }
  return $pane_form;
}