1

I created a module for searching movies in different external databases. I use the Drupal node add page to handle the insertion of the movie. Now I need to know a way to send the data from my custom controller to the /node/add/movies_movie page.

In the controller I use the following code to send data to the module page.

if ($system === "IMDB") {
    $database = new ImdbDatabase;
    $movie = $database->getDataById($id);

    $tempstore = \Drupal::service('user.private_tempstore')->get('movies');
    $tempstore->set("newMovie", $movie);
}

$option = [
    'guid' => $guid,
];

$url = Url::fromUri('internal:/node/add/movies_movie');

return $this->redirect(
    $url->getRouteName(),
    $url->getRouteParameters(),
    $url->getOptions(),
    302
);

My hook gets the value using the private tempstore service:

function movies_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id)
{
    if($form_id === 'node_movies_movie_form')
    {
        //$movie = null;

        $storage = $form_state->getStorage();

        $tempstore = \Drupal::service('user.private_tempstore')->get('movies');
        $movie = $tempstore->get('newMovie');

I use hook_form_alter()to place the objects inside the add form. How can I send data from my controller to hook_form_alter() to place the custom values on the form?

To better understand my problem I will explain my situation in normal English without any technical terms.

The site contains a page to search to movies using a search bar. It will show the result of all the movies in a table with an add button after each movie. When pressing on a add button it will get all the data from the movie and will automaticly place the received movie data, in the movie insertion form. On the movie insertion form can be user change all the values and save it.

  • Form alter let's you alter an existing form before it is rendered. You can't pipe data into it directly, but you can load data from within the alter hook. – Kevin Mar 4 '17 at 19:08
  • Ok but how can I load the data in a safe way. I already tried using the tempStore service, but when opening multiple tabs it will not work correctly? – JimmyD Mar 4 '17 at 19:12
  • How are you loading the data in hook_form_alter()? Use the same method in your controller. – Jaypan Mar 4 '17 at 23:37
  • Added the source of my hook in my question. But in my setup it will not work when sending multiple calls on the same time to the server. – JimmyD Mar 5 '17 at 7:17
  • In other words I need a way to send data from my controller to my hook and that way is request independent (not using sessions or tempstore). – JimmyD Mar 5 '17 at 7:23
3

I think you should not load the data in your controller but in your hook_form_alter. Redirect the user to a path containing a query parameter in Url, so that the url may look like "http://www.yoursite.com/node/add/movies_movie?guid=123" and change the guid paramter to the value of the movie.

Then in your hook_form_alter:

$request = \Drupal::request();
$guid = $request->query->get('guid');

if ($system === "IMDB" && $guid) {
    $database = new ImdbDatabase;
    $movie = $database->getDataById($guid);

    // fill in the values of the movie into the form fields
}

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.