You are here

class PushNotificationsConfigForm in Push Notifications 8

Class PushNotificationsConfigForm.

@package Drupal\push_notifications\Form

Hierarchy

Expanded class hierarchy of PushNotificationsConfigForm

1 string reference to 'PushNotificationsConfigForm'
push_notifications.routing.yml in ./push_notifications.routing.yml
push_notifications.routing.yml

File

src/Form/PushNotificationsConfigForm.php, line 14

Namespace

Drupal\push_notifications\Form
View source
class PushNotificationsConfigForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'push_notifications.apns',
      'push_notifications.gcm',
    ];
  }

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

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

    // Form constructor.
    $form = parent::buildForm($form, $form_state);

    // Get config.
    $config_apns = $this
      ->config('push_notifications.apns');
    $config_gcm = $this
      ->config('push_notifications.gcm');
    $configuration_apns_replacements = array(
      '@link' => 'http://blog.boxedice.com/2009/07/10/how-to-build-an-apple-push-notification-provider-server-tutorial/',
      ':cert_name_development' => push_notifications_get_certificate_name('development'),
      ':cert_name_production' => push_notifications_get_certificate_name('production'),
    );

    // APNS.
    $form['apns'] = array(
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Apple Push Notifications'),
    );
    $form['apns']['instructions'] = array(
      '#type' => 'markup',
      '#markup' => $this
        ->t('Configure Push Notifications for Apple\'s Push Notification Server. Select your environment. Both environments require the proper certificates in the \'certificates\' folder of this module.<br />The filename for the development certificate should be \':cert_name_development\', the production certificate should be \':cert_name_production\'. See <a href="@link" target="_blank">this link</a> for instructions on creating certificates.', $configuration_apns_replacements),
    );
    $form['apns']['regenerate_certificate_string_description'] = array(
      '#type' => 'item',
      '#title' => $this
        ->t('APNS Certificate Name'),
      '#markup' => $this
        ->t('Click here to create a new random name for your APNS certificates. Please note that you will have to update the filenames for both certificate files accordingly.'),
    );
    $form['apns']['regenerate_certificate_string'] = array(
      '#type' => 'submit',
      '#value' => $this
        ->t('Generate new certificate string'),
      '#submit' => array(
        '::submitRegenerateCertificateString',
      ),
    );
    $form['apns']['apns_environment'] = array(
      '#type' => 'select',
      '#title' => $this
        ->t('APNS Environment'),
      '#description' => $this
        ->t('Select the active APNS Environment. Please note that development certificates do not work with apps released in the Apple app store; production certificates only work with apps released in the app store.'),
      '#options' => array(
        'development' => 'Development',
        'production' => 'Production',
      ),
      '#default_value' => $config_apns
        ->get('environment'),
    );
    $stream_context_limit_options = array(
      1,
      5,
      10,
      25,
      50,
    );
    $form['apns']['apns_stream_context_limit'] = array(
      '#type' => 'select',
      '#title' => $this
        ->t('Stream Context Limit'),
      '#description' => $this
        ->t('Defines the amount of messages sent per stream limit, i.e. how many notifications are sent per connection created with Apple\'s servers. The higher the limit, the faster the message delivery. If the limit is too high, messages might not get delivered at all. Unclear (to me) what Apple\'s <em>actual</em> limit is.'),
      '#options' => array_combine($stream_context_limit_options, $stream_context_limit_options),
      '#default_value' => $config_apns
        ->get('stream_context_limit'),
    );
    $form['apns']['apns_passphrase'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Passphrase'),
      '#description' => $this
        ->t('If your APNS certificate has a passphrase, enter it here. Otherwise, leave this field blank.'),
      '#default_value' => $config_apns
        ->get('passphrase'),
    );
    $form['apns']['apns_certificate_folder'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('APNS Certificate Folder Path'),
      '#description' => $this
        ->t('The preferred location for the certificate files is a folder outside of your web root, i.e. a folder not accessible through the Internet. Specify the full path here, e.g. \'/users/danny/drupal_install/certificates/\'. If you are using the \'certificates\' folder within the module directory, leave this field blank.'),
      '#default_value' => $config_apns
        ->get('certificate_folder'),
    );
    $form['apns']['apns_set_entrust_certificate'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Set Entrust root certificate'),
      '#description' => t('If APNS does not work and you are getting errors like %php_error_string your server might be missing the Entrust root certificate. Enable this to explicitely add it when establishing a connection to APNS. See more in this <a href="@link_kb" target="_blank">Knowledgebase Article</a> and the <a href="@link_entrus" target="_blank">Entrust Root Certificate Downloads</a>', array(
        '%php_error_string' => 'Warning: stream_socket_client() [...]',
        '@link_entrust' => 'https://www.entrust.com/get-support/ssl-certificate-support/root-certificate-downloads/',
        '@link_kb' => 'http://stackoverflow.com/questions/4817520/why-ssl-of-entrust-ssl-certificate-is-required-for-apns',
      )),
      '#default_value' => $config_apns
        ->get('set_entrust_certificate'),
    );

    // Google Cloud Messaging.
    $form['gcm'] = array(
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Google Cloud Messaging'),
      '#description' => $this
        ->t('Enter your Google Cloud Messaging details.'),
    );
    $form['gcm']['gcm_api_key'] = array(
      '#type' => 'textfield',
      '#title' => t('Google Cloud Messaging API Key'),
      '#description' => t('Enter the API key for your Google Cloud project'),
      '#default_value' => $config_gcm
        ->get('api_key'),
    );
    return parent::buildForm($form, $form_state);
  }

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

    // If custom certificate directory is set, ensure the directory exists.
    $custom_dir = $form_state
      ->getValue('apns_certificate_folder');
    if (!empty($custom_dir)) {
      if (!file_exists(Html::escape($custom_dir))) {
        $form_state
          ->setErrorByName('apns_certificate_folder', $this
          ->t('Custom certificate directory does not exist. Please create the path before saving your configuration.'));
      }
    }
  }

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

    // Store APNS config.
    $config_apns = $this
      ->config('push_notifications.apns');
    $config_apns
      ->set('environment', $form_state
      ->getValue('apns_environment'));
    $config_apns
      ->set('stream_context_limit', $form_state
      ->getValue('apns_stream_context_limit'));
    $config_apns
      ->set('passphrase', $form_state
      ->getValue('apns_passphrase'));
    $config_apns
      ->set('certificate_folder', $form_state
      ->getValue('apns_certificate_folder'));
    $config_apns
      ->set('set_entrust_certificate', $form_state
      ->getValue('apns_set_entrust_certificate'));
    $config_apns
      ->save();

    // Store GCM config.
    $config_gcm = $this
      ->config('push_notifications.gcm');
    $config_gcm
      ->set('api_key', $form_state
      ->getValue('gcm_api_key'));
    $config_gcm
      ->save();
  }

  /**
   * Regenerates the APNS random certificate string.
   */
  public function submitRegenerateCertificateString(array &$form, FormStateInterface $form_state) {
    push_notifications_set_random_certificate_string();
    drupal_set_message($this
      ->t('The names for your APNS certificates were successfully changed. Please rename both certificate files.'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
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.
PushNotificationsConfigForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
PushNotificationsConfigForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
PushNotificationsConfigForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
PushNotificationsConfigForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
PushNotificationsConfigForm::submitRegenerateCertificateString public function Regenerates the APNS random certificate string.
PushNotificationsConfigForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
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.