You are here

class NotificationsWidgetLoggerSettingsForm in Notifications widget 8

Provides settings for Activity tracking module.

Hierarchy

Expanded class hierarchy of NotificationsWidgetLoggerSettingsForm

1 string reference to 'NotificationsWidgetLoggerSettingsForm'
notifications_widget.routing.yml in ./notifications_widget.routing.yml
notifications_widget.routing.yml

File

src/Form/NotificationsWidgetLoggerSettingsForm.php, line 17

Namespace

Drupal\notifications_widget\Form
View source
class NotificationsWidgetLoggerSettingsForm extends ConfigFormBase {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The messager type manager.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messengerTypeManager;

  /**
   * The module manager service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Constructs an AutologoutSettingsForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module manager service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
   *   The module entity manager service.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger_manager
   *   The module messager manager service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_manager, MessengerInterface $messenger_manager) {
    parent::__construct($config_factory);
    $this->moduleHandler = $module_handler;
    $this->entityTypeManager = $entity_manager;
    $this->messengerTypeManager = $messenger_manager;
  }

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

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

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

  /**
   * Build form.
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('notifications_widget.settings');
    $form['excluded_entities'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Exclude Entities (comma seperated)'),
      '#description' => $this
        ->t('Exclude the notification entities for excluding'),
      '#default_value' => $config
        ->get('excluded_entities'),
    ];
    $form['additional_entity_type'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Additional Entity Types (comma seperated)'),
      '#description' => $this
        ->t('Provide the Entity Types to enable and configure notifications'),
      '#default_value' => $config
        ->get('additional_entity_type'),
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $haystack = [
      'user',
      'node_type',
      'comment_type',
      'taxonomy_vocabulary',
    ];
    $values = $form_state
      ->getValues();
    $entities = explode(',', $values['additional_entity_type']);
    if (count(array_intersect($haystack, $entities)) > 0) {
      $form_state
        ->setErrorByName('additional_entity_type', $this
        ->t('Default entity type already exists.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $values = $form_state
      ->getValues();
    $savedEntities = explode(',', $this
      ->config('notifications_widget.settings')
      ->get('additional_entity_type'));
    $revisedEntities = explode(',', $values['additional_entity_type']);

    // Prepare removed entity types and remove its configuration.
    $deletedEntities = array_diff($savedEntities, $revisedEntities);
    $this
      ->removeEntityNotificationsSettings($deletedEntities, $values);
    $this
      ->config('notifications_widget.settings')
      ->set('excluded_entities', $values['excluded_entities'])
      ->set('additional_entity_type', $values['additional_entity_type'])
      ->save();
    $this->messengerTypeManager
      ->addMessage($this
      ->t('Notification widget will work well once you saved the configuration from <a href=":user_settings_url">Notification Widget Settings</a>.', [
      ':user_settings_url' => Url::fromRoute('notifications_widget.notifications_widget_settings')
        ->toString(),
    ]), 'warning');
  }

  /**
   * Remove addtional configuration for entity types.
   */
  public function removeEntityNotificationsSettings($entities, $values) {
    foreach ($entities as $entityType) {
      if ($this->entityTypeManager
        ->hasDefinition($entityType)) {
        $entityTypeStorage = $this->entityTypeManager
          ->getStorage($entityType);
        $entityTypes = $entityTypeStorage
          ->loadMultiple();
        if (!empty($entityTypes)) {
          foreach ($entityTypes as $entityType => $entityTypeData) {
            $enableSettings = $entityType . '_enable';
            $enableCreate = $entityType . '_noti_create_message';
            $enableUpdate = $entityType . '_noti_update_message';
            $enableDelete = $entityType . '_noti_delete_message';
            $createLink = $entityType . '_redirect_create_link';
            $updateLink = $entityType . '_redirect_update_link';
            $deleteLink = $entityType . '_redirect_delete_link';
            $this
              ->config('notifications_widget.settings')
              ->clear($enableSettings)
              ->clear($enableCreate)
              ->clear($enableUpdate)
              ->clear($enableDelete)
              ->clear($createLink)
              ->clear($updateLink)
              ->clear($deleteLink)
              ->save();
          }
        }
      }
    }
  }

}

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.
NotificationsWidgetLoggerSettingsForm::$entityTypeManager protected property The entity type manager.
NotificationsWidgetLoggerSettingsForm::$messengerTypeManager protected property The messager type manager.
NotificationsWidgetLoggerSettingsForm::$moduleHandler protected property The module manager service.
NotificationsWidgetLoggerSettingsForm::buildForm public function Build form. Overrides ConfigFormBase::buildForm
NotificationsWidgetLoggerSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
NotificationsWidgetLoggerSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
NotificationsWidgetLoggerSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
NotificationsWidgetLoggerSettingsForm::removeEntityNotificationsSettings public function Remove addtional configuration for entity types.
NotificationsWidgetLoggerSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
NotificationsWidgetLoggerSettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
NotificationsWidgetLoggerSettingsForm::__construct public function Constructs an AutologoutSettingsForm object. Overrides ConfigFormBase::__construct
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.