Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Meta search: open external links in new window #6968

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-DEV.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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