You are here

class ContentHubSubscriberController in Acquia Content Hub 8

Controller for Content Hub Discovery page.

Hierarchy

Expanded class hierarchy of ContentHubSubscriberController

File

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

Namespace

Drupal\acquia_contenthub_subscriber\Controller
View source
class ContentHubSubscriberController extends ControllerBase {

  /**
   * Config Factory.
   *
   * @var \Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

  /**
   * Language Manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * Csrf Token Generator.
   *
   * @var \Drupal\Core\Access\CsrfTokenGenerator
   */
  protected $csrfTokenGenerator;

  /**
   * The entity manager.
   *
   * @var \Drupal\acquia_contenthub\EntityManager
   */
  protected $entityManager;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The state key/value store.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * The version of content hub module.
   */
  const CH_VERSION = '1';

  /**
   * ContentHubSubscriberController constructor.
   *
   * @param \Drupal\core\Config\ConfigFactoryInterface $config_factory
   *   The client manager.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   * @param \Drupal\Core\Access\CsrfTokenGenerator $csrf_token_generator
   *   The csrf token generator.
   * @param \Drupal\acquia_contenthub\EntityManager $entity_manager
   *   The entity manager.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state key/value store.
   */
  public function __construct(ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager, CsrfTokenGenerator $csrf_token_generator, EntityManager $entity_manager, EntityTypeManagerInterface $entity_type_manager, StateInterface $state) {
    $this->configFactory = $config_factory;
    $this->languageManager = $language_manager;
    $this->csrfTokenGenerator = $csrf_token_generator;
    $this->entityManager = $entity_manager;
    $this->entityTypeManager = $entity_type_manager;
    $this->state = $state;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {

    /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
    $config_factory = $container
      ->get('config.factory');

    /** @var \Drupal\Core\Language\LanguageManagerInterface $language_manager */
    $language_manager = $container
      ->get('language_manager');

    /** @var \Drupal\Core\Access\CsrfTokenGenerator $csrf_token_generator */
    $csrf_token_generator = $container
      ->get('csrf_token');

    /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
    $entity_manager = $container
      ->get('acquia_contenthub.entity_manager');

    /** @var \Drupal\acquia_contenthub\EntityManager $entity_manager */
    $entity_type_manager = $container
      ->get('entity_type.manager');

    /** @var \Drupal\Core\State\StateInterface $state */
    $state = $container
      ->get('state');
    return new static($config_factory, $language_manager, $csrf_token_generator, $entity_manager, $entity_type_manager, $state);
  }

  /**
   * Obtains a list of supported entity types and bundles by this site.
   *
   * This also includes the 'bundle' key field. If the bundle key is empty this
   * means that this entity does not have any bundle information.
   *
   * @return array
   *   An array of entity_types and bundles keyed by entity_type.
   */
  public function getSupportedEntityTypesAndBundles() {
    $entity_types = $this->entityManager
      ->getAllowedEntityTypes();
    $entity_types_and_bundles = [];
    foreach ($entity_types as $entity_type => $bundles) {
      if ($entity_type === 'taxonomy_term') {
        $bundle_key = 'vocabulary';
      }
      else {
        $bundle_key = $this->entityTypeManager
          ->getDefinition($entity_type)
          ->getKey('bundle');
      }
      $entity_types_and_bundles[$entity_type] = [
        'bundle_key' => $bundle_key,
        'bundles' => array_keys($bundles),
      ];
    }
    return $entity_types_and_bundles;
  }

  /**
   * Loads the content hub discovery page from an ember app.
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentHubSubscriberController::$configFactory protected property Config Factory. Overrides ControllerBase::$configFactory
ContentHubSubscriberController::$csrfTokenGenerator protected property Csrf Token Generator.
ContentHubSubscriberController::$entityManager protected property The entity manager. Overrides ControllerBase::$entityManager
ContentHubSubscriberController::$entityTypeManager protected property The entity type manager. Overrides ControllerBase::$entityTypeManager
ContentHubSubscriberController::$languageManager protected property Language Manager. Overrides ControllerBase::$languageManager
ContentHubSubscriberController::$state protected property The state key/value store.
ContentHubSubscriberController::CH_VERSION constant The version of content hub module.
ContentHubSubscriberController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
ContentHubSubscriberController::getSupportedEntityTypesAndBundles public function Obtains a list of supported entity types and bundles by this site.
ContentHubSubscriberController::loadDiscovery public function Loads the content hub discovery page from an ember app.
ContentHubSubscriberController::__construct public function ContentHubSubscriberController constructor.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
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.
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.