You are here

class ModalPage in Modal 8

Same name and namespace in other branches
  1. 8.3 src/ModalPage.php \Drupal\modal_page\ModalPage
  2. 8.2 src/ModalPage.php \Drupal\modal_page\ModalPage

Modal Page Class.

Hierarchy

Expanded class hierarchy of ModalPage

1 string reference to 'ModalPage'
modal_page.services.yml in ./modal_page.services.yml
modal_page.services.yml
1 service uses ModalPage
modal_page.modals in ./modal_page.services.yml
Drupal\modal_page\ModalPage

File

src/ModalPage.php, line 13

Namespace

Drupal\modal_page
View source
class ModalPage {

  /**
   * Path Matcher.
   *
   * @var \Drupal\Core\Path\PathMatcherInterface
   */
  protected $pathMatcher;

  /**
   * The current request.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * {@inheritdoc}
   */
  public function __construct(ConfigFactory $config_factory, RequestStack $request_stack, PathMatcherInterface $path_matcher) {
    $this->pathMatcher = $path_matcher;
    $this->request = $request_stack
      ->getCurrentRequest();
    $this->configFactory = $config_factory;
  }

  /**
   * Function to check Modal will show.
   */
  public function checkModalToShow() {
    $modal = FALSE;
    $config = $this->configFactory
      ->get('modal_page.settings');
    $modals_by_page = $config
      ->get('modals');
    $modals_by_parameter = $config
      ->get('modals_by_parameter');
    if (empty($modals_by_page) && empty($modals_by_parameter)) {
      return FALSE;
    }
    if ($modals_by_page) {
      $modal = $this
        ->getModalByPage($modals_by_page);
    }
    if (empty($modal) && $modals_by_parameter) {
      $modal = $this
        ->getModalByParameter($modals_by_parameter);
    }
    return $modal;
  }

  /**
   * Function to get Modal by Page.
   */
  public function getModalByPage($modals) {
    $modals_settings = explode(PHP_EOL, $modals);
    foreach ($modals_settings as $id => $modal_settings) {
      $id = 'ModalByPage.' . $id;
      $modal = explode('|', $modal_settings);
      $path = $modal[0];
      if ($path != '<front>') {
        $path = Xss::filter($modal[0]);
      }
      $path = trim($path);
      $path = ltrim($path, '/');
      $title = Xss::filter($modal[1]);
      $title = trim($title);
      $text = Xss::filter($modal[2]);
      $text = trim($text);
      $button = Xss::filter($modal[3]);
      $button = trim($button);
      $label_do_not_show_again = FALSE;
      if (isset($modal[4]) && !empty($modal[4])) {
        $label_do_not_show_again = Xss::filter($modal[4]);
        $label_do_not_show_again = trim($label_do_not_show_again);
      }
      $is_front_page = $this->pathMatcher
        ->isFrontPage();
      if ($is_front_page) {
        $current_path = '<front>';
      }
      else {
        $current_uri = $this->request
          ->getRequestUri();
        $current_path = ltrim($current_uri, '/');
      }
      if ($path == $current_path) {
        $modal = [
          'id' => $id,
          'title' => $title,
          'text' => $text,
          'button' => $button,
          'do_not_show_again' => $label_do_not_show_again,
        ];
        return $modal;
      }
    }
  }

  /**
   * Function to get Modal by Parameter.
   */
  public function getModalByParameter($modals) {
    $modals_settings = explode(PHP_EOL, $modals);
    $parameters = $this->request->query
      ->all();
    foreach ($modals_settings as $id => $modal_settings) {
      $id = 'ModalByParameter.' . $id;
      $modal = explode('|', $modal_settings);
      $parameter_settings = Xss::filter($modal[0]);
      $parameter = trim($parameter_settings);
      $title = Xss::filter($modal[1]);
      $title = trim($title);
      $text = Xss::filter($modal[2]);
      $text = trim($text);
      $button = Xss::filter($modal[3]);
      $button = trim($button);
      $label_do_not_show_again = FALSE;
      if (isset($modal[4]) && !empty($modal[4])) {
        $label_do_not_show_again = Xss::filter($modal[4]);
        $label_do_not_show_again = trim($label_do_not_show_again);
      }
      $parameter_data = explode('=', $parameter);
      $parameter_key = $parameter_data[0];
      $parameter_value = $parameter_data[1];
      if (!empty($parameters[$parameter_key]) && $parameters[$parameter_key] == $parameter_value) {
        $modal = [
          'id' => $id,
          'title' => $title,
          'text' => $text,
          'button' => $button,
          'do_not_show_again' => $label_do_not_show_again,
        ];
        return $modal;
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ModalPage::$configFactory protected property The config factory.
ModalPage::$pathMatcher protected property Path Matcher.
ModalPage::$request protected property The current request.
ModalPage::checkModalToShow public function Function to check Modal will show.
ModalPage::getModalByPage public function Function to get Modal by Page.
ModalPage::getModalByParameter public function Function to get Modal by Parameter.
ModalPage::__construct public function