Jump to content

Determining the context a page is being created in [solved]


FireWire
 Share

Recommended Posts

Hey all,

I have pages that can be created two ways, in the PW admin and via an endpoint where JSON data is sent. I have a hook that checks field data on page save and errors if it is not correct.

I would like this to create an error in the PW admin using $this->errors, but if the page is created via the Page API in the script that handles JSON requests I want to throw an exception I can catch and handle accordingly for the JSON response. This also allows for setting a user friendly error message for the PW admin, and machine friendly data for API use.

Trying to figure out how to detect which context that the page is being created in. A script, or in the PW admin and act accordingly. Is this possible?

Link to comment
Share on other sites

  • FireWire changed the title to Determining the context a page is being created in

Sounds like you can simply set a flag at runtime:

<?php
// your script that creates the json page
$page = new Page();
...
$page->createdViaApi = true;
$page->save();

Then your hook should be able to catch that flag:

<?php
$wire->addHookAfter("Pages::saveReady", function($event) {
  $page = $event->arguments(0);
  if($page->createdViaApi) {
    // check and throw exception
  }
  else {
    // check and show error to user
  }
});

 

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

As far as I understand your issue/question... you could add a text field or maybe even better a select options field (if there are two or more options to create a page - future-proof) or a checkbox (if there are only these two options) and add a default value for either admin backend or JSON endpoint - depending on the source.

It's just another field, doesn't take that much memory/space and can even be selected from within a query.

Link to comment
Share on other sites

1 minute ago, bernhard said:

Sounds like you can simply set a flag at runtime:

<?php
// your script that creates the json page
$page = new Page();
...
$page->createdViaApi = true;
$page->save();

Then your hook should be able to catch that flag:

<?php
$wire->addHookAfter("Pages::saveReady", function($event) {
  $page = $event->arguments(0);
  if($page->createdViaApi) {
    // check and throw exception
  }
  else {
    // check and show error to user
  }
});

 

So there I was diving into a technical hole and the solution was so simple to begin with. Excellent.

Link to comment
Share on other sites

1 minute ago, wbmnfktr said:

As far as I understand your issue/question... you could add a text field or maybe even better a select options field (if there are two or more options to create a page - future-proof) or a checkbox (if there are only these two options) and add a default value for either admin backend or JSON endpoint - depending on the source.

It's just another field, doesn't take that much memory/space and can even be selected from within a query.

This would be a good idea if I knew the context ahead of time but the content on a field could be entered/edited from anywhere after added to a template.

Link to comment
Share on other sites

  • FireWire changed the title to Determining the context a page is being created in [solved]
3 minutes ago, FireWire said:

This would be a good idea if I knew the context ahead of time but the content on a field could be entered/edited from anywhere after added to a template.

As we speak about page creation... you should know that. Unless I'm missing something but ok.
The solution @bernhard provided looks promising eitherway.

Link to comment
Share on other sites

9 minutes ago, wbmnfktr said:

As we speak about page creation... you should know that. Unless I'm missing something but ok.
The solution @bernhard provided looks promising eitherway.

I think your solution works for a bigger context of where a lot of data on a page would be dependent on the context it was created in. I just needed to make sure that the hook for one field can determine at runtime whether the field was filled by a user in the PW admin or the Page API.

Your solution from the PW admin side did make me think bigger picture which is also a possible solution for my application. This field is being used on pages that can be created/edited in the ProcessWire admin, but they can also be created/modified by a website REST API that other systems- in our case Salesforce- can use to create/modify data on the site. Calls to this website API are authenticated using ProcessWire users which have an API key assigned and a role of web-api-access. So rather than focus on verifying where the the page was created at the field level, I could check that the user creating the field is an API user at the page level.

I got a little more into the weeds with that description, but your idea helped me think of a different approach.

  • Like 1
Link to comment
Share on other sites

I think the easiest and arguably cleanest way to detect this would be wire('page'). In the Admin it will give you the system page with the ProcessPageEdit process, ID 10. When it’s your API it should be whatever page you set up as the endpoint.

Or you could look at input()->url or something.

  • Like 2
Link to comment
Share on other sites

1 hour ago, Jan Romero said:

I think the easiest and arguably cleanest way to detect this would be wire('page'). In the Admin it will give you the system page with the ProcessPageEdit process, ID 10. When it’s your API it should be whatever page you set up as the endpoint.

Or you could look at input()->url or something.

That's a pretty rad idea as well. The API endpoints are all set virtually using URL hooks so the process comes up as ProcessPageView in that context.

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...