Jump to content

Endpoint for requests from inputfield.


Zeka
 Share

Recommended Posts

Hi.

I'm working on Inputfieldtype that have to get some data from pasted URL. Something like InputfieldURLChecker, but this one is using js, but mine should use AJAX request to some endpoint on the site to get data and then render additional information to the inputfield.

So the question is how to provide such endpoint. I think that I can create a separete module that creates some page under Admin and make a request to this page. But I'm not sure that this is an optimal way, maybe there is something already in Inputfiled class or elsewhere that can help.

Thanks in advance. 

Eugene.

Link to comment
Share on other sites

4 hours ago, Zeka said:

but mine should use AJAX request to some endpoint on the site

I'm confused. Do you want to fetch data from a remote URL, another site? Or from another PW page in the same installation? I guess you could use CURL or WireHttp. Not sure you need a separate page under admin though.

  • Like 1
Link to comment
Share on other sites

Something I have done is add a method through a hook such as: 

$wire->addHook("ProcessPageEdit::executeCustomModule", function(){ ... })

So then you can make a request to:  page/edit/custom-module 

  • Like 4
Link to comment
Share on other sites

10 hours ago, Zeka said:

use AJAX request to some endpoint on the site to get data

I had a similar need recently for a new Fieldtype & Inputfield called AdminComments (allows users to post comments on backend pages).

The way i did it was to have a Process module which installs when you install the Fieldtype and Inputfield. The javascript gets the endpoint from a data attribute that the inputfield generates (i put all the data attributes for the post url and other info directly in the inputfield, though it could have been put in the JS config also).

If it helps to see this method, i can share that beta module with you. I also researched extensively to see if i could get away without a process module, and use some other endpoint, but was unsuccessful, so i just went with a Process Module. I made the module permission page-edit so that it doesn't need any special permissions to access when doing the post requests.

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

@dragan, @elabx, @Macrura Thanks for ideas. 

8 hours ago, Macrura said:

If it helps to see this method, i can share that beta module with you. I also researched extensively to see if i could get away without a process module, and use some other endpoint, but was unsuccessful, so i just went with a Process Module. I made the module permission page-edit so that it doesn't need any special permissions to access when doing the post requests.

@Macrura Would be glad to see your module. Actually, my main intention is also to keep only Fieldtype and Inputfieldtype modules without additional modules. I have read somewhere that it is also possible to make requests to Input/Fieldtype config page. Have you tried this approach? 

 

Link to comment
Share on other sites

Interesting topic, I'll need this soon. RockGrid is sending Requests to the current page appending ?field=myfield so that it makes sure it gets loaded (even if it's set to ajax). But I want to take a different route for the next version. It's easier and cleaner to have one single endpoint url imho. 

2 hours ago, Zeka said:

I have read somewhere that it is also possible to make requests to Input/Fieldtype config page. Have you tried this approach? 

I think @Robin S mentioned that somewhere but can't find it..

Link to comment
Share on other sites

@Zeka, you can hook before any Process execute method that the user has access to and replace the output. You can see an example here where I replace ProcessLogin::executeLogout (because that is something that all users have access to).

I like the sound of @elabx's suggestion of adding a new execute method and accessing the URL segment for it, but with some quick testing I couldn't get that to work. But that's probably just me. 

  • Like 1
Link to comment
Share on other sites

18 hours ago, elabx said:

Something I have done is add a method through a hook such as: 


$wire->addHook("ProcessPageEdit::executeCustomModule", function(){ ... })

So then you can make a request to:  page/edit/custom-module 

@elabx Could you please tell more how did you implement it? 

I have an autoload module, where I declare my hook 

public function init() {
	$this->addHook('ProcessPageEdit::executePreview', $this, 'testPreview');
}


public function testPreview($event) {
	return 'test';
}

But when I try to access 'admin/page/edit/preview' I got redirect to 'admin/page/edit/bookmarks/' and 'admin/page/edit/preview/' ( with slash) redirects to 'admin/page/edit/preview/bookmarks/bookmarks/bookmarks/bookmarks/'.

Link to comment
Share on other sites

OHHH you know what, I'm plain wrong. The problem with processPageEdit is that it redirects on init if there is no page id in the query parameters, so unless you add that parameter to your request, it will always redirect. 

https://github.com/processwire/processwire/blob/master/wire/modules/Process/ProcessPageEdit/ProcessPageEdit.module#L358

Also its hould be preceded by the word execute, and camelcase the method name.

What I had ACTUALLY done, is hook right before page edit and just halt execution when I'm done with my stuff, but surely this doesn't feel really clean. 

https://github.com/elabx/InputfieldExportFiles/blob/master/InputfieldExportFiles.module#L30

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...