following the work done in this my previous issue: https://www.drupal.org/node/2745595 (now closed),
I went forward refactoring ALL the Leaflet_views submodule plug-ins to better comply to the Drupal 8 coding best practice regarding Dependency Injection, Service Container, and Unit Testability (https://www.drupal.org/node/2133171).

In synthesis the following has been done:

- removed direct calls to static functions/methods, and more correctly implemented dependency_injection of services withe the service container and the "create" method;
- removed deprecated classes with the preferred ones and methods (mainly from EntityManager to its new EntityTypeManager and relevant other sub services ...);
- re-titled the plugin to "Leaflet map (classic)" (sounded me better than the actual Leaflet map (old));
- corrected the plugin id to "leaflet_map";
- fixed several mini adjustments on the coding standards, mainly on the correct commenting standards side (my code sniffer in phpstorm is very strict on this).

Patch attached about all the this.
Hope this helps and feel free to reject the undesired amendments ...

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

itamair created an issue. See original summary.

itamair’s picture

Title: Leaflet_Views module refactored with D8 Dependency Injection (better coding standards) » Leaflet_Views module refactored with D8 Dependency Injection (better coding practice & standards)
szeidler’s picture

Status: Patch (to be ported) » Needs review
FileSize
26.36 KB

Thanks itamair, great effort.

For me that patch didn't applied cleanly, so that I needed to reroll it for the current HEAD. I wasn't able to figure out, what was the problem with your patch in detail.

Hunk #5 FAILED at 163.
1 out of 13 hunks FAILED -- saving rejects to file leaflet_views/src/Plugin/views/row/LeafletMarker.php.rej

I can confirm, that the patch is working. It's really a nice step to move the modules code quality forward.

Through the change of the plugin id from leafet_map to leaflet_map we need a proper handling for existing sites to work with the plugin name change. Otherwise all of these maps need to be reconfigured. When applying the patch on an existing installation you will get a

exception 'Drupal\Component\Plugin\Exception\PluginNotFoundException' with message 'The "leafet_map" plugin does  [error]
not exist.' in
itamair’s picture

@szeidler that error you mention (regarding the plugin name change) is a proper matter of D8 configuration settings. It arises from the original views configuration yaml files, that will keep looking for the (original) "leafet_map" views plugin style.

To fix them you will need to export/copy (admin/config/development/configuration/single/export) each views config yaml file and re-import it (admin/config/development/configuration/full/import), with the following lines corrected:

............ (omissis)
      style:
        type: leaflet_map
............ (omissis)
pvhee’s picture

Status: Needs review » Needs work

itamair, could you reroll the patch against the current head? Agree with your observations and this will clean up the Leaflet code which was ported rather fast to Drupal 8.

itamair’s picture

new update patch on this rerolled on the current head. This should be tested. Hope will be reviewed in time not to make this old again ;-)

itamair’s picture

Status: Needs work » Needs review
pvhee’s picture

Status: Needs review » Needs work

This looks good and working well in my leaflet sandbox site:

A couple of comments though.

You seem to revert some of the work that went into #273657: No events are shown in year view or calendar month block (you changed number fields back to textfields)

Can we rewrite this more concise?

$entity = $this->entityManager->getStorage($this->entityType)->load($result->nid);
            $view_builder = $this->entityManager->getViewBuilder($entity->getEntityTypeId());
            $build = $view_builder->view($entity, $this->options['view_mode'], $entity->language());

e.g. to

$entity = $this->entityManager->getStorage($this->entityType)->load($result->nid);
$build = $this->entityManager->getViewBuilder($entity->getEntityTypeId())->view($entity, $this->options['view_mode'], $entity->language());
pvhee’s picture

FYI, I've set up a mirror repository of Leaflet at https://github.com/marzeelabs/leaflet -- this should make it easier to create a PR and review changes.

pvhee’s picture

Another note: we might need to provide an upgrade path in order to avoid the following error

exception 'Drupal\Component\Plugin\Exception\PluginNotFoundException' with message 'The "leafet_map" plugin does not exist.' in                                        [error]
.../web/core/lib/Drupal/Component/Plugin/Discovery/DiscoveryTrait.php:52
Stack trace:
itamair’s picture

@pvhee tnx for your comments and suggestions.

Here is the updated patch for with you suggested more concise code.

Couple of things:
- I don't get your first note at the #8 comment: You seem to revert some of the work that went into #273657: No events are shown in year view or calendar month block (you changed number fields back to textfields).
It seems to be a 2008 D5 issue, regarding the Calendar module. Isn't it?
- right, we might need to provide an upgrade path in order to avoid the error regarding 'The "leafet_map" plugin does not exist.'. But how?
Might be just a copy class of the leaflet_views/src/Plugin/views/style/LeafletMap.php one, just with an annotation like the following ... ?

* @ViewsStyle(
* id = "leafet_map",
* title = @Translation("Leaflet map (uncorrect one)"),
* help = @Translation("Displays a View as a Leaflet map with a buggy id. Should be changed to the Leaflet map (classic) one ."),
* display_types = {"normal"},
* theme = "leaflet-map"
* )

itamair’s picture

Status: Needs work » Needs review
pvhee’s picture

Status: Needs review » Needs work

Apologies, I meant to refer to issue #2736573: Number validation in formatter forms not working

I'd suggest removing the refactoring of the leafet to leaflet change and do this in a separate issue where we can discuss upgrade path (if we really want to provide that).

Other than that this looks good so we can commit this back after these fixes.

itamair’s picture

@pvhee ok I fixed it (everything):

- re- included the code for the issue #2736573: Number validation in formatter forms not working, plus some further minor fixer on code refactoring and Drupal Standards
- I provided a (so called) upgrade path (that might be thought as temporary, but perfectly working, imho) in the form of a further LeafetMap Class View Style Plugin (id= leafet_map) that just extends the correct LaefletMap Class (id=leaflet_map), with any additional code (it works exactly as the id=leaflet_map one). In this way every user set with the (UN-CORRECT) leafet_map plugin id won't have any application error but just an advised message in the view backend that invites him to shift to the new correct and ANALOG Leaflet map (classic) one.

Hope this finally helps. Feel free to amend all this the way you prefer, and commit this to the head ...

itamair’s picture

Status: Needs work » Needs review
itamair’s picture

A further improvement and a better "upgrade path" might be

  1. - to extend the hook_requirements (the existing one should be moved in the .install file instead ...) that:
    • - check if there is an existing view (throughout the configuration management check via config.factory service) that uses the (bad) leafet_map id view display style;
    • - print a message to the user to invite him to run an application update (drush updb / update.php) to fix a bad leaflet views plugin id use.
  2. add an hook_update (in the same .install file) that runs a views configuration change that moves every leafet_map id view display style to the correct one (leaflet_map), for the affected leaflet views.

Let me know if this upgrade path might be worth to be done (I just started to draft the hook_requirements update ...)

pvhee’s picture

Status: Needs review » Needs work

Thanks for the new patch, but not sure about the upgrade path, and I would like people to test this first in their current installs.

If we want to get the rest merged in earlier, we need to remove the leafet->leaflet change and handle this in a separate issue as I suggested in #13, can this be done?

itamair’s picture

Ok, sure ... I will update this patch on this, by the we ;-) (and open a new issue on the leafet->leaflet change)

itamair’s picture

Here is a new rerolled patch, specifically regarding refactoring with dependency injections (more complaints with Drupal best practices and testability),
and skipping the matter regarding the error on the leafet_map view style plugin.

Once committed this to the head/actual dev of the project, I will open a new issue addressing a proper path to correct the leafet_map -> leaflet_map plugin name.
Just let us know when this will be committed/merged.

itamair’s picture

Status: Needs work » Needs review

  • pvhee committed dce98f4 on 8.x-1.x authored by itamair
    Issue #2745851 by itamair, szeidler, pvhee: Leaflet_Views module...
pvhee’s picture

Status: Needs review » Fixed

Thanks itamair, I've reviewed this, removed some slashes from the namespace use statements that weren't needed, and committed this!

Status: Fixed » Closed (fixed)

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