Insert Entity Reference nid

Created on 10 February 2020, over 4 years ago
Updated 20 February 2023, over 1 year ago

Here is a solution to insert the template and a referenced entity on a new node the same time.

With a link in a view on current (parent) node, prepopulates a new node with the template plus reference:

Example:

node/add/press?template=press_template&edit[field_reference][widget][0][target_id]={{ nid }}

Oh ..

Issue is when you select another template on the new node, you lose the reference field.

When trying to insert the referenced node into the node form template, I have tried:

field_reference: [node:field_reference:entity:nid]

but gives ‘error’. So the reference has to stay in the url, not the template.

To possibly solve this, I need to remove the dropdown menu for my users not to lose the referenced node. Keep the ‘add content’ link solely on the original node with a view.

Would I do that via css, or perhaps another solution?

Most appreciated :)

🌱 Plan
Status

Fixed

Version

1.0

Component

Documentation

Created by

🇫🇷France Liliplanet Cannes

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Hi,

    I need exactly the same - is there any way to keep id and fix that :
    "Issue is when you select another template on the new node, you lose the reference field."

    I don't want to lose reference field. My question is - why this template lose references if they are not in templates it should not change it. SO to keep references would be the best, any clue ?

  • 🇳🇱Netherlands PatrickScheffer

    A template applies to the complete node form. If you select another template, the whole form refreshes and gets filled with values from the selected template. If a field is not in the template, it stays empty. So if you want an entity reference to be pre filled in multiple templates, all of those templates should have that field configured.

  • Hi Patrick,

    Thank you in my case i cannot prepopulate entity reference in template because it is project id and i dont know from which project user will raise it.

    However i found workaround. To hide template select with css and I have added javascript select list with action to open direct template and populate project id from link.

    I had to use additional 3 modules and its combinations but it works now as I need.

  • 🇳🇱Netherlands PatrickScheffer

    Glad to hear you found a solution. Can I close this issue?

  • If it is not possible to select template without change the link then yes you can.

    Still not sure why when you select a template and page is refreshed the wole link is lost.

  • Status changed to Needs review over 1 year ago
  • 🇳🇱Netherlands PatrickScheffer

    I think I finally understand the problem. When selecting a template, it creates a new URL to redirect to. This URL is the current URL with the template parameter added to it, it ignores other parameters. I pushed an update to the 8.x-1.x branch which includes the existing parameters in the redirect URL. Can you test it?

  • YES, YES ,YES - now it works!

    Perfect, you have made my day.

    I am working views to create link as follow:

    /node/add/template_test?project={{ raw_arguments.nid }}&destination=/node/{{ raw_arguments.nid }}/edit?display=project_edit_role

    and once I am on page > contextual filter will create link as :

    https://mysite.com/node/add/template_test?project=22981&destination=/ibm...

    So now I am able to change template and only this part changes "&template=my_template_name" and my project ID will stay the same so I can also add destination when I hit save - it will forward me to destination as needed.

    Thank you 1000 times! this is much much easier then my workaround with additional modules, css and javascripts!

    Man, you cannot even image how power this module now is!

  • Status changed to Fixed over 1 year ago
  • 🇳🇱Netherlands PatrickScheffer

    Thank you for your nice words, I'm glad it helps you. :)

    A better GUI would be nice, but that would mean a complete overhaul of the config storage. Maybe somewhere in the future. ;)

    I'm closing this issue.

  • Status changed to Needs review over 1 year ago
  • Hello Patrick

    I am not sure if it is related to this fix, but it seems that the current dev version does not work since last update. After the cache was refreshed it is no longer possible to create a new node where the template is used.

    I am getting following error. However If I use older version 8.x-1.3 this issue is not there.

    Error: Call to a member function getType() on null in node_form_templates_form_node_form_alter() (line 183 of /home/turpin/htdocs/drupal-9.1.0-ibm/modules/node_form_templates/node_form_templates.module)
    
    Error: Call to a member function getType() on null in node_form_templates_form_node_form_alter() (line 183 of /modules/node_form_templates/node_form_templates.module)
    
    #0 /core/lib/Drupal/Core/Extension/ModuleHandler.php(562): node_form_templates_form_node_form_alter()
    #1 /core/lib/Drupal/Core/Form/FormBuilder.php(835): Drupal\Core\Extension\ModuleHandler->alter()
    #2 /modules/autosave_form/src/Form/AutosaveFormBuilder.php(249): Drupal\Core\Form\FormBuilder->prepareForm()
    #3 /core/lib/Drupal/Core/Form/FormBuilder.php(279): Drupal\autosave_form\Form\AutosaveFormBuilder->prepareForm()
    #4 /modules/autosave_form/src/Form/AutosaveFormBuilder.php(97): Drupal\Core\Form\FormBuilder->buildForm()
    #5 /core/lib/Drupal/Core/Controller/FormController.php(73): Drupal\autosave_form\Form\AutosaveFormBuilder->buildForm()
    #6 [internal function]: Drupal\Core\Controller\FormController->getContentResult()
    #7 /core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array()
    #8 /core/lib/Drupal/Core/Render/Renderer.php(564): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
    #9 /core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext()
    #10 /core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext()
    #11 /vendor/symfony/http-kernel/HttpKernel.php(159): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
    #12 /vendor/symfony/http-kernel/HttpKernel.php(81): Symfony\Component\HttpKernel\HttpKernel->handleRaw()
    #13 /core/lib/Drupal/Core/StackMiddleware/Session.php(58): Symfony\Component\HttpKernel\HttpKernel->handle()
    #14 /core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle()
    #15 /core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle()
    #16 /core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass()
    #17 /core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle()
    #18 /core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle()
    #19 /vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle()
    #20 /core/lib/Drupal/Core/DrupalKernel.php(709): Stack\StackedHttpKernel->handle()
    #21 /index.php(19): Drupal\Core\DrupalKernel->handle()
    #22 {main}
    

    Are you experiencing the same issue ?

  • Status changed to Fixed over 1 year ago
  • My Bad, I had configured in template field which I have already deleted yesterday.
    When I removed it from the template config, it works fine.

  • Status changed to Fixed over 1 year ago
Production build 0.69.0 2024