1

I'm trying to create a map that shows user profiles and other geolocated content (for example, news and events). A view doesn't allow to mix users and other content. I know I can show content that is referenced from a user, but how can I accomplish this without references?

The users and the content share the same taxonomy, though.

edit:

I have tried rovr138 suggestion to no avail, I always get the error (below image) as soon as the both views share the same display name that i have set here:

enter image description here

Error: Class 'Views' not found in combine_views_views_pre_render() (row 24 in /var/www/html/myproject/web/modules/custom/combine_views/combine_views.module)

Here's the code i adapted minimally:

use \Drupal\views\ViewExecutable;

function combine_views_views_pre_render(\Drupal\views\ViewExecutable $view) {
if($view->id() == 'page_test' && $view->current_display == 'map') {

  $args = []; //these are contextual filters on the view
  foreach ($view->argument as $argument_key => $argument_value) {
    if(is_string($argument_value->argument)) {
      $args[] = $argument_value->argument;
    }
    else {
      $args[] = $argument_value->argument[0];
    }
  }

  // Get user view
  $user_view = Views::getView('event_test');
  if (is_object($user_view)) {
    $user_view->setArguments($args);
    $user_view->setDisplay('map');
    $user_view->preExecute();
    $user_view->execute();
  }

  if (!empty($user_view->result)) {
    $view->result = array_merge($user_view->result, $view->result);
  }
}
}
  • 1
    Does the taxonomy term hold the location address? – No Sssweat Apr 17 at 15:53
  • I see where you're going, but unfortunately no. It's just a taxonomy for topics. The users have their address field (geocoded) as the events have. – Volker Apr 17 at 16:00
  • @NoSssweat This is probably a common case of misconception. I never thought, that this would be required one day when creating this website. I should have made the user and the events/news addresses Entity References to an independent content type. I'm trying now how to export some hundred addresses and import them to a new field that is referenced as a stand-alone geolocation CT. If someone else has a hint, be welcome! – Volker Apr 17 at 17:47
  • First, do a small-sized test to see if you can get the new setup working as you want. Once you know that it works, then Export to csv then import & create tax terms with Feeds – No Sssweat Apr 18 at 4:08
  • Without use Drupal\views\Views; PHP doesn't know which Views class you are trying to use. That's the reason for the error message. Using another class from the same namespace doesn't make PHP find every class in the same namespace. – kiamlaluno Apr 22 at 10:15
3
+250

Alternative,

Create your 2 views. Let's say they're called, user_points and node_points and both have a display called map.

You could then create a simple module that does,

use \Drupal\views\ViewExecutable;
use \Drupal\views\Views;

function hook_views_pre_render(ViewExecutable $view) {
  if($view->id() == 'node_points' && $view->current_display == 'map') {

    $args = []; //these are contextual filters on the view
    foreach ($view->argument as $argument_key => $argument_value) {
      if(is_string($argument_value->argument)) {
        $args[] = $argument_value->argument;
      }
      else {
        $args[] = $argument_value->argument[0];
      }
    }

    // Get user view
    $user_view = Views::getView('user_points');
    if (is_object($user_view)) {
      $user_view->setArguments($args);
      $user_view->setDisplay('map');
      $user_view->preExecute();
      $user_view->execute();
    }

    if (!empty($user_view->result)) {
      $view->result = array_merge($user_view->result, $view->result);
    }
  }
}

On your node_points view, you'll now have the results from user_points.

| improve this answer | |
  • Thanks rovr138, i have tried this, but it gives me this error: Error: Class 'Views' not found in combine_views_views_pre_render() (row 37 in /myproject/web/modules/custom/combine_views/combine_views.module). Probably something's missing? Do i have to set a namespace or route? – Volker Apr 21 at 18:15
  • @No Ssssweat Not sure if that helped, as there's another thing i have found out: The error still occurs as soon as i give the views the same machine name ("map"). I don't know how to proceed. I have tested this with really simple views, to no avail. I'm really a noob when it comes to custom modules, so there may be another source for problems. – Volker Apr 22 at 9:14
  • 2
    @Volker oh yes, another use was missing, fixed. – No Sssweat Apr 22 at 13:09
  • @NoSssweat, first thanks a lot of beeing so patient with me! I have added the other use. But the error remains. Cleared caches, double checked the code, tested with simple views (users, pages). Error: Error: Class 'Views' not found in combine_views_views_pre_render() (row 25 in /myproject/web/modules/custom/combine_views/combine_views.module). – Volker Apr 22 at 14:25
  • 2
    My bad, I need coffee, try again now. – No Sssweat Apr 22 at 14:34
1

Here's something that I would do, but I'm not sure if it's the best way to do it.

  • Create different views for the various entity types
    • Render these views as a JSON feeds
  • Create a custom block which will simply include some JS code
    • Preferably render a DIV with a unique ID inside your block
    • Now, in the JS code, get the ID of unique DIV and render the map yourself.

Personally, I'd choose this solution so that views rendering doesn't get in my way. However, depending on your needs, this approach might be complex (if you want filters, etc).

Some alternate solutions: * Get views to render one of the maps (or an empty map) and add the data points yourself with your JSON feeds.

| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.