Skip to content

Instantly share code, notes, and snippets.

@kernusr
Last active July 5, 2019 07:59
Show Gist options
  • Save kernusr/7e73f93050a3968af3695e5b579522f6 to your computer and use it in GitHub Desktop.
Save kernusr/7e73f93050a3968af3695e5b579522f6 to your computer and use it in GitHub Desktop.
Programmatically add menu item in Joomla!
<?php
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Form;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\Response\JsonResponse;
use Joomla\CMS\Table\Table;
protected function addMenuItem($data){
$app = Factory::getApplication();
// Ключевые пути для модели
BaseDatabaseModel::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_menus/models');
Form::addFormPath(JPATH_ADMINISTRATOR . '/components/com_menus/models/forms');
Table::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_menus/tables');
// Дёргаем модель com_menu.item
/* @var MenusModelItem $menuItem */
$menuItem = BaseDatabaseModel::getInstance('Item', 'MenusModel');
// Не знаю, зачем это, но без него не работает
if ($data['menutype'] !== '')
{
$app->input->set('menutype', $data['menutype']);
}
$form = $menuItem->getForm($data);
//Если не прошла валидация или не удалось сохранить пункт - выводим ошибку и отключаемся
if(!$data = $menuItem->validate($form, $data) || !$menuItem->save($data)){
@ob_end_clean();
echo new JsonResponse(null, $menuItem->getError(), true);
die();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment