Jump to content

How to add additional button next to the save button on top (backend)


Juergen
 Share

Recommended Posts

Hello @ all,

I have created a little module (adapted from Somas PageSave.module) which adds an additional save button next to the save button at the bottom.

post-2257-0-48411700-1455001491_thumb.pn

This button saves the page and redirects to the frontend page.

Here ist the code:

<?php
/**
 * Adding other types of save buttons for page edit form.
 *
 * ProcessWire 2.x
 * Copyright (C) 2010 by Ryan Cramer
 * Licensed under GNU/GPL v2, see LICENSE.TXT
 *
 * http://www.processwire.com
 * http://www.ryancramer.com
 *
 */
class CustomPageSave extends WireData implements Module
{
    /**
     * getModuleInfo is a module required by all modules to tell ProcessWire about them
     *
     * @return array
     *
     */
    public static function getModuleInfo()
    {
        return array(
            'title' => 'CustomPageSave',
            'version' => 1,
            'summary' => 'Example for adding other save buttons to page edit',
            'href' => 'http://www.processwire.com',
            'singular' => true,
            'autoload' => true
        );
    }
    /**
     * Initialize the module
     *
     * ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called
     * when ProcessWire's API is ready. As a result, this is a good place to attach hooks.
     *
     */
    public function init()
    {
        $this->addHookAfter("ProcessPageEdit::buildForm", $this, "addSaveButton");
        // tell processwire that this is a page save
        if ($this->input->post->submit_save_minor) {
            $this->input->post->submit_save = 1;
            // attach hook on page save
            $this->addHookAfter("Pages::saved", $this, "hookPageSave");
        }
    }
    public function hookPageSave($event)
    {
        $page = $event->arguments("page");
        if ($this->input->post->submit_save_minor) {
            // this will get saved after this saveReady hook so no need to save here            
            $message = __("Page was saved");
            $this->message($message);
            $pageid = $page->id;
            $goto   = wire("pages")->get("id=$pageid")->url; //get url of frontend article
            wire("session")->redirect($goto);
        }
    }
    public function addSaveButton($event)
    {
        $form       = $event->return;
        $buttontext = __("Save and go to page");
        // new submit button
        $f          = $this->modules->InputfieldSubmit;
        $f->attr("name", "submit_save_minor");
        $f->attr("value", $buttontext);
        // add submit button after the regular save button
        $form->insertAfter($f, $form->get("submit_save"));
    }
}

Now I want to do the same for the save button (copy) at the top, but I cannot figure out how.

post-2257-0-84650700-1455001595_thumb.pn

The problem is that both save buttons have the same name: submit_save.

The code line for inserting the button is the following:

$form->insertAfter($f, $form->get("submit_save"));

But how can I achive this if the button at the top and at the bottom have the same name?

Maybe someone can help me?

Best regards

  • Like 1
Link to comment
Share on other sites

Thanks BitPoet

this does exactly what I wanted.

Here is the complete code for all who are interested in:

Edit: Condition not to show on pages under the admin section was added

<?php
/**
 * Adding other types of save buttons for page edit form.
 *
 * ProcessWire 2.x
 * Copyright (C) 2010 by Ryan Cramer
 * Licensed under GNU/GPL v2, see LICENSE.TXT
 *
 * http://www.processwire.com
 * http://www.ryancramer.com
 *
 */
class CustomPageSave extends WireData implements Module
{
    /**
     * getModuleInfo is a module required by all modules to tell ProcessWire about them
     *
     * @return array
     *
     */
    public static function getModuleInfo()
    {
        return array(
            'title' => 'CustomPageSave',
            'version' => 1,
            'summary' => 'Example for adding other save buttons to page edit',
            'href' => 'http://www.processwire.com',
            'singular' => true,
            'autoload' => true
        );
    }
    /**
     * Initialize the module
     *
     * ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called
     * when ProcessWire's API is ready. As a result, this is a good place to attach hooks.
     *
     */
    public function init()
    {
        $this->addHookAfter("ProcessPageEdit::buildForm", $this, "addSaveButton");
        // tell processwire that this is a page save
        if ($this->input->post->submit_save_minor) {
            $this->input->post->submit_save = 1;
            // attach hook on page save
            $this->addHookAfter("Pages::saved", $this, "hookPageSave");
        }
    }
    public function hookPageSave($event)
    {
        $page = $event->arguments("page");
        if ($this->input->post->submit_save_minor) {
            // this will get saved after this saveReady hook so no need to save here            
            $message = __("Page was saved");
            $this->message($message);
            $pageid = $page->id;
            $goto   = wire("pages")->get("id=$pageid")->url; //get url of frontend article
            wire("session")->redirect($goto);
        }
    }
    public function addSaveButton($event)
    {
        $page = $event->object->getPage();
        if ($page->rootParent->id != "2") { //dont show on all pages which are under the admin section
            $form       = $event->return;
            $buttontext = __("Save and go to page");
            // new submit button
            $f          = $f2 = $this->modules->InputfieldSubmit;
            $f->attr("name", "submit_save_minor");
            $f->attr("value", $buttontext);
            $f2->attr("name", "submit_save_minor");
            $f2->attr("value", $buttontext);
            $f2->class .= ' ui-priority-secondary head_button_clone';
            // add submit button after the regular save button
            $form->insertAfter($f, $form->get("submit_save"));
            $form->insertAfter($f2, $form->get("submit_save"));
        }
    }
}
  • Like 2
Link to comment
Share on other sites

  • 1 month later...

Thanks for posting this Juergen, just needed a second button, too  ^_^

As far as I know (and for me it works) you don't need to include two buttons just stick to your first code with the class definition inserted like so for example..

$form       = $event->return;
$buttontext = __("Save and go to page");
$f          = $this->modules->InputfieldSubmit;
$f->attr("name", "submit_save_minor");
$f->attr("value", $buttontext);
$f->class .= ' ui-priority-secondary head_button_clone';
$form->insertAfter($f, $form->get("submit_save"));

the class is taking care of the duplication because the upper button/s are clone using jquery, thus "head_button_clone" tells the script to clone this button, too ;)  

  • Like 2
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...