You are here

public function PaymentForm::viewElements in Payment 8.2

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

modules/payment_form/src/Plugin/Field/FieldFormatter/PaymentForm.php, line 104

Class

PaymentForm
Formats payment form fields.

Namespace

Drupal\payment_form\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $entity_type_id = $items
    ->getEntity()
    ->getEntityTypeId();
  $bundle = $items
    ->getEntity()
    ->bundle();
  $field_name = $this->fieldDefinition
    ->getName();

  /** @var \Drupal\payment\Entity\PaymentInterface $payment */
  $payment = $this->paymentStorage
    ->create([
    'bundle' => 'payment_form',
  ]);
  $payment
    ->setCurrencyCode($this->fieldDefinition
    ->getSetting('currency_code'));

  /** @var \Drupal\payment_form\Plugin\Payment\Type\PaymentForm $payment_type */
  $payment_type = $payment
    ->getPaymentType();
  $payment_type
    ->setDestinationUrl($this->requestStack
    ->getCurrentRequest()
    ->getUri());
  $payment_type
    ->setEntityTypeId($entity_type_id);
  $payment_type
    ->setBundle($bundle);
  $payment_type
    ->setFieldName($field_name);
  foreach ($items as $item) {

    /** @var \Drupal\payment_form\Plugin\Field\FieldType\PaymentForm $item */
    $plugin_id = $item
      ->get('plugin_id')
      ->getValue();
    if ($plugin_id) {
      $payment
        ->setLineItem($this->paymentLineItemManager
        ->createInstance($plugin_id, $item
        ->get('plugin_configuration')
        ->getValue()));
    }
  }
  return $this->entityFormBuilder
    ->getForm($payment, 'payment_form');
}