Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

Leaflet map to add coordinates to page header

admin first-time form

Solved by Brice Boucard View solution

Started by Brice Boucard 4 years ago · 12 replies · 943 views
4 years ago

Hi,

It's been a long time since I last used Grav and I've just installed the latest version to work on a personal project.

I'm not a developer and I've tried to read the docs but I'm quite lost at the moment...

Here is what I'd like to achieve: the idea is to add coordinates to some contents. For this, I'd like to have a Leaflet map displayed inside a tab and when clicking it, adding the coordinates to the content header.

I suppose I have to develop a plugin rather than a theme for this ? I don't really know where to start...

Thanks in advance :)

4 years ago

@bricebou,

I would start with:

Not sure what you are trying to achieve though...

  • Do you want to display a map in one of the pages of your website?
  • Or do you want to click on a map in Admin and add the coordinates to the header of a page?
    • And what would you then want to do with the coordinates?
4 years ago

Thanks for the answer ! I want to add a map in the Admin, in a tab of the page form, click it to add coordinates to the page header.

4 years ago

More precisely, I'd like to create a new field type I would call when defining a page template.

4 years ago

@bricebou,

but it seems we can’t declare assets within the plugin/blueprints.yaml

Indeed, you cannot add assets inside a blueprint file. Try adding the assets during the 'onAssetsInitialized' event in your custom plugin's PHP file, or inside the Twig template that lays out the new custom field.

last edited 04/12/22 by pamtbaau
4 years ago

Thanks @pamtbaau !

I've managed to create a new form field which is a clickable Leaflet map and to store the clicked coordinates inside the page header 🙂

Thanks :)

4 years ago

@bricebou, That's nice...

Please note, the forum is not just a Q&A for the benefit of the asker. It is also a repo of shared knowledge. I've shared my knowledge with you, would you mind sharing your knowledge (in the form of code) with us form members?

👍 2
last edited 04/12/22 by pamtbaau
4 years ago Solution

Sorry...

I've created a new theme which creates new pages, more precisely this one, extrait.yaml with a call to a new form field :

YAML
extends@: default

form:
  fields:
    tabs:
      type: tabs
      active: 1
      fields:
        content:
          type: tab
          fields:
            header.extrait_coord:
              type: geolocation
              label: Coordonnées

I've created the field in admin/themes/grav/templates/forms/fields/geolocation/geolocation.html.twig :

TWIG
{% do assets.addJs('theme://dist/js/admin.js' , { 'loading':'defer' }) %}

{% extends "forms/field.html.twig" %}

{% block prepend %}
    <div id="leaflet"></div>
{% endblock %}

As documented, we have to declare the admin templates path in our plugin.php:

PHP
public static function getSubscribedEvents(): array
    {
        return [
            'onAdminTwigTemplatePaths' => ['onAdminTwigTemplatePaths', 0]
        ];
    }

    public function onAdminTwigTemplatePaths($event): void
    {
        $paths = $event['paths'];
        $paths[] = __DIR__ . '/admin/themes/grav/templates';
        $event['paths'] = $paths;
    }

The javascript deals with the Leaflet map and with the interactions with it (creation of markers, dragging...)

I think that's all :)

Maybe, il would be better to slice this code, to develop a geolocation plugin, but I'm not comfortable enough with Grav yet to follow this path...

👍 1
4 years ago

@bricebou, Thanks for sharing!

What's the rationale to create file geolocation.html.twig inside /user/plugins/myplugin/admin/themes/grav/templates/forms/fields/geolocation/
instead of /user/plugins/myplugin/templates//forms/fields/geolocation/ ?

If I'm not mistaken, you only have to mimic the path below /templates/.

Setting the template path could then be:

PHP
public function onAdminTwigTemplatePaths($event): void
{
  $paths = $event['paths'];
  $paths[] = __DIR__ . '/templates'; // -or- "plugins://$this->name/templates";
  $event['paths'] = $paths;

  // -or-

  $this->grav['twig']->twig_paths[] = "plugins://$this->name/templates";
}
4 years ago

I tried this and I've just tested this again to be sure: if I do what you suggest, my admin is broken... My form appears but without the admin theme...

Maybe because I do all of what I described through a theme and not a plugin ?

4 years ago

@bricebou, Interesting...

Well, you have tested it and I haven't, so you're probably right ;-)

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 57 15 hours ago
Support · by Anna, 3 days ago
2 65 18 hours ago
Support · by Justin Young, 18 hours ago
1 33 18 hours ago
Support · by Duc , 1 week ago
2 68 5 days ago
Support · by Colin Hume, 1 week ago
2 60 6 days ago