class WorkspaceSwitcherForm in Drupal 8
Same name and namespace in other branches
- 9 core/modules/workspaces/src/Form/WorkspaceSwitcherForm.php \Drupal\workspaces\Form\WorkspaceSwitcherForm
- 10 core/modules/workspaces/src/Form/WorkspaceSwitcherForm.php \Drupal\workspaces\Form\WorkspaceSwitcherForm
Provides a form that activates a different workspace.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\workspaces\Form\WorkspaceSwitcherForm implements WorkspaceFormInterface
Expanded class hierarchy of WorkspaceSwitcherForm
1 file declares its use of WorkspaceSwitcherForm
- WorkspaceSwitcherBlock.php in core/
modules/ workspaces/ src/ Plugin/ Block/ WorkspaceSwitcherBlock.php
File
- core/
modules/ workspaces/ src/ Form/ WorkspaceSwitcherForm.php, line 16
Namespace
Drupal\workspaces\FormView source
class WorkspaceSwitcherForm extends FormBase implements WorkspaceFormInterface {
/**
* The workspace manager.
*
* @var \Drupal\workspaces\WorkspaceManagerInterface
*/
protected $workspaceManager;
/**
* The workspace entity storage handler.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $workspaceStorage;
/**
* The messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Constructs a new WorkspaceSwitcherForm.
*
* @param \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager
* The workspace manager.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
*/
public function __construct(WorkspaceManagerInterface $workspace_manager, EntityTypeManagerInterface $entity_type_manager, MessengerInterface $messenger) {
$this->workspaceManager = $workspace_manager;
$this->workspaceStorage = $entity_type_manager
->getStorage('workspace');
$this->messenger = $messenger;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('workspaces.manager'), $container
->get('entity_type.manager'), $container
->get('messenger'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'workspace_switcher_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$workspaces = $this->workspaceStorage
->loadMultiple();
$workspace_labels = [];
foreach ($workspaces as $workspace) {
$workspace_labels[$workspace
->id()] = $workspace
->label();
}
$active_workspace = $this->workspaceManager
->getActiveWorkspace();
if ($active_workspace) {
unset($workspace_labels[$active_workspace
->id()]);
}
$form['current'] = [
'#type' => 'item',
'#title' => $this
->t('Current workspace'),
'#markup' => $active_workspace ? $active_workspace
->label() : $this
->t('None'),
'#wrapper_attributes' => [
'class' => [
'container-inline',
],
],
];
$form['workspace_id'] = [
'#type' => 'select',
'#title' => $this
->t('Select workspace'),
'#required' => TRUE,
'#options' => $workspace_labels,
'#wrapper_attributes' => [
'class' => [
'container-inline',
],
],
'#access' => !empty($workspace_labels),
];
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Activate'),
'#button_type' => 'primary',
'#access' => !empty($workspace_labels),
];
if ($active_workspace) {
$form['actions']['switch_to_live'] = [
'#type' => 'submit',
'#submit' => [
'::submitSwitchToLive',
],
'#value' => $this
->t('Switch to Live'),
'#limit_validation_errors' => [],
'#button_type' => 'primary',
];
}
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$id = $form_state
->getValue('workspace_id');
/** @var \Drupal\workspaces\WorkspaceInterface $workspace */
$workspace = $this->workspaceStorage
->load($id);
try {
$this->workspaceManager
->setActiveWorkspace($workspace);
$this->messenger
->addMessage($this
->t('%workspace_label is now the active workspace.', [
'%workspace_label' => $workspace
->label(),
]));
} catch (WorkspaceAccessException $e) {
$this->messenger
->addError($this
->t('You do not have access to activate the %workspace_label workspace.', [
'%workspace_label' => $workspace
->label(),
]));
}
}
/**
* Submit handler for switching to the live version of the site.
*/
public function submitSwitchToLive(array &$form, FormStateInterface $form_state) {
$this->workspaceManager
->switchToLive();
$this->messenger
->addMessage($this
->t('You are now viewing the live version of the site.'));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
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:: |
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. | |
WorkspaceSwitcherForm:: |
protected | property |
The messenger service. Overrides MessengerTrait:: |
|
WorkspaceSwitcherForm:: |
protected | property | The workspace manager. | |
WorkspaceSwitcherForm:: |
protected | property | The workspace entity storage handler. | |
WorkspaceSwitcherForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
WorkspaceSwitcherForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
WorkspaceSwitcherForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
WorkspaceSwitcherForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
WorkspaceSwitcherForm:: |
public | function | Submit handler for switching to the live version of the site. | |
WorkspaceSwitcherForm:: |
public | function | Constructs a new WorkspaceSwitcherForm. |