class FasttoggleController in Fasttoggle 8.2
Same name and namespace in other branches
- 8.3 src/Controller/FasttoggleController.php \Drupal\fasttoggle\Controller\FasttoggleController
Route controller for Fasttoggle.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\fasttoggle\Controller\FasttoggleController
Expanded class hierarchy of FasttoggleController
2 files declare their use of FasttoggleController
- user.php in src/
Plugin/ SettingObject/ user.php - Fasttoggle Managed Node
- viewElementsTrait.php in src/
viewElementsTrait.php
File
- src/
Controller/ FasttoggleController.php, line 16
Namespace
Drupal\fasttoggle\ControllerView source
class FasttoggleController extends ControllerBase {
/**
* @var \Drupal\fasttoggle\SettingObjectPluginManager
* The plugin manager for objects that have Fasttogglable settings.
*/
private $objectManager;
/**
* @var \Drupal\fasttoggle\SettingGroupPluginManager
* The plugin manager for grouping of settings.
*/
private $settingGroupManager;
/**
* @var \Drupal\fasttoggle\SettingPluginManager
* The plugin manager for classes that implement toggling settings on
* objects.
*/
private $settingManager;
/**
* Constructor
*/
public function __construct() {
$this->objectManager = \Drupal::service('plugin.manager.fasttoggle.setting_object');
$this->settingGroupManager = \Drupal::service('plugin.manager.fasttoggle.setting_group');
$this->settingManager = \Drupal::service('plugin.manager.fasttoggle.setting');
}
/**
* Get Fasttoggle config.
*
* @return array
* Fasttoggle configuration array.
*/
public static function getConfig() {
return \Drupal::config('fasttoggle.settings');
}
/**
* Get an array of sitewide settings.
*
* @return array
* An array of objects and settings for building the form and saving
* settings.
*/
public function getSitewideSettingsInfo() {
$objectSettings = $this->settingsManager
->getDefinitions();
$objects = $this->objectManager
->getDefinitions();
foreach ($objects as $type => $object) {
$plugin = $this->objectManager
->createInstance($type);
$objectSettings[$type] += $plugin
->sitewideSettings();
}
return $objectSettings;
}
/**
* Get an object manager for an object.
*
* @param object
* The object to use.
*
* @return \Drupal\fasttoggle\Plugin\SettingObject
* The matching object.
*/
public function getObjectManager($instance) {
$objects = $this->objectManager
->getDefinitions();
foreach ($objects as $type => $object) {
$plugin = $this->objectManager
->createInstance($type);
if ($plugin
->objectMatches($instance)) {
$plugin
->setObject($instance);
return $plugin;
}
}
return null;
}
/**
* Get the group and setting for a field name.
*
* @param \Drupal\Core\Field\FieldDefinitionInterface
* The field with the value to be toggled.
* @return array
* The group and setting - NULL if not found.
*/
public function groupAndSettingFromFieldName($definition) {
$settingPlugin = $this->settingManager
->match($definition);
if ($settingPlugin) {
$group = $settingPlugin
->getPluginDefinition()['group'];
$groupPlugin = $this->settingGroupManager
->createInstance($group);
}
else {
$groupPlugin = NULL;
}
return array(
$groupPlugin,
$settingPlugin,
);
}
/**
* Toggle a setting.
*
* @param $request
* The request object.
*/
public function toggle(Request $request) {
// I don't like this - I should be able to just specify args in the routing
// like the docs say and put them as args to this function. It wouldn't work
// however...
// We need to do our own handling of the path if we're coming via a view.
$parts = explode('/', $request
->getPathInfo());
if (count($parts) != 4 && count($parts) != 5) {
$response = new Response();
$response
->setContent('Incorrect number of parameters');
$response
->setStatusCode(400);
return $response;
}
// Use the token to look up cached parameters.
// Fasttoggle Parameters
// - Entity type
// - Entity ID
// - Setting Group
// - Setting
// - Language code
// - Base formatter
// Formatter parameters
// - Plugin definition
// - Field definition
// - Settings
// - Label
// - View mode
// - Third party settings
$cid = $parts[3];
$newValue = count($parts) == 5 ? NULL : $parts[4];
$cached = \Drupal::cache()
->get('fasttoggle-' . $cid);
if (!$cached) {
$response = new Response();
$response
->setContent('Unrecognised setting hash.');
$response
->setStatusCode(400);
return $response;
}
$entity_type = $cached->data['object_type'];
$entity_id = $cached->data['object_id'];
$group = $cached->data['group'];
$setting = $cached->data['setting'];
// Validate parameters.
$this->objectManager = \Drupal::service('plugin.manager.fasttoggle.setting_object');
$objects = $this->objectManager
->getDefinitions();
if (!$objects[$entity_type]) {
$response = new Response();
$response
->setContent('Unrecognised entity type.');
$response
->setStatusCode(400);
return $response;
}
$controller = \Drupal::entityTypeManager()
->getStorage($entity_type);
$entity = $controller
->load($entity_id);
if (!$entity) {
$response = new Response();
$response
->setContent('Invalid entity ID.');
$response
->setStatusCode(400);
return $response;
}
$settings = $this->settingManager
->getDefinitions();
if (empty($settings[$entity_type]) || empty($settings[$entity_type][$group]) || empty($settings[$entity_type][$group][$setting])) {
return;
}
// Check access to the setting group.
$plugin = $this->settingManager
->createInstance($setting);
$plugin
->setObject($entity);
$plugin
->setField($cached->data['field_definition']);
// Check permissions.
if (!$plugin
->mayEdit()
->allowed()) {
$response = new Response();
$response
->setContent('No edit permission.');
$response
->setStatusCode(400);
return $response;
}
// Apply the change - will need modifying for roles.
//$instance = array_search($setting, $this->attributes);
$instance = 0;
if (is_null($newValue)) {
$plugin
->nextValue($instance);
}
else {
$plugin
->setValue($instance, $newValue);
}
$plugin
->save();
$renderer = \Drupal::service('renderer');
$delta = $plugin
->get_field();
$render_array = $plugin
->formatter($delta, $cached->data);
$html = $renderer
->render($render_array);
// Return Ajax for the modified content and modified link.
$response = new AjaxResponse();
$response
->addCommand(new ReplaceCommand('#fasttoggle-' . $cid, $html . ""));
return $response;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity manager. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
40 |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity manager service. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
ControllerBase:: |
protected | function | Returns the state storage service. | |
FasttoggleController:: |
private | property | The plugin manager for objects that have Fasttogglable settings. | |
FasttoggleController:: |
private | property | The plugin manager for grouping of settings. | |
FasttoggleController:: |
private | property | The plugin manager for classes that implement toggling settings on objects. | |
FasttoggleController:: |
public static | function | Get Fasttoggle config. | |
FasttoggleController:: |
public | function | Get an object manager for an object. | |
FasttoggleController:: |
public | function | Get an array of sitewide settings. | |
FasttoggleController:: |
public | function | Get the group and setting for a field name. | |
FasttoggleController:: |
public | function | Toggle a setting. | |
FasttoggleController:: |
public | function | Constructor | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |