Jump to content

How to save value to every pages specific field?


Flashmaster82
 Share

Recommended Posts

Hi i need some help to solve this. I need to save a specific value when saving a page

$p = $pages->get('template=customer_project');

$p->location = "Hello world";

$page->save($p); 

This is my code right now in ready.php. location is my field "text field" inside template "customer_project". Right now when i refresh a page with the template it only populates one page? I want every page that has this field "location" to say Hello world and save it to the database? Don´t know if a hook is better..

 

PLEASE HELP!!!

 

Link to comment
Share on other sites

Hi!!

Try this: 

$found = $pages->find('template=customer_project');
forach($found as $p){
	$p->setAndSave('location',"Hello world" ); 
}

EDIT: Just reading again that you have this in ready.php so this will run on every page load, for every page with custom_project template, not very efficient lol. Like you mention, perhaps a hook after certain action would be better?

  • Thanks 1
Link to comment
Share on other sites

You are a life saver Elabx thanks!! That worked..

I´m now facing another problem

$found = $pages->find('template=customer_project');

if ($found->customer_project_address === 0){
   $coordinates = "Hello world";
    } 
 
foreach ($found as $p){  
	$p->setAndSave('location',$coordinates); 
    }

customer_project_address is a toggle button that i´m trying to target. But the IF statement just don´t work here i don´t know why.. I needs to be outside the foreach btw. Do you have any clues?

 

Link to comment
Share on other sites

$found = $pages->find('template=customer_project');

$geo = new \OpenCage\Geocoder\Geocoder('API-KEY');
    
   if ($found->customer_project_address === 0){
   $result_parent = $geo->geocode("Location", ['language' => 'Sweden', 'countrycode' => '+46']);  
   $first = $result_parent['results'][0];
   $coordinates = $first['geometry']['lat'] . ',' . $first['geometry']['lng'];
    } 

   else if ($found->customer_project_address === 1){
   $result = $geo->geocode("Another location", ['language' => 'Sweden', 'countrycode' => '+46']); 
   $first = $result['results'][0];
   $coordinates = $first['geometry']['lat'] . ',' . $first['geometry']['lng'];     
    } 

foreach ($found as $p):
{
   $p->setAndSave('my_location_field',$coordinates); 
}
endforeach;

This is my current code. It´s fetching a geolocation with longitude and latitude. But the IF Statements are outside the foreach. I only want one request per page because its slowing down the whole site and admin otherwise. The "customer_project_address" is a toggle field btw. I´m using Opencage to fetch the long/lat from an address in the pages. My purpose of this is to put just put addresses in the pages to then get markers on a map later on. There is modules for this i know but they comes with a map inside admin also which i dont want. Also they cant be inside groups/repeaters etc thats why im doing this instead. But maybe its better to do something with a hook, dont know how to do that though, im no the most experienced programmer only beginner.

Link to comment
Share on other sites

Would it work if after one of the following events:

  • User visits page (literally opens up the browser in the page)
  • Page save (whoever saves the address, hits Save in the admin, or the page is saved through the api)

The location lat, lng would get added and saved to the page's field through the API? 

  • Thanks 1
Link to comment
Share on other sites

I tried to make an overview of everything so its easier. Well if i have hundreds of addresses it would then make the loading time to a minute ? That´s why i trying to fetch the lat/long json file on every page so that it only needs to request the lat/long coordinates from the field in the map page instead of calling hundreds of json files when visiting the site. So thats why i wanted to have the if statements before the foreach inside ready.php so it wont call the opencagedata twice on every page. The toggle field inside the template (customer_project) is asking for an address inside the template and on the parent page so if i then have them both populated it would then request double operation. Only want one coordinate from every page on the map page. It is working as intended but its the IF statement that i´m struggling with right now. Maybe also the whole process can be faster somehow.

Link to comment
Share on other sites

Not sure if you read my last post and why I asked what I asked but nontheless I think you could try the following code on ready.php. Have in mind, the following function declared in the saved() hook executes after the page is saved, which would be my first approach in getting the gecoded location without doing all the pages in one request . So this approach sort of "throttles" geocoding the address, doing it only when the user adds the address. 

$wire->addHookAfter('Pages::saved', function(HookEvent $event) {
   $page = $event->arguments(0);
   // Skip if not the template we are interested in..
   if($page->template != 'customer_project') return;
   $geo = new \OpenCage\Geocoder\Geocoder('API-KEY');

   //This is the field that contains the address in the pages with customer_project template
   $address = $page->address_field;
   
   if ($page->customer_project_address === 1 && $address){
     // I guess the first param is the actual address? $address assigned above?
     $result = $geo->geocode("Another location", ['language' => 'Sweden', 'countrycode' => '+46']); 
     $first = $result['results'][0];
     $coordinates = $first['geometry']['lat'] . ',' . $first['geometry']['lng'];
     //here the field gets saved, have in mind that this is different than saving 
     //the whole page, which would get is into an infinite loop in this scenario
     $page->setAndSave('my_location_field', $coordinates); 
    }
});

 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...