joomla / joomla-cms

Home of the Joomla! Content Management System
https://www.joomla.org
GNU General Public License v2.0
4.69k stars 3.63k forks source link

Add Modal cancel functionality to core #42410

Open HLeithner opened 7 months ago

HLeithner commented 7 months ago

Is your feature request related to a problem? Please describe.

In core we use the layout modal in the cancel task in the controller for modalselect closing. This is implemented in several controllers. it would make sense to move this functionality to the FromController cancel method. This function already has similar functionality for redirection logic. Additionally in the postSaveHook in the same Controller is used for save operation redirection and should be added to the core at the same time.

Describe the solution you'd like

Move selectmodal functionality to FromController

Additional context

Something like that but a bit simplified:

    /**
     * Method to cancel an edit.
     *
     * @param   string  $key  The name of the primary key of the URL variable.
     *
     * @return  boolean  True if access level checks pass, false otherwise.
     *
     * @since _0_VERSION_0_
     */
    public function cancel($key = null)
    {
        $result = parent::cancel($key);

        // When editing in modal then redirect to modalreturn layout
        if ($result && $this->input->get('layout') === 'modal') {
            $id     = $this->input->get('_0_MVC_TABLE_SCHEMA_ID_0_', '');
            $return = 'index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($id, '_0_MVC_TABLE_SCHEMA_ID_0_')
                . '&layout=modalreturn&from-task=cancel';

            $this->setRedirect(Route::_($return, false));
        }

        return $result;
    }

    /**
     * Function that allows child controller access to model data
     * after the data has been saved.
     *
     * @param   BaseDatabaseModel  $model      The data model object.
     * @param   array              $validData  The validated data.
     *
     * @return  void
     *
     * @since _0_VERSION_0_
     */
    protected function postSaveHook(BaseDatabaseModel $model, $validData = [])
    {
        // When editing in modal then redirect to modalreturn layout
        if ($this->input->get('layout') === 'modal' && $this->task === 'save') {
            $id     = $model->getState('_0_MVC_EDIT_0_.id');
            $return = 'index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($id, '_0_MVC_TABLE_SCHEMA_ID_0_')
                . '&layout=modalreturn&from-task=save';

            $this->setRedirect(Route::_($return, false));
        }
    }
HLeithner commented 7 months ago

Additonal the addModalToolbar() method in the HtmlView could be added to FormView as well. Example code:


    /**
     * Add the page title and toolbar.
     *
     * @return  void
     */
    protected function addToolbar()
    {
        // We don't need toolbar in the modal window.
        if ($this->getLayout() === 'modal') {
            $this->addModalToolbar();
            return;
        }

        parent::addToolbar();

        ToolbarHelper::inlinehelp();
    }

    /**
     * Add the page title and toolbar.
     *
     * @return  void
     */
    protected function addModalToolbar()
    {
        $user       = $this->getCurrentUser();
        $isNew      = empty($this->item->{$this->keyName});
        $canDo      = $this->canDo;
        $toolbar    = $this->document->getToolbar();

        $canCreate = $isNew && (\count($user->getAuthorisedCategories($this->option, 'core.create')) > 0);
        $canEdit   = $canDo->get('core.edit');

        if ($canCreate || $canEdit) {
            $toolbar->apply('_0LC_MVC_EDIT_0_.apply');
            $toolbar->save('_0LC_MVC_EDIT_0_.save');
        }

        $toolbar->cancel('_0LC_MVC_EDIT_0_.cancel');
    }
rdeutz commented 2 months ago

Sounds like a good idea