Skip to content
All gists
Back to GitHub
Sign in
Sign up
Sign in
Sign up
{{ message }}
Instantly share code, notes, and snippets.
volandku
/
createMenuItem.php
Forked from
kernusr/createMenuItem.php
Created
Jul 5, 2019
Star
1
Fork
0
Star
Code
Revisions
5
Stars
1
Embed
What would you like to do?
Embed
Embed this gist in your website.
Share
Copy sharable link for this gist.
Clone via HTTPS
Clone with Git or checkout with SVN using the repository’s web address.
Learn more about clone URLs
Download ZIP
Programmatically add menu item in Joomla!
Raw
createMenuItem.php
<?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
You can’t perform that action at this time.
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.