Jump to content

getModuleConfigInputfields add button field with different action


---
 Share

Recommended Posts

In my module's getModuleConfigInputfields method, I've added a field of type: InputfieldButton.

I want this button have a different action than the usual submit button (which is still there) and the submit button to behave as it always does.

For example; I want the InputfieldButton button to clear a few folders when it's clicked. Is this possible, if so, how can I achieve this?

Link to comment
Share on other sites

Something like this works with Process type modules. First you create a submit button, change its name to your action

<?php
$submit = $this->modules->get('InputfieldSubmit');
$submit->name = 'action_name';
$submit->value = 'Action Title';
$submit->icon = 'save';
$form->add($submit);

Then inside 

<?php
public function execute() // or ready() or init()
{
	$out = '';
	// ...

	if($this->input->post('action_name')) {
		// do your thing

		$this->message('Did my thing');
		$out .= 'Here are the results';
		// ...
	}

	return $out;
}

To achieve the same thing inside plain modules, you need to check if($this->input->post('action_name')) inside ready() / init() function. However, Process modules are more suitable for these types of tasks. You get custom urlSegments that you can redirect to and process using execute[Segment] functions. Like so

<?php namespace ProcessWire;

class ProcessMyAction extends Process implements Module
{

    public static function getModuleInfo()
    {
        return [
            'title' => 'My Action',
            'summary' => 'Does my action when you need to',
            'version' => '0.0.1',
            'permission' => 'page-edit',
            'icon' => 'cogs'
        ];
    }

    public function execute()
    {
        /** @var InputfieldForm $form */
        $form = $this->buildForm();

        $out = $form->render();
        if($this->input->post('action_name')) {
            $out .= 'Just did my thing';
        }

        return $out;
    }

    public function executeHi()
    {
      	// intercepts `./hi` url segment
      
        $this->message("Well hello");
        return "<p>You clicked hi!</p>";
    }

    public function executeBye()
    {
      	// intercepts `./bye` url segment
      
        $this->setFuel('processHeadline', 'Goodbye?');
        $this->error('Not so fast!');
        return "<p>You clicked bye!</p>";
    }

    public function ___install()
    {
      	// set up process page that is reachable from dropdown menu
        try {
            $p = new Page();
            $p->template = 'admin';
            $p->parent = $this->pages->get($this->config->adminRootPageID)->child('name=setup'); // under /admin/setup
            $p->name = 'my-action';
            $p->process = $this; // when clicked from dropdown menu, execute() function runs
            $p->title = 'My Action';
            $p->save();
            $this->message("Created action page at $p->path.");
        } catch (WireException $e) {
            $this->error('Cannot create action page');
            $this->error($e->getMessage());
        }
    }

    public function ___uninstall()
    {
      	// delete admin page
        $p = $this->pages->get('name=my-action, has_parent=2');
        if($p->id) {
            $p->delete();
            $this->message("Removed Action page at $p->path");
        }
    }

    protected function buildForm()
    {
        /** @var InputfieldForm $form */
        $form = $this->modules->get('InputfieldForm');

        /** @var InputfieldName $fName */
        $fName = $this->modules->get('InputfieldName');
        // ...
        $form->add($fName);

        
        $submit = $this->modules->get('InputfieldSubmit');
        $submit->name = 'action_name';
        // ...
        $form->add($submit);
        return $form;
    }
}

 

  • Like 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
 Share

×
×
  • Create New...