Commit 09f10ef9 authored by alexpott's avatar alexpott

Issue #1987826 by jsacksick: Convert system_plugin_autocomplete() to a new style controller.

parent 6023052c
<?php
/**
* @file
* Contains \Drupal\system\Access\SystemPluginUiCheck.
*/
namespace Drupal\system\Access;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Access\AccessCheckInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\HttpFoundation\Request;
/**
* Access check for system routes.
*/
class SystemPluginUiCheck implements AccessCheckInterface {
/**
* The plugin UI manager.
*
* @var \Drupal\Component\Plugin\PluginManagerInterface
*/
protected $pluginUiManager;
/**
* Constructs a SystemController object.
*
* @param \Drupal\Component\Plugin\PluginManagerInterface $plugin_ui_manager
* The plugin UI manager.
*/
public function __construct(PluginManagerInterface $plugin_ui_manager) {
$this->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;
}
}
}
<?php
/**
* @file
* Contains \Drupal\system\Controller\SystemController.
*/
namespace Drupal\system\Controller;
use Drupal\Component\Utility\Tags;
use Drupal\Component\Utility\Unicode;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
/**
* Returns responses for System routes.
*/
class SystemController extends ContainerAware {
/**
* 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->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);
}
}
......@@ -1012,13 +1012,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,
);
}
}
......@@ -1039,37 +1032,6 @@ function system_plugin_ui_form($form, &$form_state, $plugin, $facet = NULL) {
return $form;
}
/**
* 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.
*
......
......@@ -171,3 +171,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'
......@@ -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']
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment