commit eafae8566597ba4c628ec84541f73dc8ce114654 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..db13644 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Access/SystemPluginUiCheck.php @@ -0,0 +1,55 @@ +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) { + if ($request->attributes->get('plugin_id')) { + // Checks access for a given plugin using the plugin's access() method. + $plugin_ui = $this->pluginUiManager->createInstance($request->attributes->get('plugin_id'), array()); + return $plugin_ui->access(NULL) ? static::ALLOW : static::DENY; + } + } + +} 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..bd71353 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Controller/SystemController.php @@ -0,0 +1,56 @@ +query->get('q'); + $string_typed = Tags::explode($string_typed); + $string = Unicode::strtolower(array_pop($string_typed)); + $matches = array(); + if ($string) { + $plugin_ui = $this->container->get('plugin.manager.system.plugin_ui')->getDefinition($plugin_id); + $manager = $this->container->get($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 a6a7786..4998f6c 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -1017,13 +1017,6 @@ function system_menu() { $items[$plugin['path'] . '/' . $plugin_id . '/%']['file path'] = $plugin['file_path']; } } - $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, - ); } } @@ -1045,37 +1038,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']