Skip to content

Commit

Permalink
Category field modal return
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedik committed Nov 6, 2023
1 parent 8850c6a commit b246340
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Joomla\CMS\MVC\Controller\FormController;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Versioning\VersionableControllerTrait;
use Joomla\Input\Input;
use Joomla\Registry\Registry;
Expand Down Expand Up @@ -152,6 +153,15 @@ public function cancel($key = null)
$newKey = $this->option . '.edit.category.' . substr($this->extension, 4) . '.data';
$this->app->setUserState($newKey, null);

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

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

return $result;
}

Expand Down Expand Up @@ -243,5 +253,14 @@ protected function postSaveHook(BaseDatabaseModel $model, $validData = [])
$registry = new Registry($item->metadata);
$item->metadata = (string) $registry;
}

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

$this->setRedirect(Route::_($return, false));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ public function display($tpl = null)
$this->canDo = ContentHelper::getActions($this->state->get('category.component'), $section . 'category', $this->item->id);
$this->assoc = $this->get('Assoc');

if ($this->getLayout() === 'modalreturn') {
parent::display($tpl);

return;
}

// Check for errors.
if (\count($errors = $this->get('Errors'))) {
throw new GenericDataException(implode("\n", $errors), 500);
Expand Down Expand Up @@ -119,6 +125,8 @@ public function display($tpl = null)

if ($this->getLayout() !== 'modal') {
$this->addToolbar();
} else {
$this->addModalToolbar();
}

parent::display($tpl);
Expand Down Expand Up @@ -292,4 +300,61 @@ function (Toolbar $childBar) use ($checkedOut, $canDo, $itemEditable) {

$toolbar->help($ref_key, $componentParams->exists('helpURL'), $url, $component);
}

/**
* Add the modal toolbar.
*
* @return void
*
* @since __DEPLOY_VERSION__
*
* @throws \Exception
*/
protected function addModalToolbar()
{
$extension = Factory::getApplication()->getInput()->get('extension');
$user = $this->getCurrentUser();
$userId = $user->id;
$isNew = ($this->item->id == 0);
$toolbar = Toolbar::getInstance();

// Avoid nonsense situation.
if ($extension == 'com_categories') {
return;
}

// The extension can be in the form com_foo.section
$parts = explode('.', $extension);
$component = $parts[0];

// Need to load the menu language file as mod_menu hasn't been loaded yet.
$lang = $this->getLanguage();
$lang->load($component, JPATH_BASE)
|| $lang->load($component, JPATH_ADMINISTRATOR . '/components/' . $component);

// Build the actions for new and existing records.
$canDo = $this->canDo;

// Load specific css component
/** @var \Joomla\CMS\WebAsset\WebAssetManager $wa */
$wa = $this->getDocument()->getWebAssetManager();
$wa->getRegistry()->addExtensionRegistryFile($component);

if ($wa->assetExists('style', $component . '.admin-categories')) {
$wa->useStyle($component . '.admin-categories');
} else {
$wa->registerAndUseStyle($component . '.admin-categories', $component . '/administrator/categories.css');
}

$canCreate = $isNew;
$canEdit = $canDo->get('core.edit') || ($canDo->get('core.edit.own') && $this->item->created_user_id == $userId);

// For new records, check the create permission.
if ($canCreate || $canEdit) {
$toolbar->apply('category.apply');
$toolbar->save('category.save');
}

$toolbar->cancel('category.cancel');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

defined('_JEXEC') or die;
?>
<div class="subhead noshadow mb-3">
<?php echo $this->document->getToolbar('toolbar')->render(); ?>
</div>
<div class="container-popup">
<?php $this->setLayout('edit'); ?>
<?php echo $this->loadTemplate(); ?>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* @package Joomla.Administrator
* @subpackage com_content
*
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('_JEXEC') or die;

use Joomla\Component\Content\Site\Helper\RouteHelper;

$icon = 'icon-check';
$title = $this->item ? $this->item->title : '';
$content = $this->item ? $this->item->alias : '';
$data = ['contentType' => $this->state->get('category.component') . '.category'];

if ($this->item) {
$data['id'] = $this->item->id;
$data['title'] = $this->item->title;
$data['uri'] = RouteHelper::getCategoryRoute($this->item->id, $this->item->language);
}

// Add Content select script
/** @var Joomla\CMS\WebAsset\WebAssetManager $wa */
$wa = $this->document->getWebAssetManager();
$wa->useScript('modal-content-select');

// The data for Content select script
$this->document->addScriptOptions('content-select-on-load', $data, false);

?>

<div class="px-4 py-5 my-5 text-center">
<span class="fa-8x mb-4 <?php echo $icon; ?>" aria-hidden="true"></span>
<h1 class="display-5 fw-bold"><?php echo $title; ?></h1>
<div class="col-lg-6 mx-auto">
<p class="lead mb-4">
<?php echo $content; ?>
</p>
</div>
</div>

0 comments on commit b246340

Please sign in to comment.