Jump to content

Display buttons in the header toolbars page


Recommended Posts

Hello guys.

i'm developping a custom module. It clone product from one shop to another in multistore mode.

So, a preview screenshot of what i need at the bottom. I need to put 2 buttons in the header toolbars. How can i achieve it?

My custom admin controller:

	public function compare(Request $request): Response
	{

        // Form to make comparison of sources and clones products and confirm the clone process
        $formBuilder = $this->createFormBuilder($ps, [
            'action' => $this->generateUrl('final_process'),
            'method' => 'POST'
        ]);
        
        // Add a hidden input only for each product to clone which is valid)
        // This input will contain a serialized object of the product source and the product clone
		// $productToCompare is an array($product_source_obj, $product_clone_obj)
        foreach($productsToCompare as $product_r) {

            $input_name = 'product-'.$product_r->id;

            $formBuilder
                ->add($input_name, HiddenType::class, [
                    'data' => base64_encode(serialize($productStorage)),
                    'mapped' => false,
                ])
            ;
        }
        
        $form = $formBuilder->getForm(); 
        $form->handleRequest($request);
        
        return $this->render('@Modules/mymodule/views/templates/admin/compare.html.twig', [
            'formObject'        => $form,
            'productsToCompare' => $productsToCompare,
        ]);
    }

Also, my template:

{% extends '@!PrestaShop/Admin/layout.html.twig' %}
{% block content %}

	{{ form_start(form) }}
  		<button type="submit" class="btn btn-primary">VALID AND CLONE</button>
  		<button type="button" class="btn btn-outline-secondary" onClick="window.history.back();">GO BACK</button> 
	{{ form_end(form) }}

  	<div class="row">
      <div class="content container-fluid">
        <div class="col table-responsive">
          <table class="table table-sm table-hover">
            {{ table content }}
          </table>
        </div>
      </div>
  	</div>
{% endblock %}

I thnik i'm missing something between the the extended layout and the block content.

Sans titre 1.jpg

Edited by Titi2 (see edit history)
Link to comment
Share on other sites

I found hook displayDashboardToolbarTopMenu in the header toolbar.

Here the hook in the main file of my module :

class Mymodule extends Module
{
	[...]

	public function hookDisplayDashboardToolbarTopMenu($params)
	{
		$this->get('twig')->render('@Modules/'.$this->name.'/views/templates/admin/form/form_submit.html.twig', [
			'button' => $params['button'],
		]);
        }
    }
}

And mycontroller.php:

class LinkProductsToShop extends FrameworkBundleAdminController
{
	public function someMethod()
 	{
		// build a form here
		$formBuilder = $this->createFormBuilder($ps,[])
		[...]
		
		$my_button = 'some button';

      		Hook::exec('hookDisplayDashboardToolbarTopMenu', [
      			'button' => $my_button,
      		]);

		return $this->render('@Modules/mymodule/views/templates/admin/compare_products.html.twig', [
			'form' => $form->createView(),
		]);
	}
}

 

But seems the hook is executed before Hook:exec() is called. So $params['button'] is null. It should be $my_button.

 

 

Edited by Titi2 (see edit history)
Link to comment
Share on other sites

Okay! I'm going forward. In fact, in my controller i was calling :

Hook::exec('hookDisplayDashboardToolbarTopMenu', [
	'button' => $my_button,
]);

instead of

Hook::exec('displayDashboardToolbarTopMenu', [
	'button' => $my_button,
]);

be careful of hookDisplayDashboardToolbarTopMenu. Now, my variable $my_button is passed properly to the hook function.

If i made var_dump($params['button']), it return the content of $my_button. BUT! when i send this variable to twig like this:

public function hookDisplayDashboardToolbarTopMenu($params)
{
	var_dump($params['button']); // return 'some button'

	$this->get('twig')->render('@Modules/'.$this->name.'/views/templates/admin/form/form_submit.html.twig', [
		'button' => $params['button'],
	]);
}

It doesn't not return anything. I'm missing something again...

The template is showing but not {{ button }}

Link to comment
Share on other sites

  • 9 months later...

I don't know if you've solved this problem, but I have a solution - maybe it will be useful to someone.

public function hookActionGetAdminToolbarButtons(array $params) : void
{
    // Get the collection of toolbar buttons
    $toolbar = $params['toolbar_extra_buttons_collection'];

    // Create a new button
    $newButton = new \PrestaShop\PrestaShop\Core\Action\ActionsBarButton(
        'btn-secondary', // CSS class for the button
        ['href' => 'your_link_here'], // Link where the button redirects
        'Your button text' // Text displayed on the button
    );

    // Add the new button to the collection
    $toolbar->add($newButton);
}

You don't say what version of PrestaShop you are using, but it definitely works on v8.1.3.

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