Jump to content

Module to add 2 additional save buttons and unpublish button to page edit


Juergen
 Share

Recommended Posts

Today I want to share a little module that adds 2 additional save buttons with redirect and 1 unpublish button to the page edit.

2 additional save buttons:

My intention was that it would be nice if someone saves an article in the backend and will be redirected after saving directly to the frontend page of the article. This module adds 1additional save button at the bottom next to the default save button and 1 at the top. So you can choose if you want to save the article with the default save button or you will save it with the custom save button and you will get redirected to the frontend article.

1 unpublish button:

The idea behind this was that I want to disable the setting tab for non superuser. The problem was if I hide it, then non superusers are no longer able to unpublish an article.

Therefore this module adds an additional unpublish button at the bottom - the user clicks it and the page will be saved with status unpublished.

All pages under the admin section will not be affected of this module.

Module is multilingual, so you can set the button texts in all languages.

Top view page status published:

post-2257-0-69050200-1455041771_thumb.pn

Bottom view page status published:

post-2257-0-77381200-1455041807_thumb.pn

Bottom view page status unpublished:

post-2257-0-60266900-1455041842_thumb.pn

Here is the code:

<?php
/**
 * Adding 2 additional save buttons with redirect to frontend and 1 unpublish button 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 CustomPageSaveAndUnpublish 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' => 'Custom page save and unpublish module',
            'version' => 1,
            'summary' => 'Example for adding 2 additional save buttons with redirect and 1 unpublish button 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");
        $this->addHookAfter("ProcessPageEdit::buildForm", $this, "addUnpublishButton");
        // 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");
        }
        if ($this->input->post->submit_unpublish) {
            $this->input->post->submit_save = 1;
            // attach hook on page save
            $this->addHookAfter("Pages::saveReady", $this, "hookPageSaveReadyUnpublish");
        }
    }
    public function hookPageSave($event)
    {
        //function to redirect to the frontend after save
        $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            
            $pageid = $page->id;
            $goto   = wire("pages")->get("id=$pageid")->url; //get url of frontend article
            wire("session")->redirect($goto);
        }
    }
    public function hookPageSaveReadyUnpublish($event)
    {
        //function to change the status to unpublished 
        $page             = $event->arguments("page");
        $status           = $page->status;
        $unpublishmessage = __("Status of the page is set to unpublished");
        if ($this->input->post->submit_unpublish) {
            if ($status == 1) {
                $page->status = "2049";
                $this->message($unpublishmessage);
            }
        }
    }
    public function addSaveButton($event)
    {
        //function to add the 2 additional save button with redirect at the top and at the bottom
        $page   = $event->object->getPage();
        $status = $page->status;
        if (($page->rootParent->id != "2") AND ($status == 1)) { //dont show on all pages which are under the admin section and which are not published
            $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 only if page is published            
            $form->insertAfter($f, $form->get("submit_save"));
            $form->insertAfter($f2, $form->get("submit_save"));
        }
    }
    public function addUnpublishButton($event)
    {
        //function to add the unpublish button at the bottom if page has status published
        $page = $event->object->getPage();
        if ($page->rootParent->id != 2) { //dont show on all pages which are under the admin and dont show the button under the delete tab
            $form                = $event->return;
            $unpublishbuttontext = __("Unpublish");
            // new submit button
            $f                   = $this->modules->InputfieldSubmit;
            $f->attr("name", "submit_unpublish");
            $f->attr("value", $unpublishbuttontext);
            // add unpublish button after the save button
            if ($page->status == 1) {
                $form->insertAfter($f, $form->get("submit_save"));
            }
        }
    }
}

Everybody who is interested can download the modul here:

CustomPageSaveAndUnpublish.zip

Best regards Jürgen

  • Like 11
  • Thanks 1
Link to comment
Share on other sites

  • 1 month later...
  • 2 years later...
On 3/21/2016 at 5:09 PM, Juergen said:

Glad to hear that :)! Now the "save the page and go to the frontend" button is not necessary anymore if you use PW3, because this functionality is integrated in the core.

Newbie question: where? How to access it?

Your module looks to be just what I need. I recently converted my brass band's site to Processwire (from Clipper, using Latte templating, but that's another story), and am extending it to allow a private Members area. I'd like some others to be able to add notices about rehearsals etc. I've managed to make a simple front-end form that lets them create a new post comfortably from the front end, entering date and a brief title; it then takes them to the back end if required to add full details and file attachments. I want them to get back to the main members page on completion, so the module looks a good fit for that. I'm just curious what and where the built-in functionality is.

Link to comment
Share on other sites

  • 3 weeks later...
  • 2 weeks later...

Thanks for the tip! The big bold button is what I need.

Site is Ascot Brass, which I converted from a gruesome heap of tables within tables to Clipper a couple of years ago when asked to become webmaster. I decided to try it out as my first Clipper to PW conversion when faced with creating the Members page. Use of templating in Clipper made it reasonably easy to do the original conversion; and I've found the TemplateLatteReplace module suits my way of working in PW.

KP

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

Thx @Juergen that also helped me today.

This is what I came up with (for future reference) when I needed to add a page clone button:

  /**
   * Add clone button to form
   */
  public function addCloneButton() {

    // add button to form
    $this->addHookAfter("ProcessPageEdit::buildForm", function($event) {
      $form = $event->arguments(0);
      $form->add([
        'type' => 'submit',
        'name' => 'btn_clone_plan',
        'value' => __('Clone'),
        'icon' => 'clone',
      ]);
      $form->get('btn_clone_plan')->addClass('ui-priority-secondary');
    });

    // process input
    $this->addHookAfter("ProcessPageEdit::processInput", function($event) {
      $form = $event->arguments(0);
      if($form != "InputfieldForm") return; // dont fire on inputfields
      if($this->input->post('btn_clone_plan')) {
        // tell PW to save the page
        $this->input->post->submit_save = 1;
      }
      if(count($form->getErrors())) {
        // clone only if there are no errors, otherwise do a regular save
        unset($this->input->post->btn_clone_plan);
      }
    });

    // do the clone
    $this->addHookAfter("Pages::saved", function($event) {
      if(!$this->input->post->btn_clone_plan) return;
      unset($this->input->post->btn_clone_plan); // prevent endless loop!
      $clone = $this->pages->clone($event->arguments(0));
      $this->session->redirect($clone->editUrl);
    });
  }

