Short story

Leaflet *does* reconoze entities in ordinary views, but *not* in Search API views.

Original report

We found an alternative solution for this problem.

Problem / Situation

In Views Leaflet Formatter Settings we are used to select a field or < node entity > for the popup.

However, using a Search Index based view < node entity > does not exist in current 1.0-beta3 release.

And the rendered entity from the index is unusable as field.

Proposed Solution

Support < node entity > for Search API based views.

Alternative Solution

Apply the patch from Allow rendering an entity viewmode when using fields in views (currently #20 is working fine, but does not have tests yet)

Add a field Rendered Item to your view, select it for the Leaflet popup.

Comments

hanness created an issue. See original summary.

hanness’s picture

Issue summary: View changes
hanness’s picture

Issue summary: View changes
geek-merlin’s picture

Title: Leaflet Popup does not support < node entity > in Search API View » Leaflet breaks with Search API Views
Category: Feature request » Bug report
Issue summary: View changes
Status: Active » Needs review

To make this better reviewable, i port a part of the patch from #2995576: [Performance!] Implement Ajax-lazyloaded Markers to here.

Patch flying in.

geek-merlin’s picture

geek-merlin’s picture

+++ b/leaflet_views/src/Plugin/views/style/LeafletMap.php
@@ -349,10 +350,20 @@ class LeafletMap extends StylePluginBase implements ContainerFactoryPluginInterf
-          // Render the entity with the selected view mode.
-          if ($this->options['description_field'] === '#rendered_entity' && isset($result->_entity)) {
-
+          if (!empty($result->_entity)) {
+            // Entity API provides a plain entity object.
             $entity = $result->_entity;
+          }
+          elseif (isset($result->_object)) {
+            // Search API provides a TypedData EntityAdapter.
+            $entityAdapter = $result->_object;
+            if ($entityAdapter instanceof EntityAdapter) {
+              $entity = $entityAdapter->getValue();
+            }
+          }
+
+          // Render the entity with the selected view mode.
+          if ($this->options['description_field'] === '#rendered_entity' && isset($entity)) {
 

Note that this simple patch only adds a new code path for search api style rows, so that it soes not interfere with the legacy code path and we get low regression risk.

geek-merlin’s picture

Status: Needs review » Needs work

Looks like some code ran away and we need to recognize entity type in ::init().

geek-merlin’s picture

Status: Needs work » Needs review
StatusFileSize
new2.73 KB

With some fixes.

geek-merlin’s picture

Tested and works like a charm.

itamair’s picture

thanks ... @axel.rutz, will review this asap.
Just a little further feedback.
We had a quite similar issue on the Geofield Map module, that was solved (is seems) with this simpler patch:
https://www.drupal.org/project/geofield_map/issues/2994026#comment-12736192

Your patch looks more solid, BUT wouldn't be enough just this check here too?

$entity = $result->_entity ?: $result->_object->getValue();

I am minding to replicate this solution from you also in the Geofield Map module otherwise ...

geek-merlin’s picture

Good point. I had chosen to be more verbose here for reasons...:
* I'm being a bit paranoid: if someone else adds an _object to that array (we have so many modules...) this does not break.
* On an IDE like PHPStorm (use it, man! Proprietary but free or FROSS maintainers), with a typehint like this we get free introspection (it checks if that method is called with correct parameters, click on the call and see the method, ...), which is good DX.

(PS: phpstorm als lets you "rename this variable / method" without omissions ;-)

itamair’s picture

Goood. Well noted. Going to commit all this asap.
PS: Sure PhpStorm all life (so far ... ;-))

itamair’s picture

StatusFileSize
new2.36 KB
new1.89 KB

@ axel.rutz your patch, but slightly improved with better php/drupal cs variable naming and use of already injected $this->moduleHandler service.
Please confirm that all this is ready for commit ...

geek-merlin’s picture

Status: Needs review » Reviewed & tested by the community

Yes that makes nicer code! Tested this together with #2995576-7: [Performance!] Implement Ajax-lazyloaded Markers and still works as expected.

  • itamair committed 3b08f00 on 8.x-1.x authored by axel.rutz
    Issue #2973818 by axel.rutz, itamair, hanness: Leaflet breaks with...
itamair’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.