You are here

public function ModalPageService::importModalConfigToEntity in Modal 5.0.x

Same name and namespace in other branches
  1. 4.0.x src/Service/ModalPageService.php \Drupal\modal_page\Service\ModalPageService::importModalConfigToEntity()
  2. 4.1.x src/Service/ModalPageService.php \Drupal\modal_page\Service\ModalPageService::importModalConfigToEntity()

Import Modal Config to Entity.

File

src/Service/ModalPageService.php, line 406

Class

ModalPageService
Modal Page Service Class.

Namespace

Drupal\modal_page\Service

Code

public function importModalConfigToEntity() {
  $language = $this->languageManager
    ->getCurrentLanguage()
    ->getId();
  $config = $this->configFactory
    ->get('modal_page.settings');
  $modals = $config
    ->get('modals');
  $modals_by_parameter = $config
    ->get('modals_by_parameter');
  $allow_tags = $this
    ->getAllowTags();
  if (empty($modals) && empty($modals_by_parameter)) {
    return FALSE;
  }
  if (!empty($modals)) {
    $modals_settings = explode(PHP_EOL, $modals);
    foreach ($modals_settings as $modal_settings) {
      $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], $allow_tags);
      $title = trim($title);
      $text = Xss::filter($modal[2], $allow_tags);
      $text = trim($text);
      $button = Xss::filter($modal[3]);
      $button = trim($button);
      $uuid = $this->uuidService
        ->generate();
      $modal = [
        'uuid' => $uuid,
        'title' => $title,
        'body' => $text,
        'type' => 'page',
        'pages' => $path,
        'ok_label_button' => $button,
        'langcode' => $language,
        'created' => time(),
        'changed' => time(),
      ];
      $query = $this->database
        ->insert('modal');
      $query
        ->fields($modal);
      $query
        ->execute();
    }
  }
  if (!empty($modals_by_parameter)) {
    $modals_settings = explode(PHP_EOL, $modals_by_parameter);
    foreach ($modals_settings as $modal_settings) {
      $modal = explode('|', $modal_settings);
      $parameter_settings = Xss::filter($modal[0]);
      $parameter = trim($parameter_settings);
      $parameter_data = explode('=', $parameter);
      $parameter_value = $parameter_data[1];
      $title = Xss::filter($modal[1], $allow_tags);
      $title = trim($title);
      $text = Xss::filter($modal[2], $allow_tags);
      $text = trim($text);
      $button = Xss::filter($modal[3]);
      $button = trim($button);
      $uuid = $this->uuidService
        ->generate();
      $modal = [
        'uuid' => $uuid,
        'title' => $title,
        'body' => $text,
        'type' => 'parameter',
        'parameters' => $parameter_value,
        'ok_label_button' => $button,
        'langcode' => $language,
        'created' => time(),
        'changed' => time(),
      ];
      $query = $this->database
        ->insert('modal');
      $query
        ->fields($modal);
      $query
        ->execute();
    }
  }
}