You are here

public function BynderConfigurationForm::submitForm in Bynder 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Form/BynderConfigurationForm.php \Drupal\bynder\Form\BynderConfigurationForm::submitForm()
  2. 8 src/Form/BynderConfigurationForm.php \Drupal\bynder\Form\BynderConfigurationForm::submitForm()
  3. 8.2 src/Form/BynderConfigurationForm.php \Drupal\bynder\Form\BynderConfigurationForm::submitForm()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

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

Overrides ConfigFormBase::submitForm

File

src/Form/BynderConfigurationForm.php, line 406

Class

BynderConfigurationForm
Configure bynder to enable OAuth based access.

Namespace

Drupal\bynder\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $is_initial_save = $this
    ->config('bynder.settings')
    ->get('permanent_token') === '';
  $account_domain = $this
    ->config('bynder.settings')
    ->get('account_domain');
  $is_initial_save &= $account_domain === '';
  $credentials = $form_state
    ->getValue('credentials');
  $restrictions = $form_state
    ->getValue('restrictions');
  $this
    ->config('bynder.settings')
    ->set('permanent_token', trim($credentials['permanent_token']))
    ->set('client_id', trim($credentials['client_id']))
    ->set('client_secret', trim($credentials['client_secret']))
    ->set('account_domain', $credentials['account_domain'])
    ->set('debug', $form_state
    ->getValue('debug'))
    ->set('cache_lifetime', $form_state
    ->getValue('cache_lifetime'))
    ->set('update_frequency', $form_state
    ->getValue('update_frequency'))
    ->set('timeout', $form_state
    ->getValue('timeout'))
    ->set('usage_metaproperty', $form_state
    ->getValue('usage_metaproperty'))
    ->set('restrictions.royalty_free', $restrictions['royalty_free'] ?? NULL)
    ->set('restrictions.web_license', $restrictions['web_license'] ?? NULL)
    ->set('restrictions.print_license', $restrictions['print_license'] ?? NULL)
    ->set('use_remote_images', $form_state
    ->getValue('use_remote_images'))
    ->save();
  parent::submitForm($form, $form_state);

  // If this is the first time we're configuring credentials also update
  // cached data.
  if ($is_initial_save || $account_domain !== $form_state
    ->getValue('account_domain')) {
    try {
      $this->bynder
        ->updateCachedData();
    } catch (\Exception $exception) {
    }
  }
}