2

I have a site on Drupal 8 and I update a block view with the following code :

(function ($, Drupal) {

  'use strict';

  setInterval(function() {
    $('.region-navigation-logo .view-display-id-block_3').trigger('RefreshView');
  }, 10000);

})(jQuery, Drupal);

This code updates the logo of the site. The problem is that if I scroll the page down, it goes up a little automatically every 10 seconds.

You can test on my site with :

https://www.s1biose.com/fr/user/login

identifier : demo

password : demo

Then click on the logo of the site, scroll down the home page and wait 10 seconds. You will see the page go up some pixels every 10 seconds.

UPDATE

The following code works, but it is applied to all views of the site.

I want to apply it only to 2 views :

message_activity_stream_timeline_public

and

message_activity_stream_timeline_private

How to do this ?

function message_activity_stream_ajax_render_alter(array &$data) {
  $view_name = '<view_name>';
  $view_dom_id = '<view_dom_id>';
  $selector = '.js-view-dom-id-' . $view_dom_id;

  foreach ($data as $key => $value) {
    if ($value['command'] === 'viewsScrollTop' && $value['selector'] === $selector) {
      unset ($data[$key]);
      break;
    }
  }
}

How to use this code with 2 views ?

enter image description here

mar72
  • 29
  • 2
  • would you please add 'RefreshView' function codes to your question? – Yuseferi Jul 28 '19 at 12:50
  • @YusefMohamadi I do not understand, there is already the code JS in the question – mar72 Jul 28 '19 at 13:20
  • Hi, your colleague @tropcool already had an [answer](https://stackoverflow.com/a/56941129/2529954) for the exact same issue, and has already created a [duplicate](https://stackoverflow.com/questions/57110023/why-does-my-page-go-back-a-bit-to-each-update) .. Take the time to read the linked answer : `$selector` is precisely used to filter which view should be fixed. Come on you are able to figure out what should be done next. If not I suggest you go on the original post and add a comment, or ask more info on the chat if you need more info, because the answer will remain the same. – EricLavault Jul 29 '19 at 11:11
  • @EricLavault I updated my question. How to fill in the code ? – mar72 Jul 29 '19 at 11:48
  • @EricLavault I do not understand. On this view https://www.s1biose.com/fr/article the page does not go back. The problem is only on the homepage. – mar72 Jul 29 '19 at 11:51
  • Possible duplicate of [How to update my View in Drupal 8 without scrolling back to the top?](https://stackoverflow.com/questions/56914120/how-to-update-my-view-in-drupal-8-without-scrolling-back-to-the-top) – MofX Jul 29 '19 at 11:51
  • This is explained in the answer (copy/pasted) : *If you need to match the `view_dom_id` with the `view_name`, you can search for the `settings` command in the $data array, eg. `$cmd['settings']['views']['ajaxViews']` has a structure that looks like the following, note the array key precisely being built up from the views_dom_id*. – EricLavault Jul 29 '19 at 11:54
  • "*On this view s1biose.com/fr/article the page does not go back*". Yes because this view is not refreshed (not concerned by the trigger' selector). But if you go to that page and open your browser console, after scrolling down a bit, if you run `jQuery('.view-id-accueil_article').trigger('RefreshView');` you will get back to the top. Actually this is normal, in your code the 'RefreshView' event is triggered only for the 2 view filtered by this jQuery selector : `'.region-navigation-logo .view-display-id-block_3'` – EricLavault Jul 29 '19 at 12:00
  • @EricLavault This is the logo of the site that I refresh, on all pages. So I do not understand why the problem appears on the homepage. – mar72 Jul 29 '19 at 12:17
  • Well, the homepage is part of the set of *all* pages, so why not ? Every time you trigger 'refreshView', "viewsScrollTop" will be triggered as well unless you explicitly remove the command from the ajax response data. – EricLavault Jul 29 '19 at 12:34
  • @EricLavault Is there any other method without using `RefreshView` to refresh a block without scrolling up or back to the first page ? – mar72 Jul 29 '19 at 12:52
  • If you are comfortable with Views module and AJAX, you can try to manually reload the view through an ajax request, then replace the view content by the fresh one. – EricLavault Jul 29 '19 at 13:22

1 Answers1

0

You can get help of the following code to run the code only for your views

Drupal.behaviors.blockRefresh = {
    attach: function (context, settings) {
      jQuery.each(Drupal.views.instances, function (i, view) {
        var selector = '.js-view-dom-id-' + view.settings.view_dom_id;
        if (view.settings.view_name == "message_activity_stream_timeline_public" || view.settings.view_name == "message_activity_stream_timeline_private
" ) {
         setInterval(function() {
              $('.region-navigation-logo .view-display-id-block_3').trigger('RefreshView'); }, 10000);
             }
            }
      });
    }
  }

I haven't test it but it is a sample to help you how to restrict your code just for the names of your view.

Yuseferi
  • 7,931
  • 11
  • 67
  • 103
  • Thank you, this code must be placed in the JS file ? if so, do I have to delete the code in the .module file ? – mar72 Jul 28 '19 at 14:37
  • @mar72 no, this is the js code. you need to update your js. – Yuseferi Jul 29 '19 at 13:36
  • I tested your code but it does not work and no javascript is loaded https://pastebin.com/kUbQDRRP – mar72 Jul 29 '19 at 20:05
  • There are errors in your JS code. I changed it but it does not work. This code disables "go back" for all the site and not only the home pagehttps://pastebin.com/C93wX0xA and https://pastebin.com/pL8Gksk7 – mar72 Jul 29 '19 at 20:24