You are here

class AddToHomescreenForm in Add to homescreen 8

Configure Add to Homescreen.

Hierarchy

Expanded class hierarchy of AddToHomescreenForm

1 string reference to 'AddToHomescreenForm'
addtohomescreen.routing.yml in ./addtohomescreen.routing.yml
addtohomescreen.routing.yml

File

src/Form/AddToHomescreenForm.php, line 11

Namespace

Drupal\addtohomescreen\Form
View source
class AddToHomescreenForm extends ConfigFormBase {

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);
    $config = $this
      ->config('addtohomescreen.settings');
    $form['library'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Configuration'),
      '#description' => $this
        ->t('For more information about these options, visit <a href="@addtohomescreen_url">Add to homescreen on Github</a>.', [
        '@addtohomescreen_url' => 'https://github.com/cubiq/add-to-homescreen',
      ]),
      '#open' => TRUE,
    ];
    $form['library']['debug'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Debug mode'),
      '#description' => $this
        ->t('Some of the preliminary checks are skipped and the message is shown on desktop browsers and unsupported devices as well.'),
      '#default_value' => $config
        ->get('debug'),
    ];
    $form['library']['modal'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Modal'),
      '#description' => $this
        ->t('Prevents further actions on the website until the message is closed.'),
      '#default_value' => $config
        ->get('modal'),
    ];
    $form['library']['mandatory'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Mandatory'),
      '#description' => $this
        ->t('The website is not accessible until the user adds the website to the homescreen.'),
      '#default_value' => $config
        ->get('mandatory'),
    ];
    $form['library']['skipfirstvisit'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Skip first visit'),
      '#description' => $this
        ->t('Prevent the message from appearing the first time the user visits your website. It is highly recommended to enable this option!'),
      '#default_value' => $config
        ->get('skipfirstvisit'),
    ];
    $form['library']['autostart'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Autostart'),
      '#description' => $this
        ->t('The message is not shown automatically and you have to trigger it programmatically.'),
      '#default_value' => $config
        ->get('autostart'),
    ];
    $form['library']['icon'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Icon'),
      '#description' => $this
        ->t('Display the touch icon in the pop up message.'),
      '#default_value' => $config
        ->get('icon'),
    ];
    $form['library']['startdelay'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Start delay'),
      '#description' => $this
        ->t('Seconds to wait from page load before showing the message.'),
      '#default_value' => $config
        ->get('startdelay'),
      '#size' => 10,
    ];
    $form['library']['lifespan'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Lifespan'),
      '#description' => $this
        ->t('Seconds to wait before automatically closing the message. Set to 0 to disable automatic removal.'),
      '#default_value' => $config
        ->get('lifespan'),
      '#size' => 10,
    ];
    $form['library']['displaypace'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Display pace'),
      '#description' => $this
        ->t('Minutes before the message is shown again. By default it is set to 1440, meaning the message is shown once per day.'),
      '#default_value' => $config
        ->get('displaypace'),
      '#size' => 10,
    ];
    $form['library']['maxdisplaycount'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Maximum display count'),
      '#description' => $this
        ->t('Absolute maximum number of times the call out will be shown. Set to 0 for no maximum.'),
      '#default_value' => $config
        ->get('maxdisplaycount'),
      '#size' => 10,
    ];
    $form['library']['use_custom_message'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Use a custom message'),
      '#description' => $this
        ->t('Add to homescreen comes with a localized and device specific message. You can override this message with your own.'),
      '#default_value' => $config
        ->get('use_custom_message'),
    ];
    $form['library']['message'] = [
      '#title' => $this
        ->t('Message'),
      '#type' => 'textarea',
      '#default_value' => $config
        ->get('message', $this
        ->t('To add this web app to the home screen: tap %icon and then <strong>Add to homescreen</strong>.')),
      '#description' => $this
        ->t('Available replacements: %icon'),
      '#states' => [
        'disabled' => [
          ':input[name="use_custom_message"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['compression_settings'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Library compression settings'),
      '#collapsible' => TRUE,
      '#open' => FALSE,
    ];
    $form['compression_settings']['compression_type'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Choose compression level'),
      '#options' => [
        'minified' => $this
          ->t('Production (Minified)'),
        'source' => $this
          ->t('Development (Uncompressed Code)'),
      ],
      '#default_value' => $config
        ->get('compression_type', 'minified'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this
      ->config('addtohomescreen.settings')
      ->set('debug', $form_state
      ->getValue('debug'))
      ->set('modal', $form_state
      ->getValue('modal'))
      ->set('mandatory', $form_state
      ->getValue('mandatory'))
      ->set('skipfirstvisit', $form_state
      ->getValue('skipfirstvisit'))
      ->set('autostart', $form_state
      ->getValue('autostart'))
      ->set('icon', $form_state
      ->getValue('icon'))
      ->set('startdelay', $form_state
      ->getValue('startdelay'))
      ->set('lifespan', $form_state
      ->getValue('lifespan'))
      ->set('displaypace', $form_state
      ->getValue('displaypace'))
      ->set('maxdisplaycount', $form_state
      ->getValue('maxdisplaycount'))
      ->set('use_custom_message', $form_state
      ->getValue('use_custom_message'))
      ->set('message', $form_state
      ->getValue('message'))
      ->set('compression_type', $form_state
      ->getValue('compression_type'))
      ->save();
    parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AddToHomescreenForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
AddToHomescreenForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
AddToHomescreenForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
AddToHomescreenForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
ConfigFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 13
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 11
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.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
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.