You are here

class SynonymSettingsForm in Search API Synonym 8

Class SynonymSettingsForm.

@package Drupal\search_api_synonym\Form

Hierarchy

Expanded class hierarchy of SynonymSettingsForm

1 string reference to 'SynonymSettingsForm'
search_api_synonym.routing.yml in ./search_api_synonym.routing.yml
search_api_synonym.routing.yml

File

src/Form/SynonymSettingsForm.php, line 18

Namespace

Drupal\search_api_synonym\Form
View source
class SynonymSettingsForm extends ConfigFormBase {

  /**
   * An array containing available export plugins.
   *
   * @var array
   */
  protected $availablePlugins = [];

  /**
   * Constructs a VacancySourceForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\search_api_synonym\Export\ExportPluginManager $manager
   *   The synonyms export plugin manager.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ExportPluginManager $manager) {
    parent::__construct($config_factory);
    foreach ($manager
      ->getAvailableExportPlugins() as $id => $definition) {
      $this->availablePlugins[$id] = $manager
        ->createInstance($id);
    }
  }

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

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config($this
      ->getEditableConfigNames()[0]);

    // Add cron settings
    $cron = $config
      ->get('cron');
    $options = [];
    foreach ($this->availablePlugins as $id => $source) {
      $definition = $source
        ->getPluginDefinition();
      $options[$id] = $definition['label'];
    }
    $intervals = [
      0,
      900,
      1800,
      3600,
      10800,
      21600,
      43200,
      86400,
      604800,
    ];
    $intervals = array_combine($intervals, $intervals);
    $intervals = array_map([
      \Drupal::service('date.formatter'),
      'formatInterval',
    ], $intervals);
    $intervals[0] = t('Never');
    $form['interval'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Export synonyms every'),
      '#description' => $this
        ->t('How often should Drupal export synonyms?'),
      '#default_value' => isset($cron['interval']) ? $cron['interval'] : 86400,
      '#options' => $intervals,
    ];
    $form['cron'] = [
      '#title' => $this
        ->t('Cron settings'),
      '#type' => 'details',
      '#open' => TRUE,
      '#tree' => TRUE,
      '#states' => array(
        'invisible' => array(
          ':input[name="interval"]' => array(
            'value' => 0,
          ),
        ),
      ),
    ];
    $form['cron']['plugin'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Synonym export plugin'),
      '#description' => $this
        ->t('Select the export plugin being used by cron.'),
      '#default_value' => $cron['plugin'] ? $cron['plugin'] : '',
      '#options' => $options,
    ];
    $form['cron']['type'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Type'),
      '#description' => $this
        ->t('Which synonym type should be exported by cron?'),
      '#default_value' => $cron['type'] ? $cron['type'] : 'all',
      '#options' => [
        'all' => $this
          ->t('All'),
        'synonym' => $this
          ->t('Synonyms'),
        'spelling_error' => $this
          ->t('Spelling errors'),
      ],
    ];
    $form['cron']['filter'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Filter'),
      '#description' => $this
        ->t('Which filters should be used when selecting synonyms.'),
      '#default_value' => $cron['filter'] ? $cron['filter'] : 'none',
      '#options' => [
        'none' => $this
          ->t('No filter'),
        'nospace' => $this
          ->t('Synonyms without spaces in the word'),
        'onlyspace' => $this
          ->t('Synonyms with spaces in the word'),
      ],
    ];
    $form['cron']['separate_files'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Separate files'),
      '#description' => $this
        ->t('Export synonyms with and without spaces into separate files.'),
      '#default_value' => $cron['separate_files'] ? $cron['separate_files'] : '',
      '#states' => [
        'visible' => [
          ':radio[name="cron[filter]"]' => [
            'value' => 'none',
          ],
        ],
      ],
    ];
    $form['cron']['export_if_changed'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Only export if changes'),
      '#description' => $this
        ->t('Only export synonyms if their is either new or changed synonyms since last export.'),
      '#default_value' => $cron['export_if_changed'] ? $cron['export_if_changed'] : FALSE,
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValue('cron');
    $values['interval'] = $form_state
      ->getValue('interval');
    $this
      ->config($this
      ->getEditableConfigNames()[0])
      ->set('cron', $values)
      ->save();
    parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
SynonymSettingsForm::$availablePlugins protected property An array containing available export plugins.
SynonymSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SynonymSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
SynonymSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SynonymSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SynonymSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SynonymSettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
SynonymSettingsForm::__construct public function Constructs a VacancySourceForm object. Overrides ConfigFormBase::__construct
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.