Skip to content
Snippets Groups Projects
Commit 26571486 authored by Jens Beltofte's avatar Jens Beltofte Committed by bartvig
Browse files

Issue #3366208 by beltofte: Switch from domain to clear in the latest...

parent dfba9738
No related branches found
No related tags found
1 merge request!15Issue #3366208 by beltofte: Switch from domain to clear in the latest...
......@@ -21,6 +21,11 @@
this.method = 'domain';
this.common();
},
clear: function () {
this.method = 'clear';
var _si = window._si || [];
_si.push([this.method, function() { }, drupalSettings.siteimprove.token]);
},
recheck: function () {
this.url = drupalSettings.siteimprove.recheck.url;
this.method = 'recheck';
......@@ -148,13 +153,20 @@
}
}
// If exist domain, call input Siteimprove method.
// If exist domain, call domain Siteimprove method.
if (typeof settings.siteimprove.domain !== 'undefined') {
if (settings.siteimprove.domain.auto) {
siteimprove.domain();
}
}
// If exist clear, call clear Siteimprove method.
if (typeof settings.siteimprove.clear !== 'undefined') {
if (settings.siteimprove.clear.auto) {
siteimprove.clear();
}
}
// If exist recrawl, call input Siteimprove method.
if (typeof settings.siteimprove.recrawl !== 'undefined') {
if (settings.siteimprove.recrawl.auto) {
......
......@@ -238,7 +238,7 @@ function siteimprove_page_attachments(array &$attachments) {
$attachments['#attached']['library'][] = $siteimprove->getSiteimproveLibrary();
// If node pages or taxonomy term pages, add input method, else domain
// method.
// or clear method depending on the overlay version.
$enabled_route_names = [
'entity.node.canonical',
'entity.node.latest_version',
......@@ -253,7 +253,7 @@ function siteimprove_page_attachments(array &$attachments) {
$method = 'input';
}
else {
$method = 'domain';
$method = $use_latest_experience ? 'clear' : 'domain';
}
$type = '';
......@@ -282,12 +282,18 @@ function siteimprove_page_attachments(array &$attachments) {
/** @var \Drupal\Core\Entity\ContentEntityType $type */
$entity_type = $param->getEntityType()->id();
$id = $param->id();
$entity = \drupal::entityTypeManager()->getStorage($entity_type)->load($id);
$urls = \drupal::service('siteimprove.utils')->getEntityUrls($entity);
$entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($id);
$urls = \Drupal::service('siteimprove.utils')->getUrls($entity);
$attachments['#attached']['drupalSettings']['siteimprove'][$method] = $siteimprove->getSiteimproveSettings($urls, $method);
}
}
}
// Fallback to non-entity route
if (empty($attachments['#attached']['drupalSettings']['siteimprove'][$method])) {
$urls = \Drupal::service('siteimprove.utils')->getUrls();
$attachments['#attached']['drupalSettings']['siteimprove'][$method] = $siteimprove->getSiteimproveSettings($urls, $method);
}
}
// If siteimprove_url exists in SESSION, send to Siteimprove.
......
......@@ -32,12 +32,47 @@ class DomainAccess extends SiteimproveDomainBase implements SiteimproveDomainInt
return $form;
}
/**
* Get the base url from a full url string.
*
* @param string $url
* A string containing a full url.
*
* @return string
*/
private function getBaseUrl(string $url) {
$url_parts = parse_url($url);
$base_url = $url_parts['scheme'] . '://' . $url_parts['host'];
if ($url_parts['port'] != 80 && $url_parts['port'] != 443) {
$base_url .= ':' . $url_parts['port'];
}
return $base_url;
}
/**
* {@inheritdoc}
*/
public function getUrls(EntityInterface $entity) {
$domain = \Drupal::service('domain_access.manager');
return $domain->getContentUrls($entity);
public function getUrls(?EntityInterface $entity = NULL) {
$base_urls = [];
if ($entity) {
$domain_access = \Drupal::service('domain_access.manager');
$urls = $domain_access->getContentUrls($entity);
foreach ($urls as $url) {
$base_urls[] = $this->getBaseUrl($url);
}
}
if (empty($base_urls)) {
$domain_negotiator = \Drupal::service('domain.negotiator');
$active_hostname = $domain_negotiator->negotiateActiveHostname();
$base_urls = is_array($active_hostname) ? $active_hostname : [$active_hostname];
}
return $base_urls;
}
}
......@@ -71,7 +71,7 @@ class Simple extends SiteimproveDomainBase {
* @return array
* Array of domains.
*/
public function getUrls(EntityInterface $entity) {
public function getUrls(?EntityInterface $entity = NULL) {
$domain = $this->request->getScheme() . '://' . $this->request->getHttpHost();
return [$domain];
}
......
......@@ -88,7 +88,7 @@ class Single extends SiteimproveDomainBase {
/**
* {@inheritdoc}
*/
public function getUrls(EntityInterface $entity) {
public function getUrls(?EntityInterface $entity = NULL) {
$config = $this->config('siteimprove.domain.single.settings');
$domain = $config->get('domain');
$scheme = preg_match('/^https?:\/\//', $domain) ? '' : $this->request->getScheme() . '://';
......
......@@ -87,7 +87,10 @@ abstract class SiteimproveDomainBase extends PluginBase implements ContainerFact
public function submitForm(array &$form, FormStateInterface $form_state) {}
/**
* Return urls for active domains for this entity.
* Return urls for active domains.
*
* If an entity is included as argument will that entity be used when finding
* the active domain / URL.
*
* If http/https isn't specified in domain name, use the backend's scheme.
*
......@@ -97,7 +100,7 @@ abstract class SiteimproveDomainBase extends PluginBase implements ContainerFact
* @return array
* Array of domain names without trailing slash.
*/
public function getUrls(EntityInterface $entity) {
public function getUrls(?EntityInterface $entity = NULL) {
return [];
}
......
......@@ -273,6 +273,34 @@ class SiteimproveUtils {
}
/**
* Return frontend urls for the current route.
*
* @param \Drupal\Core\Entity\EntityInterface|null $entity
* Entity to get frontend urls for.
*
* @return array
* Returns an array of frontend urls.
*/
public function getUrls(?EntityInterface $entity = NULL) {
$urls = [];
if (is_object($entity) && $entity->hasLinkTemplate('canonical')) {
$urls = $this->getEntityUrls($entity);
}
else {
$domains = $this->getEntityDomains();
$current_url = Url::fromRoute('<current>');
// Create urls for active frontend domains.
foreach ($domains as $domain) {
$urls[] = $domain . $current_url->toString();
}
}
return $urls;
}
/**
* Get active domain names for entity.
*
......@@ -282,7 +310,7 @@ class SiteimproveUtils {
* @return array
* Array of domain names without trailing slash.
*/
public function getEntityDomains(EntityInterface $entity) {
public function getEntityDomains(?EntityInterface $entity = NULL) {
// Get the active frontend domain plugin.
$config = $this->configFactory->get('siteimprove.settings');
/** @var \Drupal\siteimprove\Plugin\SiteimproveDomainBase $plugin */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment