Problem/Motivation
When a site is hosted out of a sub-directory. For example, if the root of the site is at example.com/siteroot. This causes the links set in the sort widget to duplicate the base path. So the links set in the sort widget would point to /siteroot/siteroot/search?sort=created&order=desc for example. I think this is due to the way these URLs are being set in WidgetForm.php.
public function submitForm(array &$form, FormStateInterface $form_state) {
$links = $form_state->get('links');
[$key, $order] = explode('|', $form_state->getValue('sort_by'));
foreach ($links['#items'] as $link) {
$name = $link['#sort_field'];
if ($name == $key) {
$url = $link['#url'];
$url_info = parse_url($url);
parse_str($url_info['query'], $query);
$query['order'] = $order;
$url_info['query'] = UrlHelper::buildQuery($query);
$url = $url_info['path'] . '?' . $url_info['query'];
$form_state->setRedirectUrl(Url::fromUserInput($url));
}
}
}
The URLs fetched from $form_state contain the base path. Url::fromUserInput does not seem to expect the base path and since the URL passed here doesn't match a Drupal route it appends it the path which already contains the base path to the base path, resulting in the duplication.
Steps to reproduce
Host a site out of a subdirectory. So it's base path would be something like example.com/siteroot.
Enable the search_api_sorts_widget.
Set up a search page and specify 1 or more sorts.
Set the sort widget settings to be: Active, Autosubmit, Hide submit button.
Click on an option displayed in the sort widget.
The page that will load will have a duplicated base path.
Proposed resolution
Check the URL being passed to Url::fromUserInput for the presence of the base path and remove it if it exists.
Or
Use some other method to set URLs that doesn't include the base path.
Remaining tasks
User interface changes
API changes
Data model changes
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | 3267604-base-path-duplicated.patch | 621 bytes | mrdrewkeller |
Issue fork search_api_sorts_widget-3267604
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
mrdrewkeller commentedHere's a patch that is one method of resolving this.EDIT: this should not contain setAbsolute. Disregard this patch and use #3.
Comment #3
mrdrewkeller commentedPrevious patch was not correct. This is the corrected version
Comment #8
peri22 commentedhttps://git.drupalcode.org/project/search_api_sorts_widget/-/merge_reque... strips the base path before passing the path back to Url::fromUserInput(), and adds a unit test: with a base path of '/siteroot' the redirect is 'base:siteroot/search' before the fix and 'base:search' after it. Sites in the document root are unaffected.
Comment #9
peri22 commentedComment #11
peri22 commented