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

Command icon 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

mrdrewkeller created an issue. See original summary.

mrdrewkeller’s picture

Assigned: mrdrewkeller » Unassigned
Status: Active » Needs review
StatusFileSize
new636 bytes

Here's a patch that is one method of resolving this.

EDIT: this should not contain setAbsolute. Disregard this patch and use #3.

mrdrewkeller’s picture

StatusFileSize
new621 bytes

Previous patch was not correct. This is the corrected version

The last submitted patch, 2: 3267604-base-path-duplicated.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Status: Needs review » Needs work

The last submitted patch, 3: 3267604-base-path-duplicated.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

peri22 made their first commit to this issue’s fork.

peri22’s picture

Version: 1.0.0-beta3 » 1.0.0-beta5
Status: Needs work » Needs review

https://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.

peri22’s picture

Version: 1.0.0-beta5 » 1.x-dev

  • peri22 committed d02eb0be on 1.x
    Issue #3267604: Fix duplicated base path in the sort...
    
peri22’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.