You are here

protected function PaymentReferenceBase::buildRefreshButton in Payment 8.2

Builds the refresh button.

Parameters

mixed[] $element: The root element.

\Drupal\Core\Form\FormStateInterface $form_state:

Return value

array A render array.

1 call to PaymentReferenceBase::buildRefreshButton()
PaymentReferenceBase::process in src/Element/PaymentReferenceBase.php
Implements form #process callback.

File

src/Element/PaymentReferenceBase.php, line 320

Class

PaymentReferenceBase
Provides a base for payment reference elements.

Namespace

Drupal\payment\Element

Code

protected function buildRefreshButton(array $element, FormStateInterface $form_state) {
  $plugin_selector = $this
    ->getPluginSelector($element, $form_state);
  $class = array(
    'payment_reference-refresh-button',
  );

  /** @var \Drupal\payment\Plugin\Payment\Method\PaymentMethodInterface|null $selected_payment_method */
  $selected_payment_method = $plugin_selector
    ->getSelectedPlugin();
  if (!$element['#default_value'] && $element['#available_payment_id'] && (!$selected_payment_method || !$selected_payment_method
    ->getPaymentExecutionResult()
    ->isCompleted())) {
    $class[] = 'payment-reference-hidden';
  }
  $plugin_id = $this
    ->getPluginId();
  $build = array(
    '#ajax' => array(
      'callback' => [
        get_class(),
        'instantiate#ajaxRefresh#' . $plugin_id,
      ],
      'event' => 'mousedown',
      // The AJAX behavior itself does not need a wrapper, but
      // payment_reference.js does.
      'wrapper' => $element['container']['#id'],
    ),
    '#attached' => [
      'library' => [
        'payment/payment_reference',
      ],
    ],
    '#attributes' => array(
      // system.module.css's .hidden class's is overridden by button styling,
      // so this needs a custom class.
      'class' => $class,
    ),
    '#limit_validation_errors' => [],
    '#submit' => array(
      array(
        $this->pluginDefinition['class'],
        'refresh',
      ),
    ),
    '#type' => 'submit',
    '#value' => $this
      ->t('Re-check available payments'),
  );
  return $build;
}