By dmudie on
Hi, I am trying to add a parameter onto a node add form but not having any luck. Example, I'd like to have something like this: node/add/application/333 where 333 is another ID that I can use to control some things on the add form. Can someone steer me in the right direction?
Comments
The documentation on passing
The documentation on passing parameters in routes covers what you desire in detail.
Thanks. I've looked that over
Thanks. I've looked that over and know how to use parameters in routes for my custom pages. However, I'm getting stuck on how to define the controller. I'd like to use the core node add code and just add my wildcard to it so I can use it in form_alter hook. For example, for a path like '/node/add/application/{job_id}'
Thanks and I appreciate your reply!
https://www.drupal.org/docs/8
https://www.drupal.org/docs/8/api/routing-system/introductory-drupal-8-routes-and-controllers-example
In this case, I would
In this case, I would probably do two things:
1) Create a new route for the form
2) Extend NodeForm to expect the new parameter.
example.routing.yml:
Then you build your controller:
Then in your form, you can do the following:
Note that the interface means you must set the default for $someId to NULL, or you will get an error. That is why the sanity check on $someId is included in the form definition.
Also note that if the ID refers to a node, or some other entity, it would be better to upcast it into that entity before passing to the form definition.
Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
Thank you Jaypan - this is
Thank you Jaypan - this is great. I really appreciate your time and explanation. Extending the node form is the piece I was missing.
Getting an error trying to
Getting an error trying to load the form
Error: Call to a member function getEntityTypeId() on null in Drupal\Core\Entity\EntityForm->getBaseFormId()
I would have expected the base form ID would be 'node_form" but do I need to set that somehow?
I think I know what is going
I think I know what is going on here, but when are you getting this error, on form load, or submit?
Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
It's happening on load.
It's happening on load.
NodeForm extends EntityForm,
NodeForm extends EntityForm, which has a method, setEntity(). You need to set the entity on the form. You'll have to track down how they do this in code - do they load the form, then call this method, before rendering the form? I'm not sure, but looking at that error, this would appear to be your issue.
Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
I'm still getting the
I'm still getting the getEntityTypeId() on null error. Have tried setting it in my form class like this
$this->entity = $form_state->getFormObject()->getEntity()
But it doesn't work. Pls how did you guys solve this error?
Did you manage to get it working'
I'm trying to pass some parameters to my node/add/CONTENTTYPE form and implemented this script, but I'm getting the same getEntityTypeId() error. Did you manage to solve it? If so, can you please share the complete script?
Call to a member function getEntityTypeId() on null
I am also getting this error
ReflectionException error
Am trying to add an argument to the path node/add/legals/113, where legals-> machine name of my content type and 113 is the node id. I tried your code but am getting this error,
My Project Code Structure looks like,
legals (path-sites/all/modules/custom/legals)
1. legals.info.yml
2. legals.routing.yml
3. src
3.1 Controller
3.1.1 LegalsController.php
3.2 Form
3.2.1 Legals.php
I have attached the code for each file
1. legals.info.yml:
2. legals.routing.yml:
3.1.1 LegalsController.php:
3.2.1 Legals.php:
Can you please help me with this error?
Thanks Much!
Hi Jaypan,
Hi Jaypan,
Am trying to add an argument to the path node/add/legals/113, where legals-> machine name of my content type and 113 is the node id. I tried your code but am getting this error,
My Project Code Structure looks like,
legals (path-sites/all/modules/custom/legals)
1. legals.info.yml
2. legals.routing.yml
3. src
3.1 Controller
3.1.1 LegalsController.php
3.2 Form
3.2.1 Legals.php
I have attached the code for each file
1. legals.info.yml:
2. legals.routing.yml:
legals.node_form:
3.1.1 LegalsController.php:
3.2.1 Legals.php:
Can you please help me with this error?
Also, regarding your comment on upcasting node ID, how do I do that?
Thanks Much!
Preethi
You've named your file
You've named your file LegalsController, but then you named the class PageController. You'll have to change the class name to LegalController.
Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
Thank you so much.
Can you please tell me what must be the first line of the legals.routing.yml file?
You have mentioned it to be example.node_form:
I tried giving it as legals.legalsNodeFormPage: in my legals.routing.yml file, but it says non existent service. Can you please correct me on this?
Thanks Much!
Preethi
The name of the class must be
The name of the class must be the same as the name of the file.
In your routing file, you've said to look for the class PageController. You named the file LegalsController.php, and then you made the class name PageController. These all need to be the same, so the easiest thing to do is change the name of your file from LegalsController.php to PageController.php.
Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
Hi Jaypan
yeah, i got your point. Thank you so much, that error is gone.
But my class throws a run time exception saying the argument is nullable although am passing an argument to it. This is because on the view where am passing the argument it throws this error,
Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "content_moderation.moderation_information". in Drupal\Component\DependencyInjection\Container->get() (line 153 of core/lib/Drupal/Component/DependencyInjection/Container.php).
I guess there is some error with my legals.routing.yml file.
You have mentioned in your comment like this,
My file is like this,
Is my first line on this file is right? I want to know what is causing this error.
Thanks Much in advance!
Preethi