Ajax submit: Automatically save values when changed

Created on 11 September 2018, almost 6 years ago
Updated 3 June 2024, 21 days ago

Thanks for the great module. Is there a way to add an ajax call to the element? ie when you change a value it would then save the entity on change without the need for the save button?

πŸ’¬ Support request
Status

Needs work

Component

Code

Created by

πŸ‡ΏπŸ‡¦South Africa hyltonmillar

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.

  • Patch at #41 worked for me but caused issues when the view results use groupping.

    Here is a modified version, it's most likely not the most performant solution but it can be a good starting point.

  • Status changed to Needs review about 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States loze

    This was still not working for me. I was getting Warning: Attempt to read property "_entity" on array in Drupal\views_entity_form_field\Plugin\views\field\EntityFormField::updateEntity() (line 831 of /Users/####/web/modules/contrib/views_entity_form_field/src/Plugin/views/field/EntityFormField.php)" in my logs.

    Here is a patch that got it working for me. I needed to change
    $form['output'][$key]['#rows'][$row]->_entity;
    to
    $form['output'][$key]['#rows'][$row]['#row']->_entity;

  • πŸ‡ΊπŸ‡ΈUnited States loze

    Ignore that last patch. This should work correctly.

  • Hi Loze,
    It does not change it for me - receiving following issue in logs after your patch #55

    Error: Cannot use object of type Drupal\views\ResultRow as array in Drupal\views_entity_form_field\Plugin\views\field\EntityFormField::updateEntity() (line 854 of  modules/views_entity_form_field/src/Plugin/views/field/EntityFormField.php)
    
    #0 [internal function]: Drupal\views_entity_form_field\Plugin\views\field\EntityFormField::updateEntity()
    #1  core/lib/Drupal/Core/Form/FormAjaxResponseBuilder.php(69): call_user_func_array()
    #2  core/lib/Drupal/Core/Form/EventSubscriber/FormAjaxSubscriber.php(109): Drupal\Core\Form\FormAjaxResponseBuilder->buildResponse()
    #3 [internal function]: Drupal\Core\Form\EventSubscriber\FormAjaxSubscriber->onException()
    #4  core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(142): call_user_func()
    #5  vendor/symfony/http-kernel/HttpKernel.php(219): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch()
    #6  vendor/symfony/http-kernel/HttpKernel.php(92): Symfony\Component\HttpKernel\HttpKernel->handleThrowable()
    #7  core/lib/Drupal/Core/StackMiddleware/Session.php(58): Symfony\Component\HttpKernel\HttpKernel->handle()
    #8  core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle()
    #9  core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle()
    #10  core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass()
    #11  core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle()
    #12  core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle()
    #13  vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle()
    #14  core/lib/Drupal/Core/DrupalKernel.php(709): Stack\StackedHttpKernel->handle()
    #15  index.php(19): Drupal\Core\DrupalKernel->handle()
    #16 {main}
    
  • I just tested #55 with boolean field and following error in logs :

    TypeError: Drupal\Core\Render\MainContent\AjaxRenderer::renderResponse(): Argument #1 ($main_content) must be of type array, null given, called in /core/lib/Drupal/Core/Form/FormAjaxResponseBuilder.php on line 89 in Drupal\Core\Render\MainContent\AjaxRenderer->renderResponse() (line 49 of /core/lib/Drupal/Core/Render/MainContent/AjaxRenderer.php)

  • πŸ‡¦πŸ‡ΉAustria JordiK

    Since #41 neither of the later patches (#53 and #55) worked for me. Here is a re-roll of the #41 patch to the latest dev version.
    For me it works without any errors with most of the base fields. The patch also applies cleanly to the beta10.

  • πŸ‡¦πŸ‡ΉAustria JordiK

    Had some weird behavior when the view is sorted/paginated/filtered with AJAX.
    The widget works only on the first page and after a fresh reload. The moment the view is reloaded through AJAX after e.g. column sort with AJAX the functionality breaks.
    Similar to πŸ’¬ Changes not saving properly after filtering/sorting table Active .

    Modified the AJAX attached to the form field by adding the original path as url and the rest of the parameters. Now it works fine for me.
    Adding patch.

  • Hi JordiK,

    After I have applied your patch #59 it seems it does not work as it should. It always "forget" to update some values.

  • πŸ‡¦πŸ‡ΉAustria JordiK

    Did you get this behavior on a clean install? Anything in the logs - warnings, errors? What is your filed configuration?

  • πŸ‡ΉπŸ‡­Thailand AlfTheCat

    I tried the patch from #59 for a simple view of ECK entities, with a few regular fields and only one number field using a form field.
    After this patch, I can change values but they only seem to get saved in the form. The entities themselves don't get updated.

    Two warnings and one error are reported in the log:

    Warning: Undefined array key 231 in Drupal\views_entity_form_field\Plugin\views\field\EntityFormField::updateEntity() (line 788 of /var/www/***/modules/contrib/views_entity_form_field/src/Plugin/views/field/EntityFormField.php)

    Warning: Attempt to read property "_entity" on null in Drupal\views_entity_form_field\Plugin\views\field\EntityFormField::updateEntity() (line 788 of /var/www/***/modules/contrib/views_entity_form_field/src/Plugin/views/field/EntityFormField.php)

    TypeError: Drupal\Core\Render\MainContent\AjaxRenderer::renderResponse(): Argument #1 ($main_content) must be of type array, null given, called in /var/www/***/core/lib/Drupal/Core/Form/FormAjaxResponseBuilder.php on line 89 in Drupal\Core\Render\MainContent\AjaxRenderer->renderResponse() (line 49 of /var/www/***/core/lib/Drupal/Core/Render/MainContent/AjaxRenderer.php)

    Hope this helps!

  • Status changed to Needs work 2 months ago
  • πŸ‡ΊπŸ‡ΈUnited States jimmynash

    Here is a patch based on #58 and #59 that is working for me against beta10.

    It does have some other code for hiding descriptions and titles that is different than the original patches.

    #59 did take care of the problems with ajax submit after the view had been paginated or filtered. But there was some strange
    behavior around the entity field values not all saving as mentioned in #60.

    I added a bit to reload the entity before setting the field values and this seems to take care of that. I'm not sure that it's the most
    performant thing to do though. Take that with a grain of salt.

  • πŸ‡¦πŸ‡ΉAustria maxilein

    Does the patch respect node versioning? i.e. does it create a new version, when a field value gets changed?

  • πŸ‡ΉπŸ‡­Thailand AlfTheCat

    I tried #64 but no luck. Slightly different errors than I was getting before in the log:

    Error: Call to a member function getEntityTypeId() on null in Drupal\views_entity_form_field\Plugin\views\field\EntityFormField::updateEntity() (line 852 of /var/www/***/modules/contrib/views_entity_form_field/src/Plugin/views/field/EntityFormField.php)

    Warning: Trying to access array offset on value of type null in Drupal\views_entity_form_field\Plugin\views\field\EntityFormField::updateEntity() (line 845 of /var/www/***/modules/contrib/views_entity_form_field/src/Plugin/views/field/EntityFormField.php)

    Warning: Attempt to read property "_relationship_entities" on null in Drupal\views_entity_form_field\Plugin\views\field\EntityFormField::updateEntity() (line 845 of /var/www/***/modules/contrib/views_entity_form_field/src/Plugin/views/field/EntityFormField.php)

Production build 0.69.0 2024