Problem/Motivation

If a view uses a parameter in the path (e.g. node/%/report), it is impossible to get a redirect to redirect to a URL that uses that parameter (e.g. node/%). This is true for both custom redirects and redirecting to a display.

Proposed resolution

  • For custom redirect we should expose the view arguments as tokens
  • For display redirect we should include the route parameters

Comments

andrewbelcher created an issue. See original summary.

cosolom’s picture

Status: Active » Needs review
StatusFileSize
new1.13 KB

Try this patch.

tobiberlin’s picture

The patch works well when display redirect is set. What is missing is the possibility to use the views argument within the custom redirect. The main probem is that there is no token defined to get the argument. I just solved this by defining a custom token - maybe this would be something which should be solved in views module?!


/**
 * Implements hook_token_info().
 */
function my_module_token_info() {
  $tokens_view['argument_1'] = [
    'name' => 'The first argument of the view'
  ];

  return [
    'tokens' => ['view' => $tokens_view],
  ];
}

/**
 * Implements hook_tokens().
 */
function my_module_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {
  $replacements = [];
  if ($type == 'view' && !empty($data['view'])) {
    /** @var ViewExecutable $view */
    $view = $data['view'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'argument_1':
          $args = $view->args;
          if (!empty($args)) {
            $replacements[$original] = $args[0];
          }

          break;
      }
    }
  }
  return $replacements;
}

In this way you can define a custom redirect path like "/node/[view:argument_1]"

alianov’s picture

StatusFileSize
new1.56 KB

adding to #2,
an instance of EntityInterface as a url parameter (%group), fails in UrlGenerator.
Might be a bug in core.

alianov’s picture

jeremyr’s picture

Status: Needs review » Reviewed & tested by the community

Patch in #4 works great for me. Specifically needed the %group parameter in my case.

manish-31’s picture

Patch #4 works perfectly for me. Thanks @alianov

svendecabooter’s picture

I can confirm patch #4 fixes this issue for me as well.

golddragon007’s picture

Status: Reviewed & tested by the community » Needs work

Indeed #4 is working, but it does not fulfil the whole issue.

"For custom redirect we should expose the view arguments as tokens" implementation missing.

I do see that the original code supports tokens, but it seems that does not allow to use of arguments of the current URL.

Like:
node/%
node/%node

jweowu’s picture

Cross-referencing with #3337622: Batch data export triggers MissingMandatoryParametersException with contextual argument in URL in case followers of this will also understand how to resolve that.

sanju_bera’s picture

Try the patch for implementation of "For custom redirect we should expose the view arguments as tokens"

Created a view token which will take view path component as a variable argument.

amanp’s picture

I confirm that patch in #4 resolves the MissingMandatoryParametersException for display redirects with Drupal 10.2 / views_data_export:1.4

yousefanbar’s picture

I confirm that patch in #4 works great for me with Drupal 9.5.11 / views_data_export:1.4

aaronbauman’s picture

Status: Needs work » Needs review
StatusFileSize
new833 bytes

Maybe I'm missing something, but this patch works for me.

Very simply: we already have everything we need to generate the route from the view.

abdulaziz zaid’s picture

thanks @aaronbauman

if I use Custom redirect path:

the patch is not working.

if I use Redirect to this display:

your patch is working with and without checked 'Include query string parameters on redirect'

you should pass the arg if the 'Include query string parameters on redirect' is checked.

mediabounds’s picture

The patch in #14 works for me.

Previous comments requested being able to use path arguments as tokens if a custom redirect path is specified, but that is already possible (though not obvious).

For example, if you had a redirect path of /users/%uid/submissions/export and wanted to redirect to /user/%uid/export-complete, you could specify the following in Custom redirect path:
/users/[current-page:url:args:value:1]/export-complete

steven jones’s picture

Status: Needs review » Needs work

Thanks for the patch in #14 we should get some tests in for this and make sure it works and continues to work!

chetananemade’s picture

Hi,
The patch in #11 worked well for us with Drupal 10.2 and Views_data_export 1.4.
However, after upgrading to Drupal 10.3 and Views_data_export 1.5, we have updated the patch in #11 to make it compatible with the new versions.

cp19112000’s picture

Temporary solutions without errors or warnings (Work for Drupal 10.3.11, views_data_export 8.x-1.5)

    View commerce_promotion_coupons (page_1) config
  • - Path: promotion/%/coupons
  • - Add a contextual filter raw value from URL


    In view commerce_promotion_coupons (data_export) config
  • - Path: promotion/%/export-coupons
  • - Redirect to this display: page_1
  • - Add a contextual filter raw value from URL



Then add this hook

function hook_views_pre_view(ViewExecutable $view, $display_id, array &$args) {
  if ($view->id() == 'commerce_promotion_coupons' && $display_id == 'data_export') {
    $display = $view->display_handler;
    $redirect_path = $display->getOption('redirect_path');
    $display->setOption('redirect_to_display', FALSE);
    $display->setOption('custom_redirect_path', TRUE);
    $display->setOption('redirect_path', str_replace('{commerce_promotion}', $args[0] ?? 'all', $redirect_path));
  }
}
aaronbauman’s picture

Status: Needs work » Closed (duplicate)

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

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

Maintainers, please credit people who helped resolve this issue.