You are here

class PopupMessageSettingsForm in Popup message 8

Class PopupMessageSettingsForm.

@package Drupal\popup_message\Form

Hierarchy

Expanded class hierarchy of PopupMessageSettingsForm

1 string reference to 'PopupMessageSettingsForm'
popup_message.routing.yml in ./popup_message.routing.yml
popup_message.routing.yml

File

src/Form/PopupMessageSettingsForm.php, line 21

Namespace

Drupal\popup_message\Form
View source
class PopupMessageSettingsForm extends ConfigFormBase {

  /**
   * CssCollectionOptimizer service.
   *
   * @var \Drupal\Core\Asset\CssCollectionOptimizer
   */
  protected $cssOptimizer;

  /**
   * JsCollectionOptimizer service.
   *
   * @var \Drupal\Core\Asset\JsCollectionOptimizer
   */
  protected $jsOptimizer;

  /**
   * Drupal\Core\Entity\EntityRepositoryInterface service.
   *
   * @var \Drupal\Core\Entity\EntityRepositoryInterface
   */
  protected $entityRepository;

  /**
   * PopupMessageSettingsForm constructor.
   *
   * @param \Drupal\Core\Asset\CssCollectionOptimizer $cssOptimizer
   *   Load service css collection optimizer.
   * @param \Drupal\Core\Asset\JsCollectionOptimizer $jsOptimizer
   *   Load service js collection optimizer.
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository
   *   Entity repository.
   */
  public function __construct(CssCollectionOptimizer $cssOptimizer, JsCollectionOptimizer $jsOptimizer, EntityRepositoryInterface $entityRepository) {
    $this->cssOptimizer = $cssOptimizer;
    $this->jsOptimizer = $jsOptimizer;
    $this->entityRepository = $entityRepository;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('asset.css.collection_optimizer'), $container
      ->get('asset.js.collection_optimizer'), $container
      ->get('entity.repository'));
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('popup_message.settings');
    $form['popup_message_enable'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Enable Popup'),
      '#default_value' => $config
        ->get('enable') ? $config
        ->get('enable') : 0,
      '#options' => [
        1 => $this
          ->t('Enabled'),
        0 => $this
          ->t('Disabled'),
      ],
    ];
    $form['popup_message_fieldset'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Popup message settings'),
      '#collapsed' => FALSE,
      '#collapsible' => TRUE,
    ];
    $form['popup_message_fieldset']['popup_message_title'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Message title'),
      '#required' => TRUE,
      '#default_value' => $config
        ->get('title'),
    ];
    $popup_message_body = $config
      ->get('body');
    $form['popup_message_fieldset']['popup_message_body'] = [
      '#type' => 'text_format',
      '#base_type' => 'textarea',
      '#title' => $this
        ->t('Message body'),
      '#default_value' => $popup_message_body['value'],
      '#format' => isset($popup_message_body['format']) ? $popup_message_body['format'] : NULL,
    ];
    $form['popup_message_fieldset']['settings'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Settings'),
      '#expanded' => FALSE,
    ];
    $form['popup_message_fieldset']['cookie_settings'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Cookie settings'),
      '#expanded' => FALSE,
    ];
    $form['popup_message_fieldset']['settings']['popup_message_width'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Window width'),
      '#required' => TRUE,
      '#default_value' => empty($config
        ->get('width')) ? 300 : $config
        ->get('width'),
    ];
    $form['popup_message_fieldset']['settings']['popup_message_height'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Window height'),
      '#required' => TRUE,
      '#default_value' => empty($config
        ->get('height')) ? 300 : $config
        ->get('height'),
    ];
    $form['popup_message_fieldset']['cookie_settings']['popup_message_check_cookie'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Check cookie'),
      '#description' => $this
        ->t('If enabled message will be displayed only once per browser session'),
      '#default_value' => $config
        ->get('check_cookie') ? $config
        ->get('check_cookie') : 0,
      '#options' => [
        1 => $this
          ->t('Enabled'),
        0 => $this
          ->t('Disabled'),
      ],
    ];
    $form['popup_message_fieldset']['cookie_settings']['popup_message_expire'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Cookie lifetime in days'),
      '#description' => $this
        ->t('Define lifetime of the cookie in days. Message will not reappear until the expiration time is exceeded. If 0, popup will reappear if browser has been closed.'),
      '#default_value' => $config
        ->get('expire') ? $config
        ->get('expire') : 0,
    ];
    $form['popup_message_fieldset']['settings']['popup_message_delay'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Delay'),
      '#description' => $this
        ->t('Message will show after this number of seconds. Set to 0 to show instantly.'),
      '#default_value' => $config
        ->get('delay') ? $config
        ->get('delay') : 0,
    ];
    $form['popup_message_fieldset']['settings']['popup_message_close_delay'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Delay before auto close'),
      '#description' => $this
        ->t('Message will close after this number of seconds. Set to 0 to disable it.'),
      '#default_value' => $config
        ->get('close_delay') ? $config
        ->get('close_delay') : 0,
    ];

