Jump to content

Where to put template PHP for InputField using a pwModalWindow?


theo
 Share

Recommended Posts

I am experimenting with writing a FieldType / InputField which is calling PHP Code via modal window / IFrame.

It is no problem to set up a normal template file -> template -> page and call it inside IFrame, but how to auto install this?

Does it work the same way as for normal modules, like using  ___install()  and  ___execute() ?

Thank you.

 

  • Like 1
Link to comment
Share on other sites

My question is, what do you want to load in the modal? Is it a ProcessWire page? I ask because it seemed to me you wanted to include some PHP via a modal?

To open a ProcessWire page in a modal is as simple as:

$out = "<a href='{$urlOfThePageYouWant}?modal=1' class='pw-modal pw-modal-medium'>Open Modal</a>";// of pw-modal-large or pw-modal-small
// return $out somewhere;

 

in your init() method in YourInputfield. @others: is this always required? I'm not sure

$modules = $this->wire('modules');
$modules->get("Jquery");
$ui = $modules->get("JqueryUI");
$ui->use("modal");
$modules->get("JqueryMagnific");

 

Edited by kongondo
more code
Link to comment
Share on other sites

Thank you.

I have no problems with the modal, i have a problem with {$urlOfThePageYouWant}

I just don't know how a fieldtype should install a template/page.

if I have:

class Someclass extends Process

I can use public function ___execute() {}

To run my code. But what to do with a

class Someclass extends Inputfield

 

 

In other words: A Javascript which belongs to a fieldtype module needs to call a page url which also belongs to the fieldtype.

How can I do this?

 

 

Link to comment
Share on other sites

OK. I see. What confused me was this.

1 hour ago, theo said:

a FieldType / InputField which is calling PHP Code via modal window / IFrame.

That's why I asked, why do you need to call PHP code in a modal? I'm still curious about this. About your other question...

1 hour ago, theo said:

Does it work the same way as for normal modules, like using  ___install() 

I've not tried it, but when installing a Fieldtype, like any other module, it will call install() if one is present. It is a module after all. So, try create you template, page, etc there (or call a script in install() to create the page, template, etc) .

Link to comment
Share on other sites

OK. Thank you.

So I have to do do it the hard way, and copy/generate the template.php file, then install the template and create a page.

But what is the right place for a template file which belongs to a InputField module?

Thanks.

Link to comment
Share on other sites

The more general question is:

A Javascript which belongs to a fieldtype module needs to call a page url which also belongs to the fieldtype.

Be it an Iframe or an Ajax call. It needs an URL which runs the PW/PHP code.

How do I handle (Install/Setup) this?

Link to comment
Share on other sites

56 minutes ago, theo said:

So I have to do do it the hard way, and copy/generate the template.php file, then install the template and create a page.

But what is the right place for a template file which belongs to a InputField module?

I'm not sure this is the 'hard way' :). Since this is a Fieldtype, it has no admin page, unlike a Process module. So, if you need a page for it, then the page needs to be created. You can do all these using the API and the template file stuff (copying it to /site/templates/ using PHP. E.g., when installing ProcessWire, it writes to/populates config.php and moves it into place under /site/config.php. That's the approach I am talking about.  If unsure how to do it, I can show you how.

It is good to create the page to for your FieldtypeYourClass stuff under /admin/. That way, it won't be tempered with by non-superususers. You can even throw in a hidden status on it. A couple of my modules (including Menu Builder) use this technique. The only difference is that they do not have template files, since I post to the Process Module instead (which has a page under /admin/. So, my suggestion.

  1. FieldtypeYourClass should auto install InputfieldYourClass.
  2. In Fieldtype add the install() method. In there, you can either (a) Directly create the assets needed (page, template, template, file, etc) or (b) call a method or methods within your FieldtypeYourClass to do these or (c) Call a YourClass.php or someOtherFile.php file with the script to create these assets.
  3. Creation of template first, then create the page, then copy the templateFile.php to /site/templates/.
  4. You will need to decide if the parent of the page will be /admin/, in which case you don't want it to appear in the admin menu, so apply hide status. 
  5. An alternative to creating this page is creating a Process Module to handle your ajax calls.
  6. Once, the page is created, grab its $page->id (e.g. saving it to a class property).
  7. In your Field's ___getConfigInputfields(), create a hidden input to store the $page->id. Alternatively, since you know the template used by your $page, as well as its name, you can be grabbing it like that when responding to the ajax calls, so no need for this hidden input.

