Forum
Custom map fieldtype adding page resources multiple times
Hi,
I've copied your original map field and changed it a little bit to add a few extra options, like outputting the "addr" field only (and not the map itself) all within a template. Your docs for a custom fieldtype says that the add_page_resources()
function is only included once per page, but the google maps api script is being included twice in the admin panel, which I think is causing the assets JS to malfunction as I don't seem to be able to upload any assets when I have my field type active in the template. I'm using Perch Runway.
I'm looking at the PerchFieldTypes.class.php file and it seems to only add classes to the $_seen array if they are already included and so the add_page_resources()
function gets called twice for my custom fieldtype.
You can see the full code for the PerchFieldType_gmap.class.php here - https://pastebin.com/5jFyxNeu
My template is here: https://pastebin.com/pcDWvTjd
My diagnostic report here: https://pastebin.com/enZrCFmN
Are you sure it's getting called twice? I can't reproduce that.
Could you try turning on debug and adding this to your
add_page_resources()
method?You should then see a big blue bar in the debug every time it gets called.
I'm sure it's getting called twice, yes. Your snippet confirmed:
Also I'm getting google errors in the console:
Do you get any insight if you hover over those?
gmap.class.php: PerchFieldType_gmap->add_page_resources():
is the only insight.It's odd that you can't reproduce. Is the PerchfieldTypes::get method behaving as expected, only adding a class to the $_seen array after it's been included? That's where the problem seems to be, or at least where I've narrowed it down to possibly occurring!
Yes, that's the only way the
add_page_resources()
method gets invoked.Yes absolutely. I guess I would expect to see the class added to the $_seen array after you call
add_page_resources()
inside theelse{}
clause. See below.I've actually just created a super basic test fieldtype and it's adding
TESTING_CONTENT
to the footer twice here too!<perch:content type="test" id="test" />
Inside perch/addons/fieldtypes/test/test.class.php
It might be worth adding that I'm using this template inside a collection?
I think you're probably spot on there - we should be adding to
$_seen
when the class is first included.Add your expected
self::$_seen[] = $classname;
and I'll do the same here for the next update.Ok, great. Will Do.
Thanks Drew