You are here

public function BynderSearch::buildConfigurationForm in Bynder 8

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

Overrides BynderWidgetBase::buildConfigurationForm

File

src/Plugin/EntityBrowser/Widget/BynderSearch.php, line 189

Class

BynderSearch
Uses a Bynder API to search and provide entity listing in a browser's widget.

Namespace

Drupal\bynder\Plugin\EntityBrowser\Widget

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildConfigurationForm($form, $form_state);
  $form['items_per_page'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Items per page'),
    '#default_value' => $this->configuration['items_per_page'],
    '#options' => [
      '10' => 10,
      '15' => 15,
      '25' => 25,
      '50' => 50,
    ],
  ];
  $form['tags_filter'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable tags filter'),
    '#default_value' => $this->configuration['tags_filter'],
  ];
  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['items_per_page']['#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'), 'entity.media_bundle.add_form')
        ->toString(),
    ]);
  }
  try {
    $options = [];
    foreach ($this->bynderApi
      ->getMetaproperties() as $key => $meta_property) {
      if ($meta_property['isFilterable']) {
        $options[$key] = bynder_get_applicable_label_translation($meta_property);
      }
    }
    $form['allowed_properties'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Allowed metadata properties'),
      '#description' => $this
        ->t('Select filters that should be available in the Entity Browser widget.'),
      '#multiple' => TRUE,
      '#default_value' => $this->configuration['allowed_properties'],
      '#options' => $options,
    ];
  } catch (\Exception $e) {
    (new UnableToConnectException())
      ->logException()
      ->displayMessage();
  }
  return $form;
}