You are here

class BynderConfigurationForm in Bynder 8

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

Configure bynder to enable OAuth based access.

@package Drupal\bynder\Form

Hierarchy

Expanded class hierarchy of BynderConfigurationForm

1 file declares its use of BynderConfigurationForm
ConfigurationFormTest.php in tests/src/FunctionalJavascript/ConfigurationFormTest.php
1 string reference to 'BynderConfigurationForm'
bynder.routing.yml in ./bynder.routing.yml
bynder.routing.yml

File

src/Form/BynderConfigurationForm.php, line 20

Namespace

Drupal\bynder\Form
View source
class BynderConfigurationForm extends ConfigFormBase {

  /**
   * Bynder api service.
   *
   * @var \Drupal\bynder\BynderApiInterface
   *   Bynder api service.
   */
  protected $bynder;

  /**
   * The renderer service.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * Meta properties from Bynder.
   *
   * @var array
   */
  protected $metaProperties;

  /**
   * Derivatives information.
   *
   * @var array
   */
  protected $derivatives;

  /**
   * Constructs a BynderConfigurationForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\bynder\BynderApiInterface $bynder
   *   The Bynder API service.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, BynderApiInterface $bynder, RendererInterface $renderer) {
    parent::__construct($config_factory);
    $this->bynder = $bynder;
    $this->renderer = $renderer;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('bynder_api'), $container
      ->get('renderer'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'bynder_configuration_form';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'bynder.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  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);
  }

  /**
   * Ajax callback for usage metadata select field.
   */
  public function usageAjaxCallback(array &$form, FormStateInterface &$form_state) {
    $form_state
      ->setRebuild(TRUE);
    return $form['usage_wrapper']['restrictions'];
  }

  /**
   * AJAX callback for test connection button.
   */
  public function testConnectionAjaxCallback(array $form, FormStateInterface $form_state) {
    $response = new AjaxResponse();
    $return_markup = [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#attributes' => [
        'id' => 'connection-test',
      ],
    ];
    $credentials = $form_state
      ->getValue('credentials');
    if ($this
      ->testApiConnection($credentials['consumer_key'], $credentials['consumer_secret'], $credentials['token'], $credentials['token_secret'], $credentials['account_domain'])) {
      $return_markup['#value'] = $this
        ->t('The API connection was established successfully.');
      $return_markup['#attributes']['style'] = 'color: green;';
    }
    else {
      $return_markup['#value'] = $this
        ->t('Could not establish connection with Bynder. Check your credentials or <a href=":support">contact support.</a>', [
        ':support' => 'mailto:support@getbynder.com',
      ]);
      $return_markup['#attributes']['style'] = 'color: red;';
    }
    $response
      ->addCommand(new ReplaceCommand('#connection-test', $this->renderer
      ->render($return_markup)));
    return $response;
  }

  /**
   * Submit callback that will update cached data.
   */
  public static function submitReloadDerivatives(array &$form, FormStateInterface $form_state) {
    \Drupal::service('bynder_api')
      ->updateCachedData();
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $credentials = $form_state
      ->getValue('credentials');
    foreach ([
      'consumer_secret',
      'token_secret',
    ] as $name) {
      if (!ctype_alnum(trim($credentials[$name]))) {
        $form_state
          ->setError($form[$name], $this
          ->t('@label needs to contain only letters and numbers.', [
          '@label' => $form[$name]['#title']
            ->render(),
        ]));
      }
    }
    foreach ([
      'consumer_key',
      'token',
    ] as $name) {
      $parts = explode('-', trim($credentials[$name]));
      if (strlen($parts[0]) !== 8 || strlen($parts[1]) !== 4 || strlen($parts[2]) !== 4 || strlen($parts[3]) !== 16 || isset($parts[4])) {
        $form_state
          ->setError($form[$name], $this
          ->t('@label needs to use the pattern 8-4-4-16.', [
          '@label' => $form[$name]['#title']
            ->render(),
        ]));
      }
      foreach ($parts as $part) {
        if (!ctype_alnum($part)) {
          $form_state
            ->setError($form[$name], $this
            ->t('@label needs to use only numbers and letters separated with "-".', [
            '@label' => $form[$name]['#title']
              ->render(),
          ]));
          break;
        }
      }
    }

    // Makes sure we don't have a leading slash in the domain url.
    $credentials['account_domain'] = rtrim($credentials['account_domain'], '/');
    $form_state
      ->setValue([
      'credentials',
      'account_domain',
    ], $credentials['account_domain']);
    if (!(substr(trim($credentials['account_domain']), 0, 8) === 'https://') || filter_var(trim($credentials['account_domain']), FILTER_VALIDATE_URL) === FALSE) {
      $form_state
        ->setError($form['account_domain'], $this
        ->t('Account domain expect a valid secure url format, as provided to you by Bynder: ":url".', [
        ':url' => 'https://bynder-domain.extension/',
      ]));
    }
    if ($form_state
      ->getValue('test_connection')) {
      if (!$form_state::hasAnyErrors() && !$this
        ->testApiConnection($credentials['consumer_key'], $credentials['consumer_secret'], $credentials['token'], $credentials['token_secret'], $credentials['account_domain'])) {
        $form_state
          ->setErrorByName('credentials', $this
          ->t('Could not establish connection with Bynder. Check your credentials or <a href=":support">contact support.</a>', [
          ':support' => 'mailto:support@getbynder.com',
        ]));
      }
    }
    parent::validateForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $is_initial_save = $this
      ->config('bynder.settings')
      ->get('consumer_key') === '';
    $is_initial_save &= $this
      ->config('bynder.settings')
      ->get('consumer_secret') === '';
    $is_initial_save &= $this
      ->config('bynder.settings')
      ->get('token') === '';
    $is_initial_save &= $this
      ->config('bynder.settings')
      ->get('token_secret') === '';
    $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('consumer_key', $credentials['consumer_key'])
      ->set('consumer_secret', $credentials['consumer_secret'])
      ->set('token', $credentials['token'])
      ->set('token_secret', $credentials['token_secret'])
      ->set('account_domain', $credentials['account_domain'])
      ->set('debug', $form_state
      ->getValue('debug'))
      ->set('cache_lifetime', $form_state
      ->getValue('cache_lifetime'))
      ->set('timeout', $form_state
      ->getValue('timeout'))
      ->set('usage_metaproperty', $form_state
      ->getValue('usage_metaproperty'))
      ->set('restrictions.royalty_free', $restrictions['royalty_free'])
      ->set('restrictions.web_license', $restrictions['web_license'])
      ->set('restrictions.print_license', $restrictions['print_license'])
      ->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) {
      }
    }
  }

  /**
   * Tests connection with the Bynder API.
   *
   * @param string $consumer_key
   *   Consumer key.
   * @param string $consumer_secret
   *   Consumer secret.
   * @param string $token
   *   Token.
   * @param string $token_secret
   *   Token secret.
   * @param string $account_domain
   *   Account domain.
   *
   * @return bool
   *   Whether communication was successfully established.
   */
  protected function testApiConnection($consumer_key, $consumer_secret, $token, $token_secret, $account_domain) {
    try {
      $this->bynder
        ->setBynderConfiguration([
        'consumerKey' => $consumer_key,
        'consumerSecret' => $consumer_secret,
        'token' => $token,
        'tokenSecret' => $token_secret,
        'baseUrl' => $account_domain,
      ]);
      $this->bynder
        ->getBrands();
    } catch (\Exception $exception) {
      return FALSE;
    }
    return TRUE;
  }

  /**
   * Gets the meta properties from Bynder.
   *
   * @return array
   *   Returns meta properties from Bynder.
   */
  protected function getMetaProperties() {
    if (!$this->metaProperties) {
      try {
        $this->metaProperties = $this->bynder
          ->getMetaproperties();
      } catch (\Exception $e) {
        return [];
      }
    }
    return $this->metaProperties;
  }

  /**
   * Gets the derivatives from Bynder.
   *
   * @return array
   *   Derivatives info.
   */
  protected function getDerivatives() {
    if (!$this->derivatives) {
      try {
        $this->derivatives = $this->bynder
          ->getDerivatives();
      } catch (\Exception $e) {
        return [];
      }
    }
    return $this->derivatives;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BynderConfigurationForm::$bynder protected property Bynder api service.
BynderConfigurationForm::$derivatives protected property Derivatives information.
BynderConfigurationForm::$metaProperties protected property Meta properties from Bynder.
BynderConfigurationForm::$renderer protected property The renderer service.
BynderConfigurationForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
BynderConfigurationForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
BynderConfigurationForm::getDerivatives protected function Gets the derivatives from Bynder.
BynderConfigurationForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
BynderConfigurationForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
BynderConfigurationForm::getMetaProperties protected function Gets the meta properties from Bynder.
BynderConfigurationForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
BynderConfigurationForm::submitReloadDerivatives public static function Submit callback that will update cached data.
BynderConfigurationForm::testApiConnection protected function Tests connection with the Bynder API.
BynderConfigurationForm::testConnectionAjaxCallback public function AJAX callback for test connection button.
BynderConfigurationForm::usageAjaxCallback public function Ajax callback for usage metadata select field.
BynderConfigurationForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
BynderConfigurationForm::__construct public function Constructs a BynderConfigurationForm object. Overrides ConfigFormBase::__construct
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.