-1

Here is my use case:

1-I select few nodes from a view with VBO

2-I run a custom action on those nodes

3-As a result, this action create an array of Terms ($resultedTerms)

My goal, after that, is to redirect the user to a custom form.

This custom form will use $resultedTerms to create a lot of widgets.

How can I achieve this? How can I pass $resultedTerms to the buildForm function of my custom form?

Note that I do not want to save the original selected nodes in the vbo view...

Here is the code using the comment of Taggart Jensen:

class MyAction extends ActionBase
{
    private $_terms;

    public function executeMultiple(array $nodes)
    {
        $this->_terms = [];
        parent::executeMultiple($nodes);
        $resultedTerms = [
            'terms' => $this->_terms
        ];
        // Get the current request to re-use some stuff.
        $request = \Drupal::request();
        $redirect_request = Request::create(
            '/admin/terms_form/',
            'POST', // this also can be GET
            $resultedTerms,
            $request->cookies->all(),
            [],
            $request->server->all()
            );
        if (is_object($request)) {
            $response = \Drupal::service('http_kernel')->handle($redirect_request, HttpKernelInterface::SUB_REQUEST);
            return $response; // I assume it should be return $this->_terms
        }
    }
    public function execute($node = NULL)
    {
        if ($node) {
            $foundTerm=...//some logic
            $this->_terms[]= $foundTerm;
        }
    }
}

And of course the route:

mymodule.termsform:
  path: '/admin/terms_form/'
  defaults:
    _form: '\Drupal\mymodule\Form\TermsForm'
    _title: 'Update terms'
  requirements:
    _permission: 'administer site configuration'
0

Yep so your going to have to hook into the VOB results or submit function I assume you have already done this. then in your function you can create a Response such as the following:

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;

$resultedTerms = [
  'terms' => [1, 2, 7]
];
// Get the current request to re-use some stuff. 
$request = \Drupal::request();
$redirect_request = Request::create(
  '/the-url-to-your-form',
  'POST', // this also can be GET
  $resultedTerms, 
  $request->cookies->all(),
  [],
  $request->server->all()
);
if (is_object($request)) {
  $response = \Drupal::service('http_kernel')->handle($redirect_request, HttpKernelInterface::SUB_REQUEST);
  return $response;
}

then in your buildForm function:

//Get the post data.
$posted = \Drupal::request()->request->all();
// if using GET try dump($_GET);
if (!empty($posted['terms'])) {
  $form['filed_name']['#default_value'] = $posted['terms'];
}

References: see "static Request create" at https://api.symfony.com/3.4/Symfony/Component/HttpFoundation/Request.html#method___construct

also see use of this in Drupal core: core/modules/comment/src/Controller/CommentController.php

line 128 ish.

and yes obviously one should use proper dependency injection but this should get you started ... also note you will test on the vbo result page not your custom form page.

  • This looks like a nice start, but I don't know where to put the ... return $response; code as $resultedTerms is constructed inside function executeMultiple of ActionBase. I will add my code to be more clear – Baud Mar 19 at 15:36
  • Maybe, my question is too easy... I understood the difficult part: how to send arguments to a route/form. But I don't know how to redirect to this form after the execution of the VBO Action. I feel this question is the easy part... this is why I don't have the answer. But maybe I am wrong. – Baud Mar 22 at 11:13
  • then $url = "/path-to-form?terms=(implode('-',$resultedTerms)" return new RedirectResponse($url) api.drupal.org/api/drupal/… – Taggart Jensen Mar 26 at 0:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.