0

I am trying to filter user ip on the template.theme file on Drupal 8 and the intention was to filter on user ip and based on the ip add in the correct arguments into the facets that is created in views page.

Is that possible in views? I can't find any reference to it.

Here is what I found:

function mtheme_preprocess_page(&$variables) {
    $languagecode = \Drupal::languageManager()->getCurrentLanguage()->getId();
    $client = @$_SERVER['HTTP_CLIENT_IP'];
    $ip = $client;
    $ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));
    $ip_data->geoplugin_countryCode;
    if(isset($ip_data->geoplugin_countryCode)) {
        if($ip_data->geoplugin_countryCode == "CH") {
            $location = "suisse";
            $link = '?f[0]=localisation:3';
        } else if($ip_data->geoplugin_countryCode == "FR") {
            $location = "france";
            $link = '?f[0]=localisation:4';
        } else {
            $location = "international";
            $link = '?f[0]=localisation:5';
        }
    }
    $redirect = $link;
    $response = new Symfony\Component\HttpFoundation\RedirectResponse($redirect);
    print "<div class=\"lang-check\" style=\"display:none;\">".$languagecode."</div>";
    print "<div class=\"loc-check\" style=\"display:none;\">".$location."</div>";
    print "<div class=\"loc-link\" style=\"display:none;\">".$link."</div>";
}

function mtheme_preprocess_views_view(&$variables) {
    $view = $variables['view'];
    $id = $view->storage->id();
    $variables['css_name'] = Html::cleanCssIdentifier($id);
    $variables['id'] = $id;
    $variables['display_id'] = $view->current_display;
}

Currently I have not managed to find out how to add in the arguments for the facets but I managed to get it working by setting in the preprocess page and add in some hack (div elements and let JavaScript do the work but it's not clean to load the page 1st then reload with arguments. Here is the JS for it:

function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}

$(document).ready(function(){
    var url = window.location.href;
    var arr = url.split('?');
    console.log(arr[1]);
    if($('body').hasClass('page-pro')) {
        if (!arr[1]) {
            if ($(".loc-check").text().length > 0) {
                window.location.href = '/projets'+$(".loc-link").html();
            }         
        }
    }
});

Sorry if it's not efficient, the question is am I able to work directly on preprocess_views and just add in the preload arguments based on user ip rather than this hack?

Any references?

  • Did you try just adding the User as a contextual argument? – Kevin Dec 12 '18 at 16:00
  • @Kevin I tried adding the user as the contextual argument but we need to check on the ip he is located.. and user argument is not sufficient for this case – Boby Dec 17 '18 at 4:20
  • Why not just create a Views plugin for this? – Kevin Dec 17 '18 at 4:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.