You are here

public function BynderConfigurationForm::buildForm in Bynder 8

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

Form constructor.

Parameters

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

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

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/BynderConfigurationForm.php, line 95

Class

BynderConfigurationForm
Configure bynder to enable OAuth based access.

Namespace

Drupal\bynder\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('bynder.settings');
  $form['consumer_key'] = [
    '#required' => TRUE,
    '#type' => 'textfield',
    '#title' => $this
      ->t('Consumer key'),
    '#parents' => [
      'credentials',
      'consumer_key',
    ],
    '#default_value' => $config
      ->get('consumer_key'),
    '#description' => $this
      ->t('Provide the consumer key. For more information check <a href="@url">Bynder knowlage base</a>.', [
      '@url' => 'https://support.getbynder.com/hc/en-us/articles/208734785-Create-API-tokens-for-your-app',
    ]),
  ];
  $form['consumer_secret'] = [
    '#required' => TRUE,
    '#type' => 'textfield',
    '#title' => $this
      ->t('Consumer secret'),
    '#parents' => [
      'credentials',
      'consumer_secret',
    ],
    '#default_value' => $config
      ->get('consumer_secret'),
    '#description' => $this
      ->t('Provide the consumer secret. For more information check <a href="@url">Bynder knowlage base</a>.', [
      '@url' => 'https://support.getbynder.com/hc/en-us/articles/208734785-Create-API-tokens-for-your-app',
    ]),
  ];
  $form['token'] = [
    '#required' => TRUE,
    '#type' => 'textfield',
    '#title' => $this
      ->t('Token'),
    '#parents' => [
      'credentials',
      'token',
    ],
    '#default_value' => $config
      ->get('token'),
    '#description' => $this
      ->t('Provide the token. For more information check <a href="@url">Bynder knowlage base</a>.', [
      '@url' => 'https://support.getbynder.com/hc/en-us/articles/208734785-Create-API-tokens-for-your-app',
    ]),
  ];
  $form['token_secret'] = [
    '#required' => TRUE,
    '#type' => 'textfield',
    '#title' => $this
      ->t('Token secret'),
    '#default_value' => $config
      ->get('token_secret'),
    '#parents' => [
      'credentials',
      'token_secret',
    ],
    '#description' => $this
      ->t('Provide the token secret. For more information check <a href="@url">Bynder knowlage base</a>.', [
      '@url' => 'https://support.getbynder.com/hc/en-us/articles/208734785-Create-API-tokens-for-your-app',
    ]),
  ];
  $form['account_domain'] = [
    '#required' => TRUE,
    '#type' => 'textfield',
    '#title' => $this
      ->t('Account domain'),
    '#default_value' => $config
      ->get('account_domain'),
    '#parents' => [
      'credentials',
      'account_domain',
    ],
    '#description' => $this
      ->t('Provide your Bynder account domain. It should be in the format "https://bynder-domain.extension". Change "bynder-domain.extension" with the domain provided by Bynder. For more information check <a href="@url">Bynder docs</a>.', [
      '@url' => 'http://docs.bynder.apiary.io/#reference/',
    ]),
  ];
  $form['debug'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Debug'),
    '#description' => $this
      ->t('Check this setting if you want to have more verbose log messages.'),
    '#default_value' => $config
      ->get('debug'),
  ];
  $form['test'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('API connection test'),
    'wrapper' => [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#attributes' => [
        'id' => 'connection-test',
      ],
      '#attached' => [
        'library' => [
          'bynder/config_form',
        ],
      ],
    ],
    'check' => [
      '#type' => 'button',
      '#limit_validation_errors' => [],
      '#value' => $this
        ->t('Test connection'),
      '#ajax' => [
        'callback' => '::testConnectionAjaxCallback',
      ],
    ],
    'test_connection' => [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Test connection before saving'),
      '#description' => $this
        ->t("Uncheck to allow saving credentials even if connection to Bynder can't be established."),
      '#default_value' => TRUE,
    ],
  ];
  if ($derivatives = $this
    ->getDerivatives()) {
    $form['derivatives'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Bynder image derivatives'),
      '#description' => $this
        ->t('Bynder provides "mini", "webimage" and "thul" image sizes by default. Custom derivatives can be configured to better suit specific use-cases. Reload of derivatives can be triggered if any derivatives are missing from the list.'),
      'derivatives_list' => [
        '#theme' => 'item_list',
        '#items' => array_merge([
          'mini',
          'webimage',
          'thul',
        ], array_map(function ($item) {
          return $item['prefix'];
        }, $derivatives)),
      ],
      'check' => [
        '#type' => 'submit',
        '#value' => $this
          ->t('Update cached information'),
        '#submit' => [
          [
            static::class,
            'submitReloadDerivatives',
          ],
        ],
      ],
    ];
  }
  if ($meta_properties = $this
    ->getMetaProperties()) {
    $options = [];
    foreach ($meta_properties as $key => $meta_property) {
      if ($meta_property['options']) {
        $options[$key] = bynder_get_applicable_label_translation($meta_property);
      }
    }
    $form['usage_wrapper'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Usage restriction metadata'),
    ];
    $form['usage_wrapper']['usage_metaproperty'] = [
      '#type' => 'select',
      '#options' => $options,
      '#default_value' => $config
        ->get('usage_metaproperty'),
      '#description' => $this
        ->t('Select metaproperty which is responsible for usage restriction. This is used to limit what assets can be used. If the information is not provided we assume royalty free.'),
      '#empty_value' => 'none',
      '#empty_option' => $this
        ->t('- None -'),
      '#ajax' => [
        'callback' => '::usageAjaxCallback',
        'effect' => 'fade',
        'event' => 'change',
        'wrapper' => 'restrictions',
      ],
    ];
    $options = [];
    if ($form_state
      ->getValue('usage_metaproperty') !== NULL) {
      foreach ($meta_properties[$form_state
        ->getValue('usage_metaproperty')]['options'] as $meta_property_option) {
        $options[$meta_property_option['id']] = bynder_get_applicable_label_translation($meta_property_option);
      }
    }
    elseif ($config
      ->get('usage_metaproperty') && $config
      ->get('usage_metaproperty') !== 'none') {
      foreach ($meta_properties[$config
        ->get('usage_metaproperty')]['options'] as $meta_property_option) {
        $options[$meta_property_option['id']] = bynder_get_applicable_label_translation($meta_property_option);
      }
    }
    $form['usage_wrapper']['restrictions'] = [
      '#type' => 'container',
      '#attributes' => [
        'id' => [
          'restrictions',
        ],
      ],
      '#tree' => TRUE,
    ];
    if ($options) {
      $form['usage_wrapper']['restrictions']['royalty_free'] = [
        '#required' => TRUE,
        '#title' => 'Royalty free restriction level',
        '#type' => 'select',
        '#options' => $options,
        '#default_value' => $config
          ->get('restrictions.royalty_free'),
        '#description' => $this
          ->t('Select metaproperty option for assets that can be used everywhere.'),
      ];
      $form['usage_wrapper']['restrictions']['web_license'] = [
        '#required' => TRUE,
        '#title' => 'Web license restriction level',
        '#type' => 'select',
        '#options' => $options,
        '#default_value' => $config
          ->get('restrictions.web_license'),
        '#description' => $this
          ->t('Select metaproperty option for the assets that may be used only online.'),
      ];
      $form['usage_wrapper']['restrictions']['print_license'] = [
        '#required' => TRUE,
        '#title' => 'Print license restriction level',
        '#type' => 'select',
        '#options' => $options,
        '#default_value' => $config
          ->get('restrictions.print_license'),
        '#description' => $this
          ->t('Select metaproperty option for the assets that may be used only for print.'),
      ];
    }
  }
  else {
    $form['usage_wrapper'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Usage restriction metadata'),
    ];
    $form['usage_wrapper']['message'] = [
      '#markup' => $this
        ->t('To set usage restriction metaproperty provide valid credentials first.'),
    ];
  }
  $form['performance'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Performance settings'),
  ];
  $form['performance']['cache_lifetime'] = [
    '#type' => 'number',
    '#step' => 1,
    '#min' => 0,
    '#title' => $this
      ->t('Cache life time'),
    '#default_value' => $config
      ->get('cache_lifetime'),
    '#description' => $this
      ->t('The maximum lifetime of cached media information in seconds.'),
  ];
  $form['performance']['timeout'] = [
    '#type' => 'number',
    '#step' => 1,
    '#min' => 1,
    '#title' => $this
      ->t('Timeout'),
    '#default_value' => $config
      ->get('timeout'),
    '#description' => $this
      ->t('The timeout for all API requests to Bynder. Setting this too low might break uploading large files and other slow requests.'),
  ];
  return parent::buildForm($form, $form_state);
}