You are here

public function DiscoveryInterfaceController::loadDiscovery in Acquia Content Hub 8.2

Loads the content hub discovery page from an ember app.

1 string reference to 'DiscoveryInterfaceController::loadDiscovery'
acquia_contenthub_curation.routing.yml in modules/acquia_contenthub_curation/acquia_contenthub_curation.routing.yml
modules/acquia_contenthub_curation/acquia_contenthub_curation.routing.yml

File

modules/acquia_contenthub_curation/src/Controller/DiscoveryInterfaceController.php, line 183

Class

DiscoveryInterfaceController
Controller for Content Hub Discovery page.

Namespace

Drupal\acquia_contenthub_curation\Controller

Code

public function loadDiscovery() {

  // A dummy query-string is added to filenames, to gain control over
  // browser-caching. The string changes on every update or full cache
  // flush, forcing browsers to load a new copy of the files, as the
  // URL changed.
  $query_string = $this->state
    ->get('system.css_js_query_string') ?: '0';

  // Get the session token.
  $token = $this->csrfTokenGenerator
    ->get('rest');

  // Get the cookie.
  $request = Request::createFromGlobals();
  $cookie_header = session_name() . '=' . current($request->cookies
    ->all());

  // Obtain the list of supported entity types and bundles.
  $entity_types_bundles = $this
    ->getSupportedEntityTypesAndBundles();
  $ember_endpoint = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'acquia_contenthub_curation') . '/ember/index.html' . '?' . $query_string;

  // Set Client User Agent.
  $module_info = $this->moduleList
    ->getExtensionInfo('acquia_contenthub');
  $module_version = isset($module_info['version']) ? $module_info['version'] : '0.0.0';
  $drupal_version = isset($module_info['core']) ? $module_info['core'] : '0.0.0';
  $client_user_agent = 'AcquiaContentHub/' . $drupal_version . '-' . $module_version;
  $import_endpoint = $GLOBALS['base_url'] . '/acquia-contenthub/';
  $saved_filters_endpoint = '';
  $languages_supported = array_keys($this->languageManager
    ->getLanguages(LanguageInterface::STATE_ALL));

  // We move default language to the top of the array.
  // Refer: CHMS-994.
  $default_language_id = $this->languageManager
    ->getDefaultLanguage()
    ->getId();
  $i = array_search($default_language_id, $languages_supported);
  unset($languages_supported[$i]);
  array_unshift($languages_supported, $default_language_id);

  // Obtain site's timezone.
  $timezone = date('P');
  $settings = $this->clientFactory
    ->getSettings();
  $form = [];
  $form['#attached']['library'][] = 'acquia_contenthub_curation/acquia_contenthub_curation';
  $form['#attached']['drupalSettings']['acquia_contenthub_curation']['host'] = $settings
    ->getUrl();
  $form['#attached']['drupalSettings']['acquia_contenthub_curation']['public_key'] = $settings
    ->getApiKey();
  $form['#attached']['drupalSettings']['acquia_contenthub_curation']['secret_key'] = $settings
    ->getSecretKey();
  $form['#attached']['drupalSettings']['acquia_contenthub_curation']['client'] = $settings
    ->getUuid();
  $form['#attached']['drupalSettings']['acquia_contenthub_curation']['ember_app'] = $ember_endpoint;
  $form['#attached']['drupalSettings']['acquia_contenthub_curation']['source'] = 'drupal8';
  $form["#attached"]['drupalSettings']['acquia_contenthub_curation']['client_user_agent'] = $client_user_agent;
  $form["#attached"]['drupalSettings']['acquia_contenthub_curation']['import_endpoint'] = $import_endpoint;
  $form["#attached"]['drupalSettings']['acquia_contenthub_curation']['saved_filters_endpoint'] = $saved_filters_endpoint;
  $form["#attached"]['drupalSettings']['acquia_contenthub_curation']['token'] = $token;
  $form["#attached"]['drupalSettings']['acquia_contenthub_curation']['cookie'] = $cookie_header;
  $form["#attached"]['drupalSettings']['acquia_contenthub_curation']['timezone'] = $timezone;
  $form["#attached"]['drupalSettings']['acquia_contenthub_curation']['languages_supported_by_subscriber'] = $languages_supported;
  $form["#attached"]['drupalSettings']['acquia_contenthub_curation']['entity_types_bundles_supported_by_subscriber'] = $entity_types_bundles;
  $form["#attached"]['drupalSettings']['acquia_contenthub_curation']['ch_version'] = "2";
  if (empty($settings
    ->getUuid())) {
    $this
      ->messenger()
      ->addWarning($this
      ->t('Acquia Content Hub must be configured to view any content. Please contact your administrator.'));
  }
  elseif (!$ember_endpoint) {
    $this
      ->messenger()
      ->addWarning($this
      ->t('Please configure your ember application by setting up config variable ember_app.'));
  }
  else {
    $form['iframe'] = [
      '#type' => 'markup',
      '#markup' => Markup::create('<iframe id="acquia-contenthub-ember" src=' . $ember_endpoint . ' width="100%" height="1000px" style="border:0"></iframe>'),
    ];
  }
  return $form;
}