0

I have a path in drupal 6, which allow me to add new content with 2 fields completed. For example, path node/add/my_content_type/4/6 means that I am on page /node/add/my_content_type, and first field has value "Philip" and second field has value "Oleg". Those names are in database, because user put them in in another content type.

I try to do the same in Drupal 8, but I cannot put arguments after /mycontent_type/. So right now, on the /node/add/my_content_type I see a form with fields, I can select names in input select, but nothing shows when I put more arguments in url.

I will give an example:

I have a module with students and school trip. On custom content "students", I add students names in text input. Those names are saved in database.

Let's say, that my second content type is "trip". I have a function, which allow me to add a trip, when students names are given. I can simply click add content 'trip' and select names in select fields, but I want to make it more simple and redirect to page /node/add/trip/4/6 where 4 is id of "Philip" and 6 is id of "Oleg". In this way I have a form with to fields already completed. It is posible in drupal 6, but no in drupal 8. Have can i do that?

I think that one of the problem could be the fact, that Drupal 8 require field "Title". So I have to modify url a little bit /node/add/trip/??TITLE??/4/6.

I need exactly this -> https://www.drupal.org/forum/support/module-development-and-code-questions/2020-04-27/webform-field-default-value-from-url but for custom content :)

7
  • 1
    Welcome to Drupal Answers! 👋 Regarding the title field problem there's Automatic Entity Label. Getting the values from a URL query strings should be feasible pretty easily from a hook_form_alter implementation. So please try this: Create a custom module to implement hook_form_alter, learn how to target the desired form, learn how to set default values to existing elements, try to retrieve URL query parameters, and the set them as default values. – leymannx Feb 21 at 10:06
  • 1
    If anything doesn't work on the way, feel free to ask another reproducible, narrowly-scoped and detailed question including what you've tried yourself so far and what exactly didn't work as expected. Many thanks – leymannx Feb 21 at 10:07
  • I cannot retrieve URL parameters. – newbe Feb 21 at 12:00
  • Okay, then please add the code that's not working to the question. And also please read drupal.stackexchange.com/q/232292/15055 and explain what exactly is unclear with the given answers. Many thanks – leymannx Feb 21 at 12:03
  • in which function in module should I implement above code to get url? I am confused. I tried with /Drupal::routeMatch()->getParameters() but it doesn't print any parameters which I was looking for – newbe Feb 21 at 12:26
0

I figured it out.

in .module, hook_form_after()

$value = \Drupal::request()->query->get('q');
$value = Html::escape($value);
$form['student']['widget']['#default_value'] = [0 => $value];

with url /node/add/trip?q=6, where 6 is id of student Oleg

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.