ContentHubSubscriberController.php in Acquia Content Hub 8
File
acquia_contenthub_subscriber/src/Controller/ContentHubSubscriberController.php
View source
<?php
namespace Drupal\acquia_contenthub_subscriber\Controller;
use Drupal\acquia_contenthub\EntityManager;
use Drupal\Core\Access\CsrfTokenGenerator;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\State\StateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class ContentHubSubscriberController extends ControllerBase {
protected $configFactory;
protected $languageManager;
protected $csrfTokenGenerator;
protected $entityManager;
protected $entityTypeManager;
protected $state;
const CH_VERSION = '1';
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;
}
public static function create(ContainerInterface $container) {
$config_factory = $container
->get('config.factory');
$language_manager = $container
->get('language_manager');
$csrf_token_generator = $container
->get('csrf_token');
$entity_manager = $container
->get('acquia_contenthub.entity_manager');
$entity_type_manager = $container
->get('entity_type.manager');
$state = $container
->get('state');
return new static($config_factory, $language_manager, $csrf_token_generator, $entity_manager, $entity_type_manager, $state);
}
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;
}
public function loadDiscovery() {
$query_string = $this->state
->get('system.css_js_query_string') ?: '0';
$token = $this->csrfTokenGenerator
->get('rest');
$request = Request::createFromGlobals();
$cookie_header = session_name() . '=' . current($request->cookies
->all());
$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;
$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));
$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;
}
}