You are here

public function Bynder::buildConfigurationForm in Bynder 8

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides MediaTypeBase::buildConfigurationForm

File

src/Plugin/MediaEntity/Type/Bynder.php, line 261

Class

Bynder
Provides media type plugin for Bynder.

Namespace

Drupal\bynder\Plugin\MediaEntity\Type

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {

  // Check the connection with bynder.
  try {
    $this->bynderApi
      ->getBrands();
  } catch (\Exception $exception) {
    if ($this->accountProxy
      ->hasPermission('administer bynder configuration')) {
      drupal_set_message($this
        ->t('Connecting with Bynder failed. Check if the configuration is set properly <a href=":url">here</a>.', [
        ':url' => $this->urlGenerator
          ->generateFromRoute('bynder.configuration_form'),
      ]), 'error');
    }
    else {
      drupal_set_message($this
        ->t('Something went wrong with the Bynder connection. Please contact the site administrator.'), 'error');
    }
  }

  /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
  $bundle = $form_state
    ->getFormObject()
    ->getEntity();
  $options = [];
  $allowed_field_types = [
    'string',
    'string_long',
  ];
  foreach ($this->entityFieldManager
    ->getFieldDefinitions('media', $bundle
    ->id()) as $field_name => $field) {
    if (in_array($field
      ->getType(), $allowed_field_types) && !$field
      ->getFieldStorageDefinition()
      ->isBaseField()) {
      $options[$field_name] = $field
        ->getLabel();
    }
  }
  $form['source_field'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Field with source information'),
    '#description' => $this
      ->t('Field on media entity that stores the media UUID from Bynder. You can create a bundle without selecting a value for this dropdown initially. This dropdown can be populated after adding fields to the bundle.'),
    '#default_value' => $this->configuration['source_field'] ?: NULL,
    '#options' => $options,
  ];
  return $form;
}