Jump to content

Add custom config view to module


Recommended Posts

Hello everybody! 

I'm new as a prestashop developer and I'm doing my first module. I've found a wall trying to add a custom HTML view to my module's config page.

I'm using prestashop 1.7.6.0.

Let me explain myself:

I have a module and I'd like to add an HTML view directly pressing the config button:

image.png.2367499c2ad6dcd30cc06d6aaf68c39a.png

The only thing I've found online, is using the helper that Prestashop provides to create forms, but I need more than a form. 

I need to add a new view, with its own controller to collect data and then use this data to trigger an action from a new Class I've created. 

I have several questions that I couldn't resolve on my own, so I hope you all will help me!

Can I add this custom HTML view to this config button? If I can, how can I do it? 

Are there any other posts related to this?  Because if I haven't found them :( 

How can I add this class to other classes to use it in this new custom config view?

 

Thank you so much for your time and your help, I hope we will find an answer together.

Regards!!

Link to comment
Share on other sites

hello,

i'll try to help you, in your module controller you have a function like this "public function getContent()" right? and let's say you already made a tpl file located at your module/views/templates/admin/config.tpl, "config.tpl" is just a sample, you can rename it to whatever you like, so let's say you already put some html on that tpl file, then on your getContent() function, make a return with the tpl file like this,

public function getContent()
{
    return $this->display(__FILE__, '/views/templates/admin/config.tpl');
}

after you install you module and press configure, prestashop will find your getContent() function and execute everything in it, just let me now if you have more questions

  • Like 1
Link to comment
Share on other sites

4 hours ago, ApoA said:

hello,

i'll try to help you, in your module controller you have a function like this "public function getContent()" right? and let's say you already made a tpl file located at your module/views/templates/admin/config.tpl, "config.tpl" is just a sample, you can rename it to whatever you like, so let's say you already put some html on that tpl file, then on your getContent() function, make a return with the tpl file like this,


public function getContent()
{
    return $this->display(__FILE__, '/views/templates/admin/config.tpl');
}

after you install you module and press configure, prestashop will find your getContent() function and execute everything in it, just let me now if you have more questions

Thank you so much, this is exactly what I didn't know. I'm still a little bit confused with all the functionalities Prestashop has 😵😵😵

And do you know how can I add a class to the other Core classes during the instalation of my module to use it from this config view? 

Thanks again, that made my day!!

  • Like 1
Link to comment
Share on other sites

1 minute ago, ApoA said:

hello,

don't worry, even now i'm still confused about prestrashop 😄

can you explain more about your question? i really don't understand. sorry.

Yes, sure, I'll explain myself! 

I've created a Class with some functions that I'd like to call from my config template. I'll write an example:

 

class MyClass{

   	 function __construct() {}
	

	 function makeMeRich(){}

}

Let's say I've written this class  "MyClass". Right now I have it at the root folder of my shop, and I include the file with   " include(dirname(__FILE__).'/MyClass.php'); "

But I do not want this, I'd like to add this class directly with the instalation of my module to create new objects of this Class like I can do this: 

$p = new Product(); 

I don't know where should I include my class to call the function makeMeRich() from my config template. 

I've read I have to create a folder with a composer.json and the class, and then use composer to call the json, but I'm a little bit lost with that too, because it's not really clear in the explanations I've read.

I hope I've explain myself enough to have an answer :P 

Link to comment
Share on other sites

6 minutes ago, ApoA said:

hello,

i'm a bit confused with what you're aiming to do, but why should "MyClass" be located at the root of your prestashop folder?

That's the point, I don't want it there but I don't know where to locate it. I'd like to add it to the Core Classes to avoid having the class at the root folder.

Link to comment
Share on other sites

hello,

you can probably put the method of "MyClass" inside your module controller. like putting "function makeMeRich(){}" inside your module controller. and if you want to use it in your config file you can do something like this

public function getContent()
{
    $this->makeMeRich();
	
    return $this->display(__FILE__, '/views/templates/admin/config.tpl');
}

public function makeMeRich(){}

 

Link to comment
Share on other sites

1 minute ago, ApoA said:

hello,

you can probably put the method of "MyClass" inside your module controller. like putting "function makeMeRich(){}" inside your module controller. and if you want to use it in your config file you can do something like this


public function getContent()
{
    $this->makeMeRich();
	
    return $this->display(__FILE__, '/views/templates/admin/config.tpl');
}

public function makeMeRich(){}

 

Hello ApoA and thank you for all the answers. I don't think this is a good idea, because the real MyClass has more than 500 lines... I'd like to have it separate from the module controller. The problem is that I do not know where to locate this MyClass.php file inside my module's folders tree to use use it from my module controller.

Link to comment
Share on other sites

hello,

well i forgot, you can just put "MyClass" inside the root folder of your module, and you must include in your module controller like this

include_once('myclass.php');

or

require_once dirname(__FILE__).'myclass.php';

it depends which one works, and after that you can do something like this above your module controller class name

$myclass = new myclass();

now you can use every method in myclass like this

 $this->makeMeRich();

i hope i got it right 😄

Edited by ApoA (see edit history)
  • Like 1
Link to comment
Share on other sites

1 minute ago, ApoA said:

hello,

well i forgot, you can just put "MyClass" inside the root folder of your module, and you must include in your module controller like this


include_once('myclass.php');

or


require_once dirname(__FILE__).'myclass.php';

it depends which one works, and after that you can do something like


$myclass = new myclass();

now you can use every method in myclass like this


 $this->makeMeRich();

i hope i got it right 😄

Yes, that seems to be perfect for what I was asking. Thank you so much, I think I can follow from here. Once again, you made my day! 

  • Like 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
×
×
  • Create New...