You are here

public function BynderUpload::getForm in Bynder 8.3

Same name and namespace in other branches
  1. 8 src/Plugin/EntityBrowser/Widget/BynderUpload.php \Drupal\bynder\Plugin\EntityBrowser\Widget\BynderUpload::getForm()
  2. 8.2 src/Plugin/EntityBrowser/Widget/BynderUpload.php \Drupal\bynder\Plugin\EntityBrowser\Widget\BynderUpload::getForm()
  3. 4.0.x src/Plugin/EntityBrowser/Widget/BynderUpload.php \Drupal\bynder\Plugin\EntityBrowser\Widget\BynderUpload::getForm()

Overrides BynderWidgetBase::getForm

File

src/Plugin/EntityBrowser/Widget/BynderUpload.php, line 126

Class

BynderUpload
Uses upload to create media entities.

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 ($form_state
    ->getValue('errors')) {
    $form['actions']['submit']['#access'] = FALSE;
    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;
  }
  $has_upload_permissions = $this->bynderApi
    ->hasUploadPermissions();
  if (empty($this->configuration['brand'])) {
    (new BrandNotSetException($this->configuration['entity_browser_id']))
      ->logException()
      ->displayMessage();
    return [];
  }
  elseif (!$has_upload_permissions) {
    $form['actions']['submit']['#access'] = FALSE;
    (new UploadPermissionException($this->configuration['entity_browser_id']))
      ->logException();
    $form['message'] = [
      '#markup' => $this
        ->t("Unable to upload files to Bynder. Make sure your user account has enough permissions."),
    ];
  }
  else {
    if ($form_state
      ->getValue('errors')) {
      $form['actions']['submit']['#access'] = FALSE;
      return $form;
    }
    $form['upload'] = [
      '#title' => $this
        ->t('File upload'),
      '#type' => 'dropzonejs',
      '#dropzone_description' => $this
        ->getConfiguration()['settings']['dropzone_description'],
    ];
    $form['description'] = [
      '#title' => $this
        ->t('Description'),
      '#type' => 'textarea',
      '#description' => $this
        ->t('Description text to be added to the assets.'),
    ];
    $this->session
      ->set('upload_permissions', $has_upload_permissions);
    if ($uploaded_assets = $this->session
      ->get('bynder_upload_batch_result', [])) {
      $form_state
        ->set('uploaded_entities', $uploaded_assets);
      $this->session
        ->remove('bynder_upload_batch_result');
      $form['upload']['#access'] = FALSE;
      $form['description']['#access'] = FALSE;
      $form['#attached']['library'][] = 'bynder/upload';
      $form['actions']['submit']['#attributes']['class'][] = 'visually-hidden';
      $form['message']['#markup'] = $this
        ->t('Finishing upload. Please wait...');
    }
    else {
      $form['actions']['submit']['#eb_widget_main_submit'] = FALSE;
      $form['actions']['submit']['#bynder_upload_submit'] = TRUE;
    }
  }
  return $form;
}