PS: This approach might be easier, but I wanted an extra button:

 

  • Like 4
Link to comment
Share on other sites

  • 2 years later...
$this->addHookAfter("ProcessPageEdit::buildForm", $this, "addSaveButton");

public function addSaveButton($event){
        $page   = $event->object->getPage();
        $status = $page->status;
        if (($page->rootParent->id != "2") AND ($status == 1)) { 
            if ($page->status == 1) {
              $form       = $event->return;
              $form->add([
              'type' => 'button',
              'name' => 'btn_pdf_plan',
              'id' => 'btn_pdf_show',
              'value' => __('Pdf'),
              'icon' => 'clone',
              ]);

				$form->get('btn_pdf_plan')->addClass('ui-priority-secondary');

....

I have used this code for create a button in the admin page area.
This work fine, but i want t move the button in the top of the form: the code append the button to end of the page...

How it is possible to force the position before the save button at top of the admin page

If i use this code

$p = $this->modules->InputfieldButton;
            $p->attr("name", "pdf_show");
            $p->attr("id", "btn_pdf_show");
            $p->attr("value", "Pdf");
            $p->class .= ' ui-priority-secondary head_button_clone';
            if ($page->status == 1) {
                $form->insertAfter($p, $form->get("submit_save"));
            }

i get 2 button, but i need only one button at the top of the page

Thank you in advance

Link to comment
Share on other sites

Hello @abmcr

Why do you not extend the button drop down with your pdf feature instead of adding a new button.

test.jpg.5e43f680c07355b93afd7cc067d2585a.jpg

BTW: Adding a second save button was the way to go a at the time where PW did not offer the possibility for a drop down button.

To get an additional dropdown level for PDF, please put the following piece of code inside your site/templates/init.php or site/templates/ready.php. If the files do not exist by default, you have to create them manually.

$wire->addHookAfter('ProcessPageEdit::getSubmitActions', function($event) {
    $actions = $event->return;
    $actions[] = [
        'value' => 'value of action, i.e. view, edit, add, etc',
        'icon' => 'file-pdf-o',
        'label' => 'Pdf',
        'class' => 'btn_pdf_plan',
    ];
    $event->return = $actions;
});

Only to mention: do not forget to change the value to your needs (fe save in your case).

Best regards

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