-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(state_api): Add State API module for custom state management at /admin/states #23
Open
sapatevaibhav
wants to merge
1
commit into
main
Choose a base branch
from
vaibhav_state_api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+479
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
vaibhav_state_api/config/schema/vaibhav_state_api.schema.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
vaibhav_state_api.manager: | ||
type: config_object | ||
label: 'Vaibhav State API configuration' | ||
mapping: | ||
storage: | ||
type: sequence | ||
label: 'State storage' | ||
sequence: | ||
type: string | ||
label: 'State entry' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
.vaibhav-state-demo { | ||
background: #f0f0f0; | ||
padding: 20px; | ||
border-radius: 5px; | ||
margin: 20px 0; | ||
} | ||
|
||
.value-at-page-load { | ||
color: blue; | ||
font-weight: bold; | ||
} | ||
|
||
.action-performed { | ||
color: green; | ||
font-weight: bold; | ||
} | ||
|
||
.value-after-action { | ||
color: red; | ||
font-weight: bold; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
(function ($, Drupal) { | ||
Drupal.behaviors.vaibhavStateApi = { | ||
attach: function (context, settings) { | ||
$('.use-ajax', context).once('vaibhavStateApi').on('click', function (e) { | ||
e.preventDefault(); | ||
var $link = $(this); | ||
$.ajax({ | ||
url: $link.attr('href'), | ||
type: 'GET', | ||
dataType: 'json', | ||
success: function (data) { | ||
$('p.value-at-page-load').text('Value at page load: ' + data.current_value); | ||
$('p.action-performed').text('Action performed: ' + data.after_action); | ||
$('p.value-after-action').text('Value after action: ' + data.final_value); | ||
} | ||
}); | ||
}); | ||
} | ||
}; | ||
})(jQuery, Drupal); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Vaibhav State API | ||
|
||
## Overview | ||
|
||
The Vaibhav State API module provides a demonstration of using the Drupal State API to store and manage persistent state information. This module includes a custom state plugin and a controller to showcase the functionality. | ||
|
||
## Features | ||
|
||
- Custom state plugin: `CustomState` | ||
- Demo page to interact with the State API | ||
|
||
## Installation | ||
|
||
1. Download and place the `vaibhav_state_api` module in your Drupal site's `modules/custom` directory. | ||
2. Enable the module using the following Drush command: | ||
```sh | ||
drush en vaibhav_state_api | ||
``` | ||
Or enable it through the Drupal admin interface. | ||
|
||
## Usage | ||
|
||
- Navigate to the demo page provided by the module at `/admin/states`. | ||
- Use the provided links to set a new value or delete the current value stored in the state. | ||
|
||
## Customization | ||
|
||
### State Plugin | ||
The custom state plugin is defined in `src/Plugin/State/CustomState.php`. This file contains the logic for managing the custom state. | ||
|
||
### State Manager | ||
The state manager is defined in `src/State/StateManager.php`. This file contains the logic for handling state-related operations. | ||
|
||
### Controller | ||
The controller is defined in `src/Controller/StateDemoController.php`. This file contains the logic for the demo page, including setting and deleting state values. | ||
|
||
## Configuration | ||
The module's services are defined in `vaibhav_state_api.services.yml`. This file registers the custom state plugin and state manager service. |
131 changes: 131 additions & 0 deletions
131
vaibhav_state_api/src/Controller/StateDemoController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php | ||
|
||
namespace Drupal\vaibhav_state_api\Controller; | ||
|
||
use Drupal\Core\Controller\ControllerBase; | ||
use Drupal\Core\State\StateInterface; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Drupal\Core\Messenger\MessengerInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Drupal\Core\Ajax\AjaxResponse; | ||
use Drupal\Core\Ajax\HtmlCommand; | ||
|
||
/** | ||
* Controller for the State API demo. | ||
*/ | ||
class StateDemoController extends ControllerBase { | ||
|
||
/** | ||
* The state service. | ||
* | ||
* @var \Drupal\Core\State\StateInterface | ||
*/ | ||
protected $customState; | ||
|
||
/** | ||
* The request stack. | ||
* | ||
* @var \Symfony\Component\HttpFoundation\RequestStack | ||
*/ | ||
protected $requestStack; | ||
|
||
/** | ||
* The messenger service. | ||
* | ||
* @var \Drupal\Core\Messenger\MessengerInterface | ||
*/ | ||
protected $messenger; | ||
|
||
/** | ||
* Constructor for StateDemoController. | ||
* | ||
* @param \Drupal\Core\State\StateInterface $custom_state | ||
* The custom state service. | ||
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack | ||
* The request stack service. | ||
* @param \Drupal\Core\Messenger\MessengerInterface $messenger | ||
* The messenger service. | ||
*/ | ||
public function __construct(StateInterface $custom_state, RequestStack $request_stack, MessengerInterface $messenger) { | ||
$this->customState = $custom_state; | ||
$this->requestStack = $request_stack; | ||
$this->messenger = $messenger; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function create(ContainerInterface $container) { | ||
return new static( | ||
$container->get('state'), | ||
$container->get('request_stack'), | ||
$container->get('messenger') | ||
); | ||
} | ||
|
||
/** | ||
* Demo page. | ||
*/ | ||
public function demoPage() { | ||
// Get the current request. | ||
$request = $this->requestStack->getCurrentRequest(); | ||
$delete = $request->query->get('delete') === 'true'; | ||
|
||
// Get current value first (shows persistence between requests) | ||
$current_value = $this->customState->get('vaibhav_demo_key'); | ||
|
||
if ($delete) { | ||
// If delete parameter is set, delete the value. | ||
$this->customState->delete('vaibhav_demo_key'); | ||
$this->messenger->addStatus($this->t('Value has been deleted!')); | ||
$after_action = 'Value was deleted'; | ||
} | ||
else { | ||
// Otherwise, set a new value. | ||
$new_value = date('Y-m-d H:i:s'); | ||
$this->customState->set('vaibhav_demo_key', $new_value); | ||
$after_action = 'Value was set to: ' . $new_value; | ||
} | ||
|
||
// Get the value after our action. | ||
$final_value = $this->customState->get('vaibhav_demo_key'); | ||
|
||
$response_data = [ | ||
'current_value' => $current_value ?? 'No value', | ||
'after_action' => $after_action, | ||
'final_value' => $final_value ?? 'No value', | ||
]; | ||
|
||
if ($request->isXmlHttpRequest()) { | ||
$response = new AjaxResponse(); | ||
$response->addCommand(new HtmlCommand('.value-at-page-load', 'Value at page load: ' . $response_data['current_value'])); | ||
$response->addCommand(new HtmlCommand('.action-performed', 'Action performed: ' . $response_data['after_action'])); | ||
$response->addCommand(new HtmlCommand('.value-after-action', 'Value after action: ' . $response_data['final_value'])); | ||
return $response; | ||
} | ||
|
||
$build = [ | ||
'#type' => 'markup', | ||
'#markup' => $this->t(' | ||
<h2>State API Demo</h2> | ||
<p class="value-at-page-load">Value at page load: @current</p> | ||
<p class="action-performed">Action performed: @action</p> | ||
<p class="value-after-action">Value after action: @final</p> | ||
<p><a href="/admin/states" class="use-ajax">Set new value</a> | <a href="/admin/states?delete=true" class="use-ajax">Delete value</a></p> | ||
', [ | ||
'@current' => $current_value ?? 'No value', | ||
'@action' => $after_action, | ||
'@final' => $final_value ?? 'No value', | ||
]), | ||
'#attached' => [ | ||
'library' => [ | ||
'core/drupal.ajax', | ||
'vaibhav_state_api/demo', | ||
], | ||
], | ||
]; | ||
|
||
return $build; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
namespace Drupal\vaibhav_state_api\Plugin\State; | ||
|
||
use Drupal\Core\State\StateInterface; | ||
use Drupal\Core\Plugin\PluginBase; | ||
use Drupal\vaibhav_state_api\State\StateManager; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
/** | ||
* Provides a custom state manager. | ||
* | ||
* @State( | ||
* id = "custom_state", | ||
* label = @Translation("Custom State Manager"), | ||
* category = "custom_states" | ||
* ) | ||
*/ | ||
class CustomState extends PluginBase implements StateInterface { | ||
// Due we are implementing StateInterface we need to implement | ||
// all the methods from the interface. | ||
// Like get, set, delete, getMultiple, setMultiple, | ||
// deleteMultiple and resetCache. | ||
/** | ||
* The state manager. | ||
* | ||
* @var \Drupal\vaibhav_state_api\State\StateManager | ||
*/ | ||
protected $manager; | ||
|
||
/** | ||
* Constructs a CustomState object. | ||
* | ||
* @param array $configuration | ||
* A configuration array containing plugin instance information. | ||
* @param string $plugin_id | ||
* The plugin ID for the plugin instance. | ||
* @param mixed $plugin_definition | ||
* The plugin implementation definition. | ||
* @param \Drupal\vaibhav_state_api\State\StateManager $manager | ||
* The state manager service. | ||
*/ | ||
public function __construct(array $configuration, $plugin_id, $plugin_definition, StateManager $manager) { | ||
parent::__construct($configuration, $plugin_id, $plugin_definition); | ||
$this->manager = $manager; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | ||
return new static( | ||
$configuration, | ||
$plugin_id, | ||
$plugin_definition, | ||
$container->get('vaibhav_state_api.manager') | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function get($key, $default = NULL) { | ||
return $this->manager->get($key) ?? $default; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function set($key, $value) { | ||
$this->manager->set($key, $value); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function delete($key) { | ||
$this->manager->delete($key); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getMultiple($keys) { | ||
$values = []; | ||
foreach ($keys as $key) { | ||
$values[$key] = $this->get($key); | ||
} | ||
return $values; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setMultiple(array $values) { | ||
foreach ($values as $key => $value) { | ||
$this->set($key, $value); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function deleteMultiple(array $keys) { | ||
foreach ($keys as $key) { | ||
$this->delete($key); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resetCache() { | ||
// Implement cache reset if needed. | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sapatevaibhav , Why do we need this custom state manager, can't we do this using Drupal's State API?