MenuLinkPluginConverter.php

Same filename in this branch
  1. 10 core/lib/Drupal/Core/ProxyClass/ParamConverter/MenuLinkPluginConverter.php
Same filename in other branches
  1. 9 core/lib/Drupal/Core/ProxyClass/ParamConverter/MenuLinkPluginConverter.php
  2. 9 core/lib/Drupal/Core/ParamConverter/MenuLinkPluginConverter.php
  3. 8.9.x core/lib/Drupal/Core/ProxyClass/ParamConverter/MenuLinkPluginConverter.php
  4. 8.9.x core/lib/Drupal/Core/ParamConverter/MenuLinkPluginConverter.php
  5. 11.x core/lib/Drupal/Core/ProxyClass/ParamConverter/MenuLinkPluginConverter.php
  6. 11.x core/lib/Drupal/Core/ParamConverter/MenuLinkPluginConverter.php

Namespace

Drupal\Core\ParamConverter

File

core/lib/Drupal/Core/ParamConverter/MenuLinkPluginConverter.php

View source
<?php

namespace Drupal\Core\ParamConverter;

use Drupal\Core\Menu\MenuLinkManagerInterface;
use Drupal\Component\Plugin\Exception\PluginException;
use Symfony\Component\Routing\Route;

/**
 * Parameter converter for upcasting entity ids to full objects.
 */
class MenuLinkPluginConverter implements ParamConverterInterface {
    
    /**
     * Plugin manager which creates the instance from the value.
     *
     * @var \Drupal\Core\Menu\MenuLinkManagerInterface
     */
    protected $menuLinkManager;
    
    /**
     * Constructs a new MenuLinkPluginConverter.
     *
     * @param \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager
     *   The menu link plugin manager.
     */
    public function __construct(MenuLinkManagerInterface $menu_link_manager) {
        $this->menuLinkManager = $menu_link_manager;
    }
    
    /**
     * {@inheritdoc}
     */
    public function convert($value, $definition, $name, array $defaults) {
        if ($value) {
            try {
                return $this->menuLinkManager
                    ->createInstance($value);
            } catch (PluginException $e) {
                // Suppress the error.
            }
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function applies($definition, $name, Route $route) {
        return !empty($definition['type']) && $definition['type'] === 'menu_link_plugin';
    }

}

Classes

Title Deprecated Summary
MenuLinkPluginConverter Parameter converter for upcasting entity ids to full objects.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.