As for the right place for a template file, I'd go for /site/templates/. That's where template files live. Alternatively, you can have a template file there that just calls yourOtherCode.php to handle your ajax calls. If you don't want other pages to be created using your template file, you can add that setting using the API when creating the template.

As for install() in Fieldtype Modules, have a look at FieldtypeOptions. It calls install(), so, yes, this can be done.

When initiating your FieldtypeYourClass (have a look at FieldtypeEvents), you can make it have a setPage() method that sets your $page, hence making it available to your InputfieldYourClass.

Other things you can do

  • Lock your hidden $page (so as not to delete it by mistake).
  • Have your FieldtypeYourClass check for the presence of the $page and throw an error if one not found (or auto-recreate it).
  • On uninstall, delete the assets you created for the module (page, template, template file, etc).

 

55 minutes ago, theo said:

A Javascript which belongs to a fieldtype module needs to call a page url which also belongs to the fieldtype.

Be it an Iframe or an Ajax call. It needs an URL which runs the PW/PHP code.

How do I handle (Install/Setup) this?

I think I've answered this above. A Fieldtype does not have its own page in the sense of a Process Module. So, you would need to create your own.

Hope this helps.

Edited by kongondo
more suggestions
  • Like 3
Link to comment
Share on other sites

Thank you very much.

I've simply added a separate MyModule.module file

class MyModule extends Process implements Module 

to the module folder.

And in the ModuleInfo of the Fieldtype added:

'installs' => 'MyModule',

So i can run my code in __execute of this Process module.

Works!

Thank you.

  • Like 1
Link to comment
Share on other sites

58 minutes ago, theo said:

I am trying to write a simple image picker field because of this thread.

The idea is to Just click the button -> click the image -> done.

It is already working but there is still a lot to do.

Looking good!

Thanks for working on this. PW really needs an "image reference" fieldtype (wishlist topic). Several times I've thought about tackling it, but seems like a lot of work because users will be expecting a feature set matching the core images inputfield: sortable, scalable thumbs, different thumbnail views, etc. Not to mention new challenges like setting tags or description on the reference that is different from the source tags/description, how to handle deletion of the source image, etc. Quite tricky.

  • Like 2
Link to comment
Share on other sites

4 minutes ago, theo said:

Oh no, do not expect too much, you scare me.

Ha, sorry. Not expecting delivery of the all-singing, all-dancing image reference module. :-)

What I meant is that due to needing to be kept in sync with the feature set of the core image inputfield/fieldtype, a full solution for an image reference fieldtype probably needs to come from Ryan, either as a core feature or a pro module. But I don't think we can expect that any time soon.

Link to comment
Share on other sites

7 hours ago, Robin S said:

Ha, sorry. Not expecting delivery of the all-singing, all-dancing image reference module. :-)

What I meant is that due to needing to be kept in sync with the feature set of the core image inputfield/fieldtype, a full solution for an image reference fieldtype probably needs to come from Ryan, either as a core feature or a pro module. But I don't think we can expect that any time soon.

You are right, it is always better when things come from Ryan. ;)

But regarding your suggestions in the message above: I think this is not what my Imagepicker is about.

1: I do not think that the backend user should be able to manipulate a referenced image in the Imagepicker field. 
   An Image field is a better choice in this case.

2: It is a single field, so the size etc. will be set in the template / css and not by the backend user as in a wysiwyg editor.

3: Deletion of the source image: This would require a system wide solution. You can delete an image which is referenced in CKEditor without warning too.

Link to comment
Share on other sites

@theo wow! I will like a lot give you my help, but I think that I'm not good enough, it's just few weeks that i work with PW and I have to understand a lot of things about it works. and honestly my php level isn't hight! But if you want you can write me I will try to help you anyway!

Link to comment
Share on other sites

3 hours ago, MarcoPLY said:

@theo wow! I will like a lot give you my help, but I think that I'm not good enough, it's just few weeks that i work with PW and I have to understand a lot of things about it works. and honestly my php level isn't hight! But if you want you can write me I will try to help you anyway!

Thank you. My level isn't high either.

But I think this project is not big enough for teamwork.

Not even sure it's a project. ;)

  • Like 1
  • Haha 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

  • Recently Browsing   0 members

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