You are here

public function ContentHubSubscriberController::loadDiscovery in Acquia Content Hub 8

Loads the content hub discovery page from an ember app.

1 string reference to 'ContentHubSubscriberController::loadDiscovery'
acquia_contenthub_subscriber.routing.yml in acquia_contenthub_subscriber/acquia_contenthub_subscriber.routing.yml
acquia_contenthub_subscriber/acquia_contenthub_subscriber.routing.yml

File

acquia_contenthub_subscriber/src/Controller/ContentHubSubscriberController.php, line 151

Class

ContentHubSubscriberController
Controller for Content Hub Discovery page.

Namespace

Drupal\acquia_contenthub_subscriber\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();
  $config = $this->configFactory
    ->get('acquia_contenthub.admin_settings');
  $ember_endpoint = $config
    ->get('ember_app') ?: $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'acquia_contenthub_subscriber') . '/ember/index.html' . '?' . $query_string;

  // Set Client User Agent.
  $module_info = \Drupal::service('extension.list.module')
    ->getAllInstalledInfo()['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 = $config
    ->get('import_endpoint') ? $config
    ->get('import_endpoint') : $GLOBALS['base_url'] . '/acquia-contenthub/';
  $saved_filters_endpoint = $config
    ->get('saved_filters_endpoint') ? $config
    ->get('saved_filters_endpoint') : $GLOBALS['base_url'] . '/acquia_contenthub/contenthub_filter/';
  $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);
  $form = [];
  $form['#attached']['library'][] = 'acquia_contenthub_subscriber/acquia_contenthub_subscriber';
  $form['#attached']['drupalSettings']['acquia_contenthub_subscriber']['host'] = $config
    ->get('hostname');
  $form['#attached']['drupalSettings']['acquia_contenthub_subscriber']['public_key'] = $config
    ->get('api_key');
  $form['#attached']['drupalSettings']['acquia_contenthub_subscriber']['secret_key'] = $config
    ->get('secret_key');
  $form['#attached']['drupalSettings']['acquia_contenthub_subscriber']['client'] = $config
    ->get('origin');
  $form['#attached']['drupalSettings']['acquia_contenthub_subscriber']['ember_app'] = $ember_endpoint;
  $form['#attached']['drupalSettings']['acquia_contenthub_subscriber']['source'] = $config
    ->get('drupal8');
  $form["#attached"]['drupalSettings']['acquia_contenthub_subscriber']['client_user_agent'] = $client_user_agent;
  $form["#attached"]['drupalSettings']['acquia_contenthub_subscriber']['import_endpoint'] = $import_endpoint;
  $form["#attached"]['drupalSettings']['acquia_contenthub_subscriber']['saved_filters_endpoint'] = $saved_filters_endpoint;
  $form["#attached"]['drupalSettings']['acquia_contenthub_subscriber']['token'] = $token;
  $form["#attached"]['drupalSettings']['acquia_contenthub_subscriber']['cookie'] = $cookie_header;
  $form["#attached"]['drupalSettings']['acquia_contenthub_subscriber']['languages_supported_by_subscriber'] = $languages_supported;
  $form["#attached"]['drupalSettings']['acquia_contenthub_subscriber']['entity_types_bundles_supported_by_subscriber'] = $entity_types_bundles;
  $form["#attached"]['drupalSettings']['acquia_contenthub_subscriber']['ch_version'] = static::CH_VERSION;
  if (empty($config
    ->get('origin'))) {
    $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;
}