    // Styles.
    // Find styles in module directory.
    $directory = drupal_get_path('module', 'popup_message') . '/styles';
    $subdirectories = scandir($directory);
    $styles = [];
    foreach ($subdirectories as $subdir) {
      if (is_dir($directory . '/' . $subdir)) {
        if (file_exists($directory . '/' . $subdir . '/' . POPUP_MESSAGE_CSS_NAME)) {
          $lib_path = $subdir . '/' . POPUP_MESSAGE_CSS_NAME;
          $styles[$lib_path] = $subdir;
        }
      }
    }
    $form['popup_message_fieldset']['settings']['popup_message_cover_opacity'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Background opacity (%)'),
      '#min' => 0,
      '#max' => 100,
      '#step' => 5,
      '#default_value' => $config
        ->get('cover_opacity') ?? 70,
      '#description' => $this
        ->t('Allows to set a custom background opacity value in percentage (0-100%).'),
    ];
    $form['popup_message_fieldset']['settings']['popup_message_style'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Popup style'),
      '#default_value' => empty($config
        ->get('style')) ? 0 : $config
        ->get('style'),
      '#options' => $styles,
      '#description' => $this
        ->t('To add custom styles create directory and file "modules/popup_message/popup_message_styles/custom_style/popup.css" and set in this file custom CSS code.'),
    ];
    $form['popup_message_fieldset']['visibility']['path'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Pages'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#group' => 'visibility',
      '#weight' => 0,
    ];
    $options = [
      $this
        ->t('All pages except those listed'),
      $this
        ->t('Only the listed pages'),
    ];
    $description = $this
      ->t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", [
      '%blog' => 'blog',
      '%blog-wildcard' => 'blog/*',
      '%front' => '<front>',
    ]);
    $title = $this
      ->t('Pages');
    $form['popup_message_fieldset']['visibility']['path']['popup_message_visibility'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Show block on specific pages'),
      '#options' => $options,
      '#default_value' => $config
        ->get('visibility') ? $config
        ->get('visibility') : 0,
    ];
    $form['popup_message_fieldset']['visibility']['path']['popup_message_visibility_pages'] = [
      '#type' => 'textarea',
      '#default_value' => $config
        ->get('visibility_pages') ? $config
        ->get('visibility_pages') : '',
      '#description' => $description,
      '#title' => '<span class="element-invisible">' . $title . '</span>',
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this
      ->config('popup_message.settings');
    $flush_cache = $config
      ->get('style') == $form_state
      ->getValue('popup_message_enable') ? FALSE : TRUE;
    $flush_cache_css = $config
      ->get('style') == $form_state
      ->getValue('popup_message_style') ? FALSE : TRUE;
    $flush_cache_js = $config
      ->get('style') == $form_state
      ->getValue('popup_message_check_cookie') ? FALSE : TRUE;
    $text = $form_state
      ->getValue('popup_message_body')['value'];
    $uuids = $this
      ->extractFilesUuid($text);
    $this
      ->recordFileUsage($uuids);
    $config
      ->set('enable', $form_state
      ->getValue('popup_message_enable'))
      ->set('title', $form_state
      ->getValue('popup_message_title'))
      ->set('body', $form_state
      ->getValue('popup_message_body'))
      ->set('height', $form_state
      ->getValue('popup_message_height'))
      ->set('width', $form_state
      ->getValue('popup_message_width'))
      ->set('check_cookie', $form_state
      ->getValue('popup_message_check_cookie'))
      ->set('expire', $form_state
      ->getValue('popup_message_expire'))
      ->set('delay', $form_state
      ->getValue('popup_message_delay'))
      ->set('close_delay', $form_state
      ->getValue('popup_message_close_delay'))
      ->set('cover_opacity', $form_state
      ->getValue('popup_message_cover_opacity'))
      ->set('style', $form_state
      ->getValue('popup_message_style'))
      ->set('visibility', $form_state
      ->getValue('popup_message_visibility'))
      ->set('visibility_pages', $form_state
      ->getValue('popup_message_visibility_pages'))
      ->save();
    if ($flush_cache) {
      drupal_flush_all_caches();
    }
    if ($flush_cache_css) {
      $this->cssOptimizer
        ->deleteAll();
    }
    if ($flush_cache_js) {
      $this->jsOptimizer
        ->deleteAll();
    }
    parent::submitForm($form, $form_state);
  }

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

  /**
   * Parse an HTML snippet for any linked file with data-entity-uuid attributes.
   *
   * @param string $text
   *   The partial (X)HTML snippet to load. Invalid markup will be corrected on
   *   import.
   *
   * @return array
   *   An array of all found UUIDs.
   */
  protected function extractFilesUuid($text) {
    $dom = Html::load($text);
    $xpath = new \DOMXPath($dom);
    $uuids = [];
    foreach ($xpath
      ->query('//*[@data-entity-type="file" and @data-entity-uuid]') as $file) {
      $uuids[] = $file
        ->getAttribute('data-entity-uuid');
    }
    return $uuids;
  }

  /**
   * Records file usage of files referenced by formatted text fields.
   *
   * Every referenced file that does not yet have the FILE_STATUS_PERMANENT
   * state, will be given that state.
   *
   * @param array $uuids
   *   An array of file entity UUIDs.
   */
  protected function recordFileUsage(array $uuids) {
    try {
      foreach ($uuids as $uuid) {
        if ($file = $this->entityRepository
          ->loadEntityByUuid('file', $uuid)) {
          if ($file->status !== FILE_STATUS_PERMANENT) {
            $file->status = FILE_STATUS_PERMANENT;
            $file
              ->save();
          }
        }
      }
    } catch (EntityStorageException $exception) {
      $this
        ->logger('popup_message')
        ->warning($exception
        ->getMessage());
    }
  }

}

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.
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.
PopupMessageSettingsForm::$cssOptimizer protected property CssCollectionOptimizer service.
PopupMessageSettingsForm::$entityRepository protected property Drupal\Core\Entity\EntityRepositoryInterface service.
PopupMessageSettingsForm::$jsOptimizer protected property JsCollectionOptimizer service.
PopupMessageSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
PopupMessageSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
PopupMessageSettingsForm::extractFilesUuid protected function Parse an HTML snippet for any linked file with data-entity-uuid attributes.
PopupMessageSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
PopupMessageSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
PopupMessageSettingsForm::recordFileUsage protected function Records file usage of files referenced by formatted text fields.
PopupMessageSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
PopupMessageSettingsForm::__construct public function PopupMessageSettingsForm constructor. 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.