You are here

class CKEditorMediaEmbedSettingsForm in CKEditor Media Embed Plugin 8

Class CKEditorMediaEmbedSettingsForm.

@package Drupal\ckeditor_media_embed\Form

Hierarchy

Expanded class hierarchy of CKEditorMediaEmbedSettingsForm

1 string reference to 'CKEditorMediaEmbedSettingsForm'
ckeditor_media_embed.routing.yml in ./ckeditor_media_embed.routing.yml
ckeditor_media_embed.routing.yml

File

src/Form/CKEditorMediaEmbedSettingsForm.php, line 21

Namespace

Drupal\ckeditor_media_embed\Form
View source
class CKEditorMediaEmbedSettingsForm extends ConfigFormBase {

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandler
   */
  protected $moduleHandler;

  /**
   * The URL generator.
   *
   * @var \Drupal\Core\Routing\UrlGeneratorInterface
   */
  protected $urlGenerator;

  /**
   * The library discovery service.
   *
   * @var \Drupal\Core\Asset\LibraryDiscoveryInterface
   */
  protected $libraryDiscovery;

  /**
   * Constructs a \Drupal\system\ConfigFormBase object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Extension\ModuleHandler $module_handler
   *   The module handler.
   * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
   *   The URL generator.
   * @param \Drupal\Core\Asset\LibraryDiscoveryInterface $library_discovery
   *   The library discovery service to use for retrieving information about
   *   the CKeditor library.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ModuleHandler $module_handler, UrlGeneratorInterface $url_generator, LibraryDiscoveryInterface $library_discovery) {
    parent::__construct($config_factory);
    $this->urlGenerator = $url_generator;
    $this->moduleHandler = $module_handler;
    $this->libraryDiscovery = $library_discovery;
  }

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

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('ckeditor_media_embed.settings');
    $version = AssetManager::getCKEditorVersion($this->libraryDiscovery, $this->configFactory);
    if (!AssetManager::pluginsAreInstalled($version)) {
      $this
        ->messenger()
        ->addWarning(_ckeditor_media_embed_get_install_instructions());
      return [];
    }
    $form['embed_provider'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Provider URL'),
      '#default_value' => $config
        ->get('embed_provider'),
      '#description' => $this
        ->t('A template for the URL of the provider endpoint.
        This URL will be queried for each resource to be embedded. By default CKEditor uses the Iframely service.<br />
        <em>Note that if you wish to support HTTPS with Iframely then you must create an account. Please read their <a href="https://iframely.com/docs/ckeditor">documentation</a> for more details.</em><br />
        <strong>Example</strong> <code>//example.com/api/oembed-proxy?resource-url={url}&callback={callback}&api_token=MYAPITOKEN</code><br />
        <strong>Default</strong> <code>http://ckeditor.iframe.ly/api/oembed?url={url}&callback={callback}</code>
        <br />
      '),
    ];
    if ($this->moduleHandler
      ->moduleExists('help')) {
      $form['embed_provider']['#description'] .= $this
        ->t('Check out the <a href=":help">help</a> page for more information.<br />', [
        ':help' => $this->urlGenerator
          ->generateFromRoute('help.page', [
          'name' => 'ckeditor_media_embed',
        ]),
      ]);
    }
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
    $embed_provider = $form_state
      ->getValue('embed_provider');
    $this
      ->prepareEmbedProviderValidation($embed_provider);
    if (!UrlHelper::isValid($embed_provider, TRUE)) {
      $form_state
        ->setErrorByName('embed_provider', $this
        ->t('The provider url was not valid.'));
    }
  }

  /**
   * Prepare the embed provider setting for validation.
   *
   * @param string $embed_provider
   *   The embed provider that should be prepared for validation.
   *
   * @return $this
   */
  protected function prepareEmbedProviderValidation(&$embed_provider) {
    if (strpos($embed_provider, '//') === 0) {
      $embed_provider = 'http:' . $embed_provider;
    }
    $embed_provider = str_replace('{url}', '', $embed_provider);
    $embed_provider = str_replace('{callback}', '', $embed_provider);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $this
      ->config('ckeditor_media_embed.settings')
      ->set('embed_provider', $form_state
      ->getValue('embed_provider'))
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CKEditorMediaEmbedSettingsForm::$libraryDiscovery protected property The library discovery service.
CKEditorMediaEmbedSettingsForm::$moduleHandler protected property The module handler.
CKEditorMediaEmbedSettingsForm::$urlGenerator protected property The URL generator. Overrides UrlGeneratorTrait::$urlGenerator
CKEditorMediaEmbedSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
CKEditorMediaEmbedSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
CKEditorMediaEmbedSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
CKEditorMediaEmbedSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
CKEditorMediaEmbedSettingsForm::prepareEmbedProviderValidation protected function Prepare the embed provider setting for validation.
CKEditorMediaEmbedSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
CKEditorMediaEmbedSettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
CKEditorMediaEmbedSettingsForm::__construct public function Constructs a \Drupal\system\ConfigFormBase 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::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.