You are here

class FarmSettingsFarmInfoForm in farmOS 2.x

Form for configuring basic farm info.

Hierarchy

Expanded class hierarchy of FarmSettingsFarmInfoForm

1 string reference to 'FarmSettingsFarmInfoForm'
farm_settings.routing.yml in modules/core/settings/farm_settings.routing.yml
modules/core/settings/farm_settings.routing.yml

File

modules/core/settings/src/Form/FarmSettingsFarmInfoForm.php, line 16

Namespace

Drupal\farm_settings\Form
View source
class FarmSettingsFarmInfoForm extends ConfigFormBase {

  /**
   * The country manager.
   *
   * @var \Drupal\Core\Locale\CountryManagerInterface
   */
  protected $countryManager;

  /**
   * Constructs a RegionalForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Locale\CountryManagerInterface $country_manager
   *   The country manager.
   */
  public function __construct(ConfigFactoryInterface $config_factory, CountryManagerInterface $country_manager) {
    parent::__construct($config_factory);
    $this->countryManager = $country_manager;
  }

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

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

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'system.date',
      'system.site',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    // Set the form title.
    $form['#title'] = $this
      ->t('Configure Farm Info');

    // Get the system.site config.
    $site = $this
      ->config('system.site');

    // Textfield to edit site name.
    $form['site_name'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Farm name'),
      '#default_value' => $site
        ->get('name'),
      '#maxlength' => 128,
      '#required' => TRUE,
    ];

    // Get the system.date config.
    $system_date = $this
      ->config('system.date');

    // Get countries.
    $countries = $this->countryManager
      ->getList();

    // Default country select.
    $form['default_country'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Default country'),
      '#empty_value' => '',
      '#default_value' => $system_date
        ->get('country.default'),
      '#options' => $countries,
      '#attributes' => [
        'class' => [
          'country-detect',
        ],
      ],
    ];

    // Get list of timezones.
    $timezones = system_time_zones();

    // Dropdown to select default timezone.
    $form['default_timezone'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Default timezone'),
      '#description' => $this
        ->t('The default timezone of the farmOS server. Note that users can configure individual timezones later.'),
      '#options' => $timezones,
      '#default_value' => $system_date
        ->get('timezone.default'),
      '#required' => TRUE,
    ];

    // Submit button.
    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Save'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // Get the submitted site name.
    $site_name = $form_state
      ->getvalue('site_name');

    // Update system.site config.
    $this->configFactory
      ->getEditable('system.site')
      ->set('name', $site_name)
      ->save();

    // Get the submitted country.
    $default_country = $form_state
      ->getValue('default_country');

    // Get the submitted timezone.
    $default_timezone = $form_state
      ->getValue('default_timezone');

    // Update system.date config.
    $this->configFactory
      ->getEditable('system.date')
      ->set('timezone.default', $default_timezone)
      ->set('country.default', $default_country)
      ->save();

    // Display message from parent submitForm.
    parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FarmSettingsFarmInfoForm::$countryManager protected property The country manager.
FarmSettingsFarmInfoForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
FarmSettingsFarmInfoForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
FarmSettingsFarmInfoForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
FarmSettingsFarmInfoForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
FarmSettingsFarmInfoForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
FarmSettingsFarmInfoForm::__construct public function Constructs a RegionalForm object. Overrides ConfigFormBase::__construct
FormBase::$configFactory protected property The config factory. 3
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. 3
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.
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.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 72
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. 27
MessengerTrait::messenger public function Gets the messenger. 27
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. 4
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.