Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
ui_patterns_settings
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
ui_patterns_settings
Merge requests
!11
Issue
#3345071
: Add menu setting type
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Issue
#3345071
: Add menu setting type
issue/ui_patterns_settings-3345071:3345071-add-menu-setting
into
8.x-2.x
Overview
4
Commits
6
Pipelines
0
Changes
3
3 unresolved threads
Hide all comments
Open
Florent Torregrosa
requested to merge
issue/ui_patterns_settings-3345071:3345071-add-menu-setting
into
8.x-2.x
2 years ago
Overview
4
Commits
6
Pipelines
0
Changes
3
3 unresolved threads
Hide all comments
Expand
0
0
Merge request reports
Compare
8.x-2.x
version 7
e750c8b5
1 year ago
version 6
fee68f36
1 year ago
version 5
ec12b4b5
1 year ago
version 4
4bde524b
1 year ago
version 3
4fc1804b
1 year ago
version 2
bfc98d7b
2 years ago
version 1
38a256e4
2 years ago
8.x-2.x (HEAD)
and
latest version
latest version
bb799636
6 commits,
1 year ago
version 7
e750c8b5
5 commits,
1 year ago
version 6
fee68f36
5 commits,
1 year ago
version 5
ec12b4b5
4 commits,
1 year ago
version 4
4bde524b
3 commits,
1 year ago
version 3
4fc1804b
3 commits,
1 year ago
version 2
bfc98d7b
2 commits,
2 years ago
version 1
38a256e4
1 commit,
2 years ago
3 files
+
423
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
3
Search (e.g. *.vue) (Ctrl+P)
src/Helpers/Menu.php
0 → 100644
+
131
−
0
Options
<?php
namespace
Drupal\ui_patterns_settings\Helpers
;
use
Drupal\Core\Template\Attribute
;
use
Drupal\Core\Url
;
/**
*
*/
class
Menu
{
/**
* Normalize menu items.
*
* Don't inject URL object into patterns templates, use "title" as item
* label and "url" as item target.
*
* @param array $items
* The items to convert.
*
* @return array
*/
public
static
function
normalize
(
array
$items
):
array
{
foreach
(
$items
as
$index
=>
&
$item
)
{
if
(
!
is_array
(
$item
))
{
unset
(
$items
[
$index
]);
continue
;
}
if
(
array_key_exists
(
"text"
,
$item
))
{
// Examples: links.html.twig, breadcrumb.html.twig, pager.html.twig,
// views_mini_pager.html.twig.
$item
[
"title"
]
=
$item
[
"text"
];
unset
(
$item
[
"text"
]);
}
if
(
!
array_key_exists
(
"title"
,
$item
))
{
$item
[
"title"
]
=
$index
;
}
if
(
array_key_exists
(
"href"
,
$item
))
{
// Examples: pager.html.twig, views_mini_pager.html.twig.
$item
[
"url"
]
=
$item
[
"href"
];
unset
(
$item
[
"href"
]);
}
if
(
!
isset
(
$item
[
"url"
])
&&
isset
(
$item
[
"link"
]))
{
// Example: links.html.twig.
$item
[
"url"
]
=
$item
[
"link"
][
"#url"
];
$item
[
"url"
]
->
setOptions
(
$item
[
"link"
][
"#options"
]);
unset
(
$item
[
"link"
]);
}
if
(
array_key_exists
(
"url"
,
$item
)
&&
(
$item
[
"url"
]
instanceof
Url
))
{
// Examples: menu.html.twig, links.html.twig.
$url
=
$item
[
"url"
];
$item
[
"url"
]
=
$url
->
toString
();
$options
=
$url
->
getOptions
();
if
(
isset
(
$options
[
"attributes"
]))
{
$item
[
"link_attributes"
]
=
new
Attribute
(
$options
[
"attributes"
]);
}
}
if
(
array_key_exists
(
"below"
,
$item
))
{
$item
[
"below"
]
=
self
::
normalize
(
$item
[
"below"
]);
}
}
$items
=
array_values
(
$items
);
return
$items
;
}
/**
* Convert pager to menu.
*
* Convert pager data structure to menu data structure. Useful for
* pager.html.twig presenter template.
*
* @param array $items
* The pager items to convert.
* @param int $current
* The current page.
*
* @return array
*/
public
static
function
convertPagerToMenu
(
array
$pager
,
int
$current
):
array
{
$items
=
[];
if
(
isset
(
$pager
[
"first"
]))
{
$items
[]
=
$pager
[
"first"
];
}
if
(
isset
(
$pager
[
"previous"
]))
{
$items
[]
=
$pager
[
"previous"
];
}
if
(
isset
(
$pager
[
"pages"
]))
{
foreach
(
$pager
[
"pages"
]
as
$index
=>
$item
)
{
$item
[
"text"
]
=
$index
;
if
(
$index
==
$current
)
{
unset
(
$item
[
"href"
]);
}
$items
[]
=
$item
;
}
}
if
(
isset
(
$pager
[
"next"
]))
{
$items
[]
=
$pager
[
"next"
];
}
if
(
isset
(
$pager
[
"last"
]))
{
$items
[]
=
$pager
[
"last"
];
}
return
$items
;
}
/**
* Convert mini pager to menu.
*
* Convert views mini pager data structure to menu data structure. Useful for
* views-mini-pager.html.twig presenter template.
*
* @param array $items
* The pager items to convert.
*
* @return array
*/
public
static
function
convertMiniPagerToMenu
(
array
$pager
):
array
{
$items
=
[];
if
(
$pager
[
"previous"
])
{
$items
[]
=
$pager
[
"previous"
];
}
$items
[]
=
[
"text"
=>
$pager
[
"current"
],
];
if
(
$pager
[
"next"
])
{
$items
[]
=
$pager
[
"next"
];
}
return
$items
;
}
}
Loading