DiscoveryInterfaceController.php in Acquia Content Hub 8.2
File
modules/acquia_contenthub_curation/src/Controller/DiscoveryInterfaceController.php
View source
<?php
namespace Drupal\acquia_contenthub_curation\Controller;
use Drupal\acquia_contenthub\Client\ClientFactory;
use Drupal\Core\Access\CsrfTokenGenerator;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleExtensionList;
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 DiscoveryInterfaceController extends ControllerBase {
protected $configFactory;
protected $languageManager;
protected $csrfTokenGenerator;
protected $entityTypeManager;
protected $bundleInfoManager;
protected $state;
protected $clientFactory;
protected $moduleList;
public function __construct(ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager, CsrfTokenGenerator $csrf_token_generator, EntityTypeBundleInfoInterface $bundle_info_manager, EntityTypeManagerInterface $entity_type_manager, StateInterface $state, ClientFactory $client_factory, ModuleExtensionList $module_list) {
$this->configFactory = $config_factory;
$this->languageManager = $language_manager;
$this->csrfTokenGenerator = $csrf_token_generator;
$this->bundleInfoManager = $bundle_info_manager;
$this->entityTypeManager = $entity_type_manager;
$this->state = $state;
$this->clientFactory = $client_factory;
$this->moduleList = $module_list;
}
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');
$bundle_info_manager = $container
->get('entity_type.bundle.info');
$entity_type_manager = $container
->get('entity_type.manager');
$state = $container
->get('state');
$client_factory = $container
->get('acquia_contenthub.client.factory');
$module_list = $container
->get('extension.list.module');
return new static($config_factory, $language_manager, $csrf_token_generator, $bundle_info_manager, $entity_type_manager, $state, $client_factory, $module_list);
}
public function getSupportedEntityTypesAndBundles() {
$entity_type_bundles = $this->bundleInfoManager
->getAllBundleInfo();
$entity_types_and_bundles = [];
foreach ($entity_type_bundles as $entity_type => $bundles) {
$bundle_key = '';
if ($entity_type === 'taxonomy_term') {
$bundle_key = 'vocabulary';
}
else {
$bundle_key = $this->entityTypeManager
->getDefinition($entity_type)
->getKey('bundle');
}
if ($bundle_key == '') {
continue;
}
$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();
$ember_endpoint = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'acquia_contenthub_curation') . '/ember/index.html' . '?' . $query_string;
$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));
$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);
$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;
}
}