You are here

public function BynderUpload::buildConfigurationForm in Bynder 8

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

Overrides BynderWidgetBase::buildConfigurationForm

File

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

Class

BynderUpload
Uses upload to create media entities.

Namespace

Drupal\bynder\Plugin\EntityBrowser\Widget

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildConfigurationForm($form, $form_state);
  foreach ($this->entityTypeManager
    ->getStorage('media_bundle')
    ->loadMultiple() as $bundle) {

    /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
    if ($bundle
      ->getType() instanceof Bynder) {
      $form['media_bundle']['#options'][$bundle
        ->id()] = $bundle
        ->label();
    }
  }
  if (empty($form['media_bundle']['#options'])) {
    $form['media_bundle']['#disabled'] = TRUE;
    $form['media_bundle']['#description'] = $this
      ->t('You must @create_bundle before using this widget.', [
      '@create_bundle' => Link::createFromRoute($this
        ->t('create a Bynder media bundle'), 'media.bundle_add')
        ->toString(),
    ]);
  }
  $brand_options = [];
  try {
    foreach ($this->bynderApi
      ->getBrands() as $brand) {
      $brand_options[$brand['id']] = $brand['name'];
      foreach ($brand['subBrands'] as $sub_brand) {
        $brand_options[$sub_brand['id']] = '- ' . $sub_brand['name'];
      }
    }
  } catch (RequestException $e) {
    (new UnableToConnectException())
      ->displayMessage();
  }
  $form['brand'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Brand'),
    '#default_value' => $this->configuration['brand'],
    '#required' => TRUE,
    '#options' => $brand_options,
  ];
  if (empty($this->configuration['brand'])) {
    $form['brand']['#empty_option'] = $this
      ->t('- Set brand -');
  }
  $form['extensions'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Allowed file extensions'),
    '#desciption' => $this
      ->t('A space separated list of file extensions'),
    '#default_value' => $this->configuration['extensions'],
  ];
  $form['dropzone_description'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Dropzone drag-n-drop zone text'),
    '#default_value' => $this->configuration['dropzone_description'],
  ];
  $form['tags'] = [
    '#type' => 'textfield',
    '#description' => $this
      ->t('Comma-separated list of tags that should be assigned to all uploaded assets.'),
    '#title' => $this
      ->t('Tags'),
    '#default_value' => implode(', ', $this->configuration['tags']),
  ];
  $metaproperties = $this->bynderApi
    ->getMetaproperties();
  $metaproperty_options = [];
  foreach ($metaproperties as $metaproperty) {
    $metaproperty_options[$metaproperty['id']] = $metaproperty['label'];
  }
  $form['metaproperties'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Metaproperties'),
    '#options' => $metaproperty_options,
    '#description' => $this
      ->t('Select metaproperties whose options should be added to all uploaded assets. Options will be selected in the next step.'),
    '#default_value' => array_keys($this->configuration['metaproperty_options']),
    '#ajax' => [
      'callback' => 'Drupal\\bynder\\Plugin\\EntityBrowser\\Widget\\BynderUpload::ajaxMetaproperties',
      'wrapper' => 'metaproperty-options-wrapper',
    ],
  ];
  $parents = [
    'table',
    $this->uuid,
    'form',
    'metaproperties',
  ];
  $enabled_metaproperties = $form_state
    ->getValue($parents) ?: array_keys($this->configuration['metaproperty_options']);
  $enabled_metaproperties = array_filter(array_values($enabled_metaproperties));
  $form['metaproperty_options'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Metaproperty options'),
    '#attributes' => [
      'id' => 'metaproperty-options-wrapper',
    ],
    '#open' => TRUE,
  ];
  if (empty($enabled_metaproperties)) {
    $form['metaproperty_options']['#attributes']['class'][] = 'visually-hidden';
  }
  foreach ($metaproperties as $metaproperty) {
    if (in_array($metaproperty['id'], $enabled_metaproperties)) {
      $options = [];
      foreach ($metaproperty['options'] as $option) {
        $options[$option['id']] = $option['displayLabel'];
      }
      $form['metaproperty_options'][$metaproperty['id']] = [
        '#type' => 'select',
        '#multiple' => TRUE,
        '#title' => $metaproperty['label'],
        '#options' => $options,
        '#default_value' => $this->configuration['metaproperty_options'][$metaproperty['id']],
      ];
    }
  }
  return $form;
}