Skip to content
Snippets Groups Projects
Commit 3a4ded72 authored by code-brighton's avatar code-brighton
Browse files

Start to user dependency injection rather than services

parent aa2489b3
No related branches found
No related tags found
No related merge requests found
......@@ -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_service->transform($tree, $manipulators);
$render_array = $menu_tree_service->build($tree);
$tree = $this->menuLinkTree->transform($tree, $manipulators);
$render_array = $this->menuLinkTree->build($tree);
$markup = \Drupal::service('renderer')->render($render_array);
$menu_title = trim($item->menu_title);
if ($menu_title) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment