commit 6d5aef9fb22f159d29238df3c4ac7fb1111fd097 Author: jsacksick Date: Sat Jun 8 10:34:52 2013 +0200 Issue #1987826: Convert system_plugin_autocomplete() to a new style controller. diff --git a/core/modules/system/lib/Drupal/system/Access/SystemPluginUiCheck.php b/core/modules/system/lib/Drupal/system/Access/SystemPluginUiCheck.php new file mode 100644 index 0000000..0c4c0dd --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Access/SystemPluginUiCheck.php @@ -0,0 +1,53 @@ +pluginUIManager = $plugin_ui_manager; + } + + /** + * {@inheritdoc} + */ + public function applies(Route $route) { + return array_key_exists('_access_system_plugin_ui', $route->getRequirements()); + } + + /** + * {@inheritdoc} + */ + public function access(Route $route, Request $request) { + // Checks access for a given plugin using the plugin's access() method. + $plugin_ui = $this->pluginUIManager->createInstance($request->attributes->get('plugin_id')); + return $plugin_ui->access(NULL); + } + +} diff --git a/core/modules/system/lib/Drupal/system/Controller/SystemController.php b/core/modules/system/lib/Drupal/system/Controller/SystemController.php new file mode 100644 index 0000000..dac198b --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Controller/SystemController.php @@ -0,0 +1,84 @@ +pluginUiManager = $plugin_ui_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('plugin.manager.system.plugin_ui') + ); + } + + /** + * Autocompletes any plugin system tied to a plugin UI plugin. + * + * The passed plugin_id indicates the specific plugin_ui plugin that is in use + * here. The documentation within the annotation of that plugin will contain a + * manager for the plugins that need to be autocompleted allowing this + * function to autocomplete plugins for any plugin type. + * + * @param string $plugin_id + * The plugin id for the calling plugin. + * @param Request $request + * The request object that contains the typed tags. + * + * @return \Symfony\Component\HttpFoundation\JsonResponse + * The matched plugins as json. + */ + public function autocomplete($plugin_id, Request $request) { + $string_typed = $request->query->get('q'); + $string_typed = Tags::explode($string_typed); + $string = Unicode::strtolower(array_pop($string_typed)); + $matches = array(); + if ($string) { + $plugin_ui = $this->pluginUiManager->getDefinition($plugin_id); + $manager = \Drupal::service($plugin_ui['manager']); + $titles = array(); + foreach($manager->getDefinitions() as $plugin_id => $plugin) { + $titles[$plugin_id] = $plugin[$plugin_ui['title_attribute']]; + } + $matches = preg_grep("/\b". $string . "/i", $titles); + } + + return new JsonResponse($matches); + } + +} diff --git a/core/modules/system/system.module b/core/modules/system/system.module index 238f303..312a90e 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -1023,11 +1023,7 @@ function system_menu() { } } $items['system/autocomplete/' . $plugin_id] = array( - 'page callback' => 'system_plugin_autocomplete', - 'page arguments' => array($plugin_id), - 'access callback' => 'system_plugin_ui_access', - 'access arguments' => array($plugin_id), - 'type' => MENU_CALLBACK, + 'route_name' => 'system_plugin_autocomplete', ); } } @@ -1050,37 +1046,6 @@ function system_plugin_ui_form($form, &$form_state, $plugin, $facet = NULL) { } /** - * Page callback: Autocompletes any plugin system tied to a plugin UI plugin. - * - * The passed plugin_id indicates the specific plugin_ui plugin that is in use - * here. The documentation within the annotation of that plugin will contain a - * manager for the plugins that need to be autocompleted allowing this function - * to autocomplete plugins for any plugin type. - * - * @param $plugin_id - * The plugin id for the calling plugin. - * - * @return object JsonResponse - */ -function system_plugin_autocomplete($plugin_id) { - $string_typed = drupal_container()->get('request')->query->get('q'); - $string_typed = drupal_explode_tags($string_typed); - $string = drupal_strtolower(array_pop($string_typed)); - $matches = array(); - if ($string) { - $plugin_ui = Drupal::service('plugin.manager.system.plugin_ui')->getDefinition($plugin_id); - $manager = Drupal::service($plugin_ui['manager']); - $titles = array(); - foreach($manager->getDefinitions() as $plugin_id => $plugin) { - $titles[$plugin_id] = $plugin[$plugin_ui['title_attribute']]; - } - $matches = preg_grep("/\b". $string . "/i", $titles); - } - - return new JsonResponse($matches); -} - -/** * Checks access for a given plugin using the plugin's access() method. * * @todo This needs more explanation, some @see, and parameter documentation. diff --git a/core/modules/system/system.routing.yml b/core/modules/system/system.routing.yml index 1e09f79..b277695 100644 --- a/core/modules/system/system.routing.yml +++ b/core/modules/system/system.routing.yml @@ -164,3 +164,10 @@ system_timezone: _controller: '\Drupal\system\Controller\TimezoneController::getTimezone' requirements: _access: 'TRUE' + +system_plugin_autocomplete: + pattern: '/system/autocomplete/{plugin_id}' + defaults: + _controller: 'Drupal\system\Controller\SystemController::autocomplete' + requirements: + _access_system_plugin_ui: 'TRUE' diff --git a/core/modules/system/system.services.yml b/core/modules/system/system.services.yml index 6aefa00..3b9eac7 100644 --- a/core/modules/system/system.services.yml +++ b/core/modules/system/system.services.yml @@ -3,6 +3,11 @@ services: class: Drupal\system\Access\CronAccessCheck tags: - { name: access_check } + access_check.system_plugin_ui: + class: Drupal\system\Access\SystemPluginUiCheck + tags: + - { name: access_check } + arguments: ['@plugin.manager.system.plugin_ui'] plugin.manager.system.plugin_ui: class: Drupal\system\Plugin\Type\PluginUIManager arguments: ['@container.namespaces']