Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
field_menu-3440919
Manage
Activity
Members
Labels
Plan
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
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
Issue forks
field_menu-3440919
Commits
3a4ded72
Commit
3a4ded72
authored
4 years ago
by
code-brighton
Browse files
Options
Downloads
Patches
Plain Diff
Start to user dependency injection rather than services
parent
aa2489b3
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Plugin/Field/FieldFormatter/MenuTreeFormatter.php
+56
-5
56 additions, 5 deletions
src/Plugin/Field/FieldFormatter/MenuTreeFormatter.php
with
56 additions
and
5 deletions
src/Plugin/Field/FieldFormatter/MenuTreeFormatter.php
+
56
−
5
View file @
3a4ded72
...
...
@@ -5,6 +5,10 @@ namespace Drupal\field_menu\Plugin\Field\FieldFormatter;
use
Drupal\Core\Field\FormatterBase
;
use
Drupal\Core\Field\FieldItemListInterface
;
use
Drupal\Core\Menu\MenuTreeParameters
;
use
Drupal\Core\Field\FieldDefinitionInterface
;
use
Drupal\Core\Menu\MenuLinkTreeInterface
;
use
Drupal\Core\Plugin\ContainerFactoryPluginInterface
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
/**
* Plugin implementation of the 'field_menu_tree_formatter' formatter.
...
...
@@ -18,7 +22,55 @@ use Drupal\Core\Menu\MenuTreeParameters;
* }
* )
*/
class
MenuTreeFormatter
extends
FormatterBase
{
class
MenuTreeFormatter
extends
FormatterBase
implements
ContainerFactoryPluginInterface
{
/**
* The menu link tree service.
*
* @var \Drupal\Core\Menu\MenuLinkTreeInterface
*/
protected
$menuLinkTree
;
/**
* Constructs a MenuTreeFormatter instance.
*
* @param string $plugin_id
* The plugin_id for the formatter.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The definition of the field to which the formatter is associated.
* @param array $settings
* The formatter settings.
* @param string $label
* The formatter label display setting.
* @param string $view_mode
* The view mode.
* @param array $third_party_settings
* Any third party settings settings.
* @param \Drupal\Core\Menu\MenuLinkTreeInterface $menu_link_tree
* The menu link tree.
*/
public
function
__construct
(
$plugin_id
,
$plugin_definition
,
FieldDefinitionInterface
$field_definition
,
array
$settings
,
$label
,
$view_mode
,
array
$third_party_settings
,
MenuLinkTreeInterface
$menu_link_tree
)
{
parent
::
__construct
(
$plugin_id
,
$plugin_definition
,
$field_definition
,
$settings
,
$label
,
$view_mode
,
$third_party_settings
);
$this
->
menuLinkTree
=
$menu_link_tree
;
}
/**
* {@inheritdoc}
*/
public
static
function
create
(
ContainerInterface
$container
,
array
$configuration
,
$plugin_id
,
$plugin_definition
)
{
return
new
static
(
$plugin_id
,
$plugin_definition
,
$configuration
[
'field_definition'
],
$configuration
[
'settings'
],
$configuration
[
'label'
],
$configuration
[
'view_mode'
],
$configuration
[
'third_party_settings'
],
$container
->
get
(
'menu.link_tree'
)
);
}
/**
* {@inheritdoc}
...
...
@@ -45,8 +97,7 @@ class MenuTreeFormatter extends FormatterBase {
$menu_parameters
->
excludeRoot
();
}
$menu_tree_service
=
\Drupal
::
service
(
'menu.link_tree'
);
$tree
=
$menu_tree_service
->
load
(
$menu_name
,
$menu_parameters
);
$tree
=
$this
->
menuLinkTree
->
load
(
$menu_name
,
$menu_parameters
);
$manipulators
=
[
[
'callable'
=>
'menu.default_tree_manipulators:checkNodeAccess'
],
...
...
@@ -54,8 +105,8 @@ class MenuTreeFormatter extends FormatterBase {
[
'callable'
=>
'menu.default_tree_manipulators:generateIndexAndSort'
],
];
$tree
=
$
menu_tree_servic
e
->
transform
(
$tree
,
$manipulators
);
$render_array
=
$
menu_tree_servic
e
->
build
(
$tree
);
$tree
=
$
this
->
menuLinkTre
e
->
transform
(
$tree
,
$manipulators
);
$render_array
=
$
this
->
menuLinkTre
e
->
build
(
$tree
);
$markup
=
\Drupal
::
service
(
'renderer'
)
->
render
(
$render_array
);
$menu_title
=
trim
(
$item
->
menu_title
);
if
(
$menu_title
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment