-
Posts
812 -
Joined
-
Last visited
Everything posted by a-ok
-
I've got some AJAX filtering going on – when the user selects a 'filter' it'll send an AJAX request to a ajax.php template and fire the following: $filterEndpoint = $this->input->get->filterEndpoint; if ($this->input->get->pageTemplate == 'projects') { $query = $pages->find("template=projects-filters-single, projects_filters_projects.count>0, include=all, sort=sort, {$filterEndpoint}"); $itemTemplate = './inc/item__project--filter.inc'; bd($query); } $output = ''; foreach ($query as $item) { $output .= wireRenderFile($itemTemplate, array('item' => $item)); } return json_encode(['html' => $output]); I have an include to a functions.inc file that has a getRatio() function that is included at the top of every template file. The issue is upon selecting a filter I get a `Call to undefined function getRatio()` error. I have tried to use include_once to the functions.inc file but doesn't seem to make a difference. Any thoughts?
-
Are ready.php hooks/addHookMethods called outside their use?
a-ok replied to a-ok's topic in General Support
Just an update. This is my current (updated) code. The `$shopifyShop` stuff is related to the Shopify API. I have a 'currency select' element which the user can use (initial check). $shopifyShop = $page->shopifyShop(); $shopifyShopCurrency = $shopifyShop['shop']['currency']; if ($this->input->post("region")) { $session->set("shopifyShopCurrency", $this->input->post("region")); } else { if (!$session->get("shopifyShopCurrency")) { $ipapiCurrencyCode = $page->ipapiGetUserLocation()['currency']['code']; if ($ipapiCurrencyCode && $ipapiCurrencyCode !== 'GBP') { $session->set("shopifyShopCurrency", "ROW"); } else { // Set it as the Shopify store default $session->set("shopifyShopCurrency", $shopifyShopCurrency); } } } -
Are ready.php hooks/addHookMethods called outside their use?
a-ok replied to a-ok's topic in General Support
Thanks for the lengthy reply! Yes I guess you're right! Storing it in a $session variable is sort of caching it (so if the user is in session then it won't need to check the ipAPI again) but I get your point about storing it as a variable initially to prevent the double call if not. Totally. I have since removed this after giving it more thought. Yes sorry I am using it throughout the templates – this is just my initial check ? -
Are ready.php hooks/addHookMethods called outside their use?
a-ok replied to a-ok's topic in General Support
Okay thanks. So can we confirm that even though ready.php is called on every page load (back and front) it will only run the hook when told to in my templates? It won't run when someone is on the backend for example? -
Are ready.php hooks/addHookMethods called outside their use?
a-ok replied to a-ok's topic in General Support
It should be, no? If `$session->get(“shopifyShopCurrency”)` doesn’t exist then it stores the ipAPI call in it so it should skip the API call next page visit within the session... -
Are ready.php hooks/addHookMethods called outside their use?
a-ok replied to a-ok's topic in General Support
The hook is included in the head.inc so it’s not ‘template’ bound – it should call on every template. The issue is it shouldn’t call on every ready.php load – it should only call when the hook is executed. -
Are ready.php hooks/addHookMethods called outside their use?
a-ok replied to a-ok's topic in General Support
Which is bizarre, right? Because if it's an `addHookMethod()` the docs say 'This enables you to add a new accessible public method to an existing object, which will execute your hook implementation method when called upon' (source: https://processwire.com/api/ref/wire/add-hook-method/) so why would ready.php run it and not just 'have it ready'? Is there an alternative way? -
Are `addHookMethod` hooks, declared in ready.php fired when ready.php is read or when the hook is used? The reason I ask is that I am combining a `addHookMethod` with an API (with a rate limit) but I'm only calling the hook in the head.inc and the page views are low but I've already hit the rate limit per month. If it's in ready.php is it being called on CMS page views etc too even if it's a hookMethod? $ipapiHttp = new WireHttp(); $ipapiAPIKey = "XXX"; wire()->addHookMethod('Page::ipapiGetUserLocation', function($event) use($ipapiHttp, $ipapiAPIKey) { $page = $event->object; $client = @$_SERVER['HTTP_CLIENT_IP']; $forward = @$_SERVER['HTTP_X_FORWARDED_FOR']; $remote = $_SERVER['REMOTE_ADDR']; if (!empty($client)) { $ip = $client; } elseif (!empty($forward)) { $ip = $forward; } else { $ip = $remote; } $result = $ipapiHttp->getJSON("https://api.ipapi.com/$ip?access_key=$ipapiAPIKey"); $event->return = $result; }); And in head.inc I'm calling the hook method: if (!$session->get("shopifyShopCurrency")) { if ($page->ipapiGetUserLocation()['currency']['code'] && $page->ipapiGetUserLocation()['currency']['code'] !== 'GBP') { $session->set("shopifyShopCurrency", "ROW"); } elseif (!$session->get("shopifyShopCurrency")) { // Set it as the Shopify store default $session->set("shopifyShopCurrency", $shopifyShopCurrency); } } else { $session->set("shopifyShopCurrency", $shopifyShopCurrency); }
-
Thanks! I ended up going with `json_encode()` which seems nice too.
-
Is it possible to output an array within a log? I'm currently doing: wire('log')->save("shopify", "collections = $collections"); Which obviously just outputs 'Array' and I tried doing: wire('log')->save("shopify", "collections = " . print_r($collections)); But this doesn't seem to do anything (or there's an error and skips it completely). Any thoughts?
-
That is useful! Unsure why it was working on my local because after testing all it would appear that using `$sanitizer->name()` doesn't return the value as lowercase.
-
`$sanitizer->pageName()` seems to work fine
-
Short one... but is there any reason why `$sanitizer` wouldn't work? On my local I have the following: `$sanitizer->name($link->settings_social_title);` which works fine but on my DigitalOcean server it doesn't seem to do anything?
-
Any thoughts?
-
Just a little thing but I tend try and use as many 'global' fields as possible across my sites (global_text, global_textarea, global_file, global_files etc.) and when you add each to a different template you can change the title of the field for that template, which is great, but what would also be great is for, say textareas, you could amend what's in the CKEditor Toolbar for the same field but on different templates. Sometimes templates just need Bold and Italic whereas some others need a lot more and having to allow the client access to the 'maximum' number when it's not required gets a bit frustrating. Anyway... just my thoughts!
-
Moving a hook function to a function called within 2 hooks
a-ok replied to a-ok's topic in API & Templates
Thanks folks. I tried this but received an error: Using $this when not in object context function shopifyInit(HookEvent $event) { 83: 84: $collections = $this->shopifyHttp->getJSON($this->shopifyBase . "custom_collections.json"); I'm using $this to grab my API keys etc outside of the scope of the hook $this->shopifyHttp = new WireHttp(); $this->shopifyAPIKey = blahblah Using $this works fine when using it directly in a hook but not within a function that's called within a hook? -
I had a hook method set up to do a few things, hooked to a lazycron of every 30 minutes: wire()->addHook('LazyCron::every30Minutes', function($event) { // functions etc } This was working fine but I needed to call the content of the function on a specific page save also so I made it into a public function and set up two hooks for it... one on the lazycron and one on page. function shopifyInit(HookEvent $event) { // functions etc } wire()->addHook('LazyCron::every30Minutes', null, 'shopifyInit'); wire()->addHookAfter('Pages::save', function($event) { $page = $event->arguments(0); if ($page->template->name == 'shop') { // Shop shopifyInit(); $page->message('Shopify cache has been cleared for all products'); } }); However on save I am getting this error... ArgumentCountError Too few arguments to function ProcessWire\shopifyInit(), 0 passed and exactly 1 expected Any thoughts where I'm going wrong?
-
Just a quick q. re https://processwire.com/docs/security/sessions/ – is this also related to https://processwire.com/api/ref/session/ – so if I was to create a session it would store it on the DB instead? I'm just thinking this could be useful for HTML5 sessionStorage etc. Thanks!
-
Sorting pages in the page tree via hook – unexpected order
a-ok replied to a-ok's topic in General Support
Turns out I had to set the sort before the return... if ($page->template->name == 'projects-overview') { foreach($page->children() as $child) { if ($child->detail_dates_date_sort === '') { $child->detail_dates_date_upcoming = $child->getUnformatted('created'); } else { $child->detail_dates_date_upcoming = $child->getUnformatted('detail_dates_date_sort'); } } $page->children()->sort('-detail_dates_date_upcoming'); $event->return = $page->children(); }- 1 reply
-
- 1
-
-
I thought this would be fairly trivial but it's not giving the expected results. I want to sort a parent/child set up in the admin page tree (I need to sort by multiple selectors, which the admin doesn't allow, so doing it via a hook) This is what I have as my hook: function hookPageTreeFind(HookEvent $event) { $page = $event->arguments('page'); if ($page->template->name == 'projects-overview') { foreach($page->children() as $child) { if ($child->detail_dates_date_sort === '') { $child->detail_dates_date_upcoming = $child->getUnformatted('created'); bd($child->title . ' ' . date('l j F Y', $child->detail_dates_date_upcoming)); } else { $child->detail_dates_date_upcoming = $child->getUnformatted('detail_dates_date_sort'); bd($child->title . ' ' . date('l j F Y', $child->detail_dates_date_upcoming)); } } $event->return = $page->children("sort=detail_dates_date_upcoming"); bd($event->return); } } wire()->addHookAfter('ProcessPageList::find', null, 'hookPageTreeFind'); It loops through each child page and grabs the date field 'detail_dates_date_sort' and sets that as the sort value. The date field is sometimes left blank so I set the sort value as the created date. I'm doing a bit of debugging (hence the bar dumps) and you can see from this screenshot that the order is messed up? The child at the end of the screenshot 'Black Quantum Futurism' has no date field set so thus outputting, correctly, the created date. Any pointers to where I'm going wrong? Thanks.
-
That's fine I don't allow for image insertions on text fields. Thanks again everyone for helping me resolve this.
-
Yes this fixed things. Thanks for the heads up. I guess it's no drama if it removes variations of ones that weren't corrupt, right?