You are here

public function BynderWidgetBase::getForm in Bynder 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/EntityBrowser/Widget/BynderWidgetBase.php \Drupal\bynder\Plugin\EntityBrowser\Widget\BynderWidgetBase::getForm()
  2. 8 src/Plugin/EntityBrowser/Widget/BynderWidgetBase.php \Drupal\bynder\Plugin\EntityBrowser\Widget\BynderWidgetBase::getForm()
  3. 4.0.x src/Plugin/EntityBrowser/Widget/BynderWidgetBase.php \Drupal\bynder\Plugin\EntityBrowser\Widget\BynderWidgetBase::getForm()
2 calls to BynderWidgetBase::getForm()
BynderSearch::getForm in src/Plugin/EntityBrowser/Widget/BynderSearch.php
BynderUpload::getForm in src/Plugin/EntityBrowser/Widget/BynderUpload.php
2 methods override BynderWidgetBase::getForm()
BynderSearch::getForm in src/Plugin/EntityBrowser/Widget/BynderSearch.php
BynderUpload::getForm in src/Plugin/EntityBrowser/Widget/BynderUpload.php

File

src/Plugin/EntityBrowser/Widget/BynderWidgetBase.php, line 145

Class

BynderWidgetBase
Base class for Bynder Entity browser widgets.

Namespace

Drupal\bynder\Plugin\EntityBrowser\Widget

Code

public function getForm(array &$original_form, FormStateInterface $form_state, array $additional_widget_parameters) {
  $form = parent::getForm($original_form, $form_state, $additional_widget_parameters);
  if (!$this
    ->checkType()) {
    $form_state
      ->setValue('errors', TRUE);
    return $form;
  }

  // Check if the API configuration is in place and exit early if not.
  foreach ([
    'consumer_key',
    'consumer_secret',
    'token',
    'token_secret',
    'account_domain',
  ] as $key) {
    if ($this->config
      ->get('bynder.settings')
      ->get($key) === '') {
      $form_state
        ->setValue('errors', TRUE);
      (new UnableToConnectException())
        ->logException()
        ->displayMessage();
      return $form;
    }
  }

  // Require oAuth authorization if we don't have a valid access token yet.
  // If we are submitting "Reload after submit" button right now we also need
  // to add it to the form for submission to work as expected. When the form
  // will be rebuild after the submit on same request we won't add it anymore.
  if (!$this->bynderApi
    ->hasAccessToken() || $this->requestStack
    ->getCurrentRequest()
    ->getMethod() == 'POST' && $this->requestStack
    ->getCurrentRequest()->request
    ->get('op') == 'Reload after submit' && $form_state
    ->isProcessingInput() === NULL) {
    $form_state
      ->setValue('errors', TRUE);
    $form['message'] = [
      '#markup' => $this
        ->t('You need to <a href="#login" class="oauth-link">log into Bynder</a> before importing assets.'),
      '#prefix' => '<p>',
      '#suffix' => '</p>',
    ];
    $form['reload'] = [
      '#type' => 'button',
      '#value' => 'Reload after submit',
      '#attached' => [
        'library' => [
          'bynder/oauth',
        ],
      ],
      '#attributes' => [
        'class' => [
          'oauth-reload',
          'visually-hidden',
        ],
      ],
    ];
    return $form;
  }
  return $form;
}