Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176
This commit is contained in:
commit
9921556621
13277 changed files with 1459781 additions and 0 deletions
276
core/lib/Drupal/Core/Controller/ControllerBase.php
Normal file
276
core/lib/Drupal/Core/Controller/ControllerBase.php
Normal file
|
@ -0,0 +1,276 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\Controller\ControllerBase.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\Controller;
|
||||
|
||||
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
|
||||
use Drupal\Core\Routing\LinkGeneratorTrait;
|
||||
use Drupal\Core\Routing\RedirectDestinationTrait;
|
||||
use Drupal\Core\Routing\UrlGeneratorTrait;
|
||||
use Drupal\Core\StringTranslation\StringTranslationTrait;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Utility base class for thin controllers.
|
||||
*
|
||||
* Controllers that use this base class have access to a number of utility
|
||||
* methods and to the Container, which can greatly reduce boilerplate dependency
|
||||
* handling code. However, it also makes the class considerably more
|
||||
* difficult to unit test. Therefore this base class should only be used by
|
||||
* controller classes that contain only trivial glue code. Controllers that
|
||||
* contain sufficiently complex logic that it's worth testing should not use
|
||||
* this base class but use ContainerInjectionInterface instead, or even better be
|
||||
* refactored to be trivial glue code.
|
||||
*
|
||||
* The services exposed here are those that it is reasonable for a well-behaved
|
||||
* controller to leverage. A controller that needs other services may
|
||||
* need to be refactored into a thin controller and a dependent unit-testable
|
||||
* service.
|
||||
*
|
||||
* @see \Drupal\Core\DependencyInjection\ContainerInjectionInterface
|
||||
*
|
||||
* @ingroup menu
|
||||
*/
|
||||
abstract class ControllerBase implements ContainerInjectionInterface {
|
||||
|
||||
use LinkGeneratorTrait;
|
||||
use RedirectDestinationTrait;
|
||||
use StringTranslationTrait;
|
||||
use UrlGeneratorTrait;
|
||||
|
||||
/**
|
||||
* The entity manager.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityManagerInterface
|
||||
*/
|
||||
protected $entityManager;
|
||||
|
||||
/**
|
||||
* The entity form builder.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityFormBuilderInterface
|
||||
*/
|
||||
protected $entityFormBuilder;
|
||||
|
||||
/**
|
||||
* The language manager.
|
||||
*
|
||||
* @var \Drupal\Core\Language\LanguageManagerInterface
|
||||
*/
|
||||
protected $languageManager;
|
||||
|
||||
/**
|
||||
* The configuration factory.
|
||||
*
|
||||
* @var \Drupal\Core\Config\Config
|
||||
*/
|
||||
protected $configFactory;
|
||||
|
||||
/**
|
||||
* The key-value storage.
|
||||
*
|
||||
* @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
|
||||
*/
|
||||
protected $keyValue;
|
||||
|
||||
/**
|
||||
* The current user service.
|
||||
*
|
||||
* @var \Drupal\Core\Session\AccountInterface
|
||||
*/
|
||||
protected $currentUser;
|
||||
|
||||
/**
|
||||
* The state service.
|
||||
*
|
||||
* @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
|
||||
*/
|
||||
protected $stateService;
|
||||
|
||||
/**
|
||||
* The module handler.
|
||||
*
|
||||
* @var \Drupal\Core\Extension\ModuleHandlerInterface
|
||||
*/
|
||||
protected $moduleHandler;
|
||||
|
||||
/**
|
||||
* The form builder.
|
||||
*
|
||||
* @var \Drupal\Core\Form\FormBuilderInterface
|
||||
*/
|
||||
protected $formBuilder;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the entity manager service.
|
||||
*
|
||||
* @return \Drupal\Core\Entity\EntityManagerInterface
|
||||
* The entity manager service.
|
||||
*/
|
||||
protected function entityManager() {
|
||||
if (!$this->entityManager) {
|
||||
$this->entityManager = $this->container()->get('entity.manager');
|
||||
}
|
||||
return $this->entityManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the entity form builder.
|
||||
*
|
||||
* @return \Drupal\Core\Entity\EntityFormBuilderInterface
|
||||
* The entity form builder.
|
||||
*/
|
||||
protected function entityFormBuilder() {
|
||||
if (!$this->entityFormBuilder) {
|
||||
$this->entityFormBuilder = $this->container()->get('entity.form_builder');
|
||||
}
|
||||
return $this->entityFormBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the requested cache bin.
|
||||
*
|
||||
* @param string $bin
|
||||
* (optional) The cache bin for which the cache object should be returned,
|
||||
* defaults to 'default'.
|
||||
*
|
||||
* @return \Drupal\Core\Cache\CacheBackendInterface
|
||||
* The cache object associated with the specified bin.
|
||||
*/
|
||||
protected function cache($bin = 'default') {
|
||||
return $this->container()->get('cache.' . $bin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a configuration object.
|
||||
*
|
||||
* This is the main entry point to the configuration API. Calling
|
||||
* @code $this->config('book.admin') @endcode will return a configuration
|
||||
* object in which the book module can store its administrative settings.
|
||||
*
|
||||
* @param string $name
|
||||
* The name of the configuration object to retrieve. The name corresponds to
|
||||
* a configuration file. For @code \Drupal::config('book.admin') @endcode,
|
||||
* the config object returned will contain the contents of book.admin
|
||||
* configuration file.
|
||||
*
|
||||
* @return \Drupal\Core\Config\Config
|
||||
* A configuration object.
|
||||
*/
|
||||
protected function config($name) {
|
||||
if (!$this->configFactory) {
|
||||
$this->configFactory = $this->container()->get('config.factory');
|
||||
}
|
||||
return $this->configFactory->get($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a key/value storage collection.
|
||||
*
|
||||
* @param string $collection
|
||||
* Name of the key/value collection to return.
|
||||
*
|
||||
* @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
|
||||
*/
|
||||
protected function keyValue($collection) {
|
||||
if (!$this->keyValue) {
|
||||
$this->keyValue = $this->container()->get('keyvalue')->get($collection);
|
||||
}
|
||||
return $this->keyValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the state storage service.
|
||||
*
|
||||
* Use this to store machine-generated data, local to a specific environment
|
||||
* that does not need deploying and does not need human editing; for example,
|
||||
* the last time cron was run. Data which needs to be edited by humans and
|
||||
* needs to be the same across development, production, etc. environments
|
||||
* (for example, the system maintenance message) should use config() instead.
|
||||
*
|
||||
* @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
|
||||
*/
|
||||
protected function state() {
|
||||
if (!$this->stateService) {
|
||||
$this->stateService = $this->container()->get('state');
|
||||
}
|
||||
return $this->stateService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the module handler.
|
||||
*
|
||||
* @return \Drupal\Core\Extension\ModuleHandlerInterface
|
||||
*/
|
||||
protected function moduleHandler() {
|
||||
if (!$this->moduleHandler) {
|
||||
$this->moduleHandler = $this->container()->get('module_handler');
|
||||
}
|
||||
return $this->moduleHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the form builder service.
|
||||
*
|
||||
* @return \Drupal\Core\Form\FormBuilderInterface
|
||||
*/
|
||||
protected function formBuilder() {
|
||||
if (!$this->formBuilder) {
|
||||
$this->formBuilder = $this->container()->get('form_builder');
|
||||
}
|
||||
return $this->formBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current user.
|
||||
*
|
||||
* @return \Drupal\Core\Session\AccountInterface
|
||||
* The current user.
|
||||
*/
|
||||
protected function currentUser() {
|
||||
if (!$this->currentUser) {
|
||||
$this->currentUser = $this->container()->get('current_user');
|
||||
}
|
||||
return $this->currentUser;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the language manager service.
|
||||
*
|
||||
* @return \Drupal\Core\Language\LanguageManagerInterface
|
||||
* The language manager.
|
||||
*/
|
||||
protected function languageManager() {
|
||||
if (!$this->languageManager) {
|
||||
$this->languageManager = $this->container()->get('language_manager');
|
||||
}
|
||||
return $this->languageManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the service container.
|
||||
*
|
||||
* This method is marked private to prevent sub-classes from retrieving
|
||||
* services from the container through it. Instead,
|
||||
* \Drupal\Core\DependencyInjection\ContainerInjectionInterface should be used
|
||||
* for injecting services.
|
||||
*
|
||||
* @return \Symfony\Component\DependencyInjection\ContainerInterface $container
|
||||
* The service container.
|
||||
*/
|
||||
private function container() {
|
||||
return \Drupal::getContainer();
|
||||
}
|
||||
|
||||
}
|
161
core/lib/Drupal/Core/Controller/ControllerResolver.php
Normal file
161
core/lib/Drupal/Core/Controller/ControllerResolver.php
Normal file
|
@ -0,0 +1,161 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\Controller\ControllerResolver.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\Controller;
|
||||
|
||||
use Drupal\Core\Routing\RouteMatch;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\Controller\ControllerResolver as BaseControllerResolver;
|
||||
use Drupal\Core\DependencyInjection\ClassResolverInterface;
|
||||
|
||||
/**
|
||||
* ControllerResolver to enhance controllers beyond Symfony's basic handling.
|
||||
*
|
||||
* It adds two behaviors:
|
||||
*
|
||||
* - When creating a new object-based controller that implements
|
||||
* ContainerAwareInterface, inject the container into it. While not always
|
||||
* necessary, that allows a controller to vary the services it needs at
|
||||
* runtime.
|
||||
*
|
||||
* - By default, a controller name follows the class::method notation. This
|
||||
* class adds the possibility to use a service from the container as a
|
||||
* controller by using a service:method notation (Symfony uses the same
|
||||
* convention).
|
||||
*/
|
||||
class ControllerResolver extends BaseControllerResolver implements ControllerResolverInterface {
|
||||
|
||||
/**
|
||||
* The class resolver.
|
||||
*
|
||||
* @var \Drupal\Core\DependencyInjection\ClassResolverInterface
|
||||
*/
|
||||
protected $classResolver;
|
||||
|
||||
/**
|
||||
* Constructs a new ControllerResolver.
|
||||
*
|
||||
* @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
|
||||
* The class resolver.
|
||||
*/
|
||||
public function __construct(ClassResolverInterface $class_resolver) {
|
||||
$this->classResolver = $class_resolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getControllerFromDefinition($controller, $path = '') {
|
||||
if (is_array($controller) || (is_object($controller) && method_exists($controller, '__invoke'))) {
|
||||
return $controller;
|
||||
}
|
||||
|
||||
if (strpos($controller, ':') === FALSE) {
|
||||
if (function_exists($controller)) {
|
||||
return $controller;
|
||||
}
|
||||
elseif (method_exists($controller, '__invoke')) {
|
||||
return new $controller;
|
||||
}
|
||||
}
|
||||
|
||||
$callable = $this->createController($controller);
|
||||
|
||||
if (!is_callable($callable)) {
|
||||
throw new \InvalidArgumentException(sprintf('The controller for URI "%s" is not callable.', $path));
|
||||
}
|
||||
|
||||
return $callable;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getController(Request $request) {
|
||||
if (!$controller = $request->attributes->get('_controller')) {
|
||||
return FALSE;
|
||||
}
|
||||
return $this->getControllerFromDefinition($controller, $request->getPathInfo());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a callable for the given controller.
|
||||
*
|
||||
* @param string $controller
|
||||
* A Controller string.
|
||||
*
|
||||
* @return mixed
|
||||
* A PHP callable.
|
||||
*
|
||||
* @throws \LogicException
|
||||
* If the controller cannot be parsed
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
* If the controller class does not exist
|
||||
*/
|
||||
protected function createController($controller) {
|
||||
// Controller in the service:method notation.
|
||||
$count = substr_count($controller, ':');
|
||||
if ($count == 1) {
|
||||
list($class_or_service, $method) = explode(':', $controller, 2);
|
||||
}
|
||||
// Controller in the class::method notation.
|
||||
elseif (strpos($controller, '::') !== FALSE) {
|
||||
list($class_or_service, $method) = explode('::', $controller, 2);
|
||||
}
|
||||
else {
|
||||
throw new \LogicException(sprintf('Unable to parse the controller name "%s".', $controller));
|
||||
}
|
||||
|
||||
$controller = $this->classResolver->getInstanceFromDefinition($class_or_service);
|
||||
|
||||
return array($controller, $method);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doGetArguments(Request $request, $controller, array $parameters) {
|
||||
$attributes = $request->attributes->all();
|
||||
$raw_parameters = $request->attributes->has('_raw_variables') ? $request->attributes->get('_raw_variables') : [];
|
||||
$arguments = array();
|
||||
foreach ($parameters as $param) {
|
||||
if (array_key_exists($param->name, $attributes)) {
|
||||
$arguments[] = $attributes[$param->name];
|
||||
}
|
||||
elseif (array_key_exists($param->name, $raw_parameters)) {
|
||||
$arguments[] = $attributes[$param->name];
|
||||
}
|
||||
elseif ($param->getClass() && $param->getClass()->isInstance($request)) {
|
||||
$arguments[] = $request;
|
||||
}
|
||||
elseif ($param->getClass() && ($param->getClass()->name == 'Drupal\Core\Routing\RouteMatchInterface' || is_subclass_of($param->getClass()->name, 'Drupal\Core\Routing\RouteMatchInterface'))) {
|
||||
$arguments[] = RouteMatch::createFromRequest($request);
|
||||
}
|
||||
elseif ($param->isDefaultValueAvailable()) {
|
||||
$arguments[] = $param->getDefaultValue();
|
||||
}
|
||||
else {
|
||||
if (is_array($controller)) {
|
||||
$repr = sprintf('%s::%s()', get_class($controller[0]), $controller[1]);
|
||||
}
|
||||
elseif (is_object($controller)) {
|
||||
$repr = get_class($controller);
|
||||
}
|
||||
else {
|
||||
$repr = $controller;
|
||||
}
|
||||
|
||||
throw new \RuntimeException(sprintf('Controller "%s" requires that you provide a value for the "$%s" argument (because there is no default value or because there is a non optional argument after this one).', $repr, $param->name));
|
||||
}
|
||||
}
|
||||
return $arguments;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\Controller\ControllerResolverInterface.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\Controller;
|
||||
|
||||
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface as BaseControllerResolverInterface;
|
||||
|
||||
/**
|
||||
* Extends the ControllerResolverInterface from symfony.
|
||||
*/
|
||||
interface ControllerResolverInterface extends BaseControllerResolverInterface {
|
||||
|
||||
/**
|
||||
* Returns the Controller instance with a given controller route definition.
|
||||
*
|
||||
* As several resolvers can exist for a single application, a resolver must
|
||||
* return false when it is not able to determine the controller.
|
||||
*
|
||||
* @param mixed $controller
|
||||
* The controller attribute like in $request->attributes->get('_controller')
|
||||
*
|
||||
* @return mixed|bool
|
||||
* A PHP callable representing the Controller, or false if this resolver is
|
||||
* not able to determine the controller
|
||||
*
|
||||
* @throws \InvalidArgumentException|\LogicException
|
||||
* Thrown if the controller can't be found.
|
||||
*
|
||||
* @see \Symfony\Component\HttpKernel\Controller\ControllerResolverInterface::getController()
|
||||
*/
|
||||
public function getControllerFromDefinition($controller);
|
||||
|
||||
}
|
115
core/lib/Drupal/Core/Controller/FormController.php
Normal file
115
core/lib/Drupal/Core/Controller/FormController.php
Normal file
|
@ -0,0 +1,115 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\Controller\FormController.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\Controller;
|
||||
|
||||
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
|
||||
use Drupal\Core\Form\FormBuilderInterface;
|
||||
use Drupal\Core\Form\FormState;
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* Common base class for form interstitial controllers.
|
||||
*
|
||||
* @todo Make this a trait in PHP 5.4.
|
||||
*/
|
||||
abstract class FormController {
|
||||
use DependencySerializationTrait;
|
||||
|
||||
/**
|
||||
* The controller resolver.
|
||||
*
|
||||
* @var \Drupal\Core\Controller\ControllerResolverInterface
|
||||
*/
|
||||
protected $controllerResolver;
|
||||
|
||||
/**
|
||||
* The form builder.
|
||||
*
|
||||
* @var \Drupal\Core\Form\FormBuilderInterface
|
||||
*/
|
||||
protected $formBuilder;
|
||||
|
||||
/**
|
||||
* Constructs a new \Drupal\Core\Controller\FormController object.
|
||||
*
|
||||
* @param \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver
|
||||
* The controller resolver.
|
||||
* @param \Drupal\Core\Form\FormBuilderInterface $form_builder
|
||||
* The form builder.
|
||||
*/
|
||||
public function __construct(ControllerResolverInterface $controller_resolver, FormBuilderInterface $form_builder) {
|
||||
$this->controllerResolver = $controller_resolver;
|
||||
$this->formBuilder = $form_builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes the form and returns the result.
|
||||
*
|
||||
* @param \Symfony\Component\HttpFoundation\Request $request
|
||||
* The request object.
|
||||
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
|
||||
* The route match.
|
||||
*
|
||||
* @return array
|
||||
* The render array that results from invoking the controller.
|
||||
*/
|
||||
public function getContentResult(Request $request, RouteMatchInterface $route_match) {
|
||||
$form_arg = $this->getFormArgument($route_match);
|
||||
$form_object = $this->getFormObject($route_match, $form_arg);
|
||||
|
||||
// Add the form and form_state to trick the getArguments method of the
|
||||
// controller resolver.
|
||||
$form_state = new FormState();
|
||||
$request->attributes->set('form', []);
|
||||
$request->attributes->set('form_state', $form_state);
|
||||
$args = $this->controllerResolver->getArguments($request, [$form_object, 'buildForm']);
|
||||
$request->attributes->remove('form');
|
||||
$request->attributes->remove('form_state');
|
||||
|
||||
// Remove $form and $form_state from the arguments, and re-index them.
|
||||
unset($args[0], $args[1]);
|
||||
$form_state->addBuildInfo('args', array_values($args));
|
||||
|
||||
return $this->formBuilder->buildForm($form_object, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the form argument string from a request.
|
||||
*
|
||||
* Depending on the type of form the argument string may be stored in a
|
||||
* different request attribute.
|
||||
*
|
||||
* One example of a route definition is given below.
|
||||
* @code
|
||||
* defaults:
|
||||
* _form: Drupal\example\Form\ExampleForm
|
||||
* @endcode
|
||||
*
|
||||
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
|
||||
* The route match object from which to extract a form definition string.
|
||||
*
|
||||
* @return string
|
||||
* The form definition string.
|
||||
*/
|
||||
abstract protected function getFormArgument(RouteMatchInterface $route_match);
|
||||
|
||||
/**
|
||||
* Returns the object used to build the form.
|
||||
*
|
||||
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
|
||||
* The route match.
|
||||
* @param string $form_arg
|
||||
* Either a class name or a service ID.
|
||||
*
|
||||
* @return \Drupal\Core\Form\FormInterface
|
||||
* The form object to use.
|
||||
*/
|
||||
abstract protected function getFormObject(RouteMatchInterface $route_match, $form_arg);
|
||||
|
||||
}
|
55
core/lib/Drupal/Core/Controller/HtmlFormController.php
Normal file
55
core/lib/Drupal/Core/Controller/HtmlFormController.php
Normal file
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\Controller\HtmlFormController.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\Controller;
|
||||
|
||||
use Drupal\Core\Form\FormBuilderInterface;
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
use Drupal\Core\DependencyInjection\ClassResolverInterface;
|
||||
|
||||
/**
|
||||
* Wrapping controller for forms that serve as the main page body.
|
||||
*/
|
||||
class HtmlFormController extends FormController {
|
||||
|
||||
/**
|
||||
* The class resolver.
|
||||
*
|
||||
* @var \Drupal\Core\DependencyInjection\ClassResolverInterface;
|
||||
*/
|
||||
protected $classResolver;
|
||||
|
||||
/**
|
||||
* Constructs a new \Drupal\Core\Routing\Enhancer\FormEnhancer object.
|
||||
*
|
||||
* @param \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver
|
||||
* The controller resolver.
|
||||
* @param \Drupal\Core\Form\FormBuilderInterface $form_builder
|
||||
* The form builder.
|
||||
* @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
|
||||
* The class resolver.
|
||||
*/
|
||||
public function __construct(ControllerResolverInterface $controller_resolver, FormBuilderInterface $form_builder, ClassResolverInterface $class_resolver) {
|
||||
parent::__construct($controller_resolver, $form_builder);
|
||||
$this->classResolver = $class_resolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getFormArgument(RouteMatchInterface $route_match) {
|
||||
return $route_match->getRouteObject()->getDefault('_form');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getFormObject(RouteMatchInterface $route_match, $form_arg) {
|
||||
return $this->classResolver->getInstanceFromDefinition($form_arg);
|
||||
}
|
||||
|
||||
}
|
77
core/lib/Drupal/Core/Controller/TitleResolver.php
Normal file
77
core/lib/Drupal/Core/Controller/TitleResolver.php
Normal file
|
@ -0,0 +1,77 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\Controller\TitleResolver.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\Controller;
|
||||
|
||||
use Drupal\Core\StringTranslation\StringTranslationTrait;
|
||||
use Drupal\Core\StringTranslation\TranslationInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Routing\Route;
|
||||
|
||||
/**
|
||||
* Provides the default implementation of the title resolver interface.
|
||||
*/
|
||||
class TitleResolver implements TitleResolverInterface {
|
||||
use StringTranslationTrait;
|
||||
|
||||
/**
|
||||
* The controller resolver.
|
||||
*
|
||||
* @var \Drupal\Core\Controller\ControllerResolverInterface
|
||||
*/
|
||||
protected $controllerResolver;
|
||||
|
||||
/**
|
||||
* Constructs a TitleResolver instance.
|
||||
*
|
||||
* @param \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver
|
||||
* The controller resolver.
|
||||
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
|
||||
* The translation manager.
|
||||
*/
|
||||
public function __construct(ControllerResolverInterface $controller_resolver, TranslationInterface $string_translation) {
|
||||
$this->controllerResolver = $controller_resolver;
|
||||
$this->stringTranslation = $string_translation;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getTitle(Request $request, Route $route) {
|
||||
$route_title = NULL;
|
||||
// A dynamic title takes priority. Route::getDefault() returns NULL if the
|
||||
// named default is not set. By testing the value directly, we also avoid
|
||||
// trying to use empty values.
|
||||
if ($callback = $route->getDefault('_title_callback')) {
|
||||
$callable = $this->controllerResolver->getControllerFromDefinition($callback);
|
||||
$arguments = $this->controllerResolver->getArguments($request, $callable);
|
||||
$route_title = call_user_func_array($callable, $arguments);
|
||||
}
|
||||
elseif ($title = $route->getDefault('_title')) {
|
||||
$options = array();
|
||||
if ($context = $route->getDefault('_title_context')) {
|
||||
$options['context'] = $context;
|
||||
}
|
||||
$args = array();
|
||||
if (($raw_parameters = $request->attributes->get('_raw_variables'))) {
|
||||
foreach ($raw_parameters->all() as $key => $value) {
|
||||
$args['@' . $key] = $value;
|
||||
$args['!' . $key] = $value;
|
||||
$args['%' . $key] = $value;
|
||||
}
|
||||
}
|
||||
if ($title_arguments = $route->getDefault('_title_arguments')) {
|
||||
$args = array_merge($args, (array) $title_arguments);
|
||||
}
|
||||
|
||||
// Fall back to a static string from the route.
|
||||
$route_title = $this->t($title, $args, $options);
|
||||
}
|
||||
return $route_title;
|
||||
}
|
||||
|
||||
}
|
36
core/lib/Drupal/Core/Controller/TitleResolverInterface.php
Normal file
36
core/lib/Drupal/Core/Controller/TitleResolverInterface.php
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\Controller\TitleResolverInterface.
|
||||
*/
|
||||
namespace Drupal\Core\Controller;
|
||||
|
||||
use Symfony\Component\Routing\Route;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* Defines a class which knows how to generate the title from a given route.
|
||||
*/
|
||||
interface TitleResolverInterface {
|
||||
|
||||
/**
|
||||
* Returns a static or dynamic title for the route.
|
||||
*
|
||||
* The returned title string must be safe to output in HTML. For example, an
|
||||
* implementation should call \Drupal\Component\Utility\SafeMarkup::checkPlain()
|
||||
* or \Drupal\Component\Utility\Xss::filterAdmin() on the string, or use
|
||||
* appropriate placeholders to sanitize dynamic content inside a localized
|
||||
* string before returning it. The title may contain HTML such as EM tags.
|
||||
*
|
||||
* @param \Symfony\Component\HttpFoundation\Request $request
|
||||
* The request object passed to the title callback.
|
||||
* @param \Symfony\Component\Routing\Route $route
|
||||
* The route information of the route to fetch the title.
|
||||
*
|
||||
* @return string|null
|
||||
* The title for the route.
|
||||
*/
|
||||
public function getTitle(Request $request, Route $route);
|
||||
|
||||
}
|
Reference in a new issue