Skip to content

Commit

Permalink
Meta search: open external links in new window (#6968)
Browse files Browse the repository at this point in the history
  • Loading branch information
yurabakhtin committed Apr 26, 2024
1 parent 60c77b2 commit ed40693
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-DEV.md
Expand Up @@ -13,6 +13,7 @@ HumHub Changelog
- Fix #6962: People filter - Hide follower options if Following is disabled in the User module
- Fix #6961: Fix people dropdown filter without defined keys
- Fix #6967: Use same order for meta searching that is used for content page searching by default
- Enh #6968: Meta search: open external links in new window

1.16.0-beta.2 (April 9, 2024)
-----------------------------
Expand Down
25 changes: 25 additions & 0 deletions protected/humhub/services/MetaSearchService.php
Expand Up @@ -81,6 +81,31 @@ public function getUrl(): string
return Url::to($params);
}

/**
* Get a link target depending on URL,
* external URLs should be opened in a new window
*
* @param string|null $url
* @return string|null
*/
public function getLinkTarget(?string $url = null): ?string
{
if ($url === null) {
$url = $this->getUrl();
}

if (!is_string($url) || Url::isRelative($url)) {
return null;
}

$url = parse_url($url);
if (!isset($url['host']) || $url['host'] !== Yii::$app->request->hostName) {
return '_blank';
}

return null;
}

/**
* Check if a searching has been done
*
Expand Down
9 changes: 7 additions & 2 deletions protected/humhub/widgets/views/metaSearchProvider.php
Expand Up @@ -23,13 +23,17 @@
<?php if ($provider->getService()->isSearched()) : ?>
<?php if ($provider->getService()->hasResults()) : ?>
<?php foreach ($provider->getService()->getResults() as $record) : ?>
<a href="<?= $record->getUrl() ?>" class="search-provider-record">
<?= Html::beginTag('a', [
'href' => $record->getUrl(),
'class' => 'search-provider-record',
'target' => $provider->getService()->getLinkTarget($record->getUrl()),
]) ?>
<span class="search-provider-record-image"><?= $record->getImage() ?></span>
<span class="search-provider-record-text">
<span><?= $record->getTitle() ?></span>
<span><?= $record->getDescription() ?></span>
</span>
</a>
<?= Html::endTag('a') ?>
<?php endforeach; ?>
<?php else : ?>
<div class="search-provider-no-results"><?= Yii::t('base', 'No results') ?></div>
Expand All @@ -38,6 +42,7 @@
<?= Button::defaultType($provider->getAllResultsText())
->link($provider->getService()->getUrl())
->cssClass('search-provider-show-all')
->options(['target' => $provider->getService()->getLinkTarget()])
->loader(false) ?>
</div>
<?php endif; ?>
Expand Down

0 comments on commit ed40693

Please sign in to comment.