class EntityBreakLockForm in Content locking (anti-concurrent editing) 8
Same name and namespace in other branches
- 8.2 src/Form/EntityBreakLockForm.php \Drupal\content_lock\Form\EntityBreakLockForm
Provides a base class for break content lock forms.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\content_lock\Form\EntityBreakLockForm
Expanded class hierarchy of EntityBreakLockForm
File
- src/
Form/ EntityBreakLockForm.php, line 20
Namespace
Drupal\content_lock\FormView source
class EntityBreakLockForm extends FormBase {
/**
* Content lock service.
*
* @var \Drupal\content_lock\ContentLock\ContentLock
*/
protected $lockService;
/**
* Current request.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* Language manager service.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* EntityBreakLockForm constructor.
*
* @param \Drupal\content_lock\ContentLock\ContentLock $contentLock
* Content lock service.
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
* Request stack service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* Language manager service.
*/
public function __construct(ContentLock $contentLock, RequestStack $requestStack, LanguageManagerInterface $language_manager) {
$this->lockService = $contentLock;
$this->request = $requestStack
->getCurrentRequest();
$this->languageManager = $language_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('content_lock'), $container
->get('request_stack'), $container
->get('language_manager'));
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$entity_type = $form_state
->getValue('entity_type_id');
$entity_id = $form_state
->getValue('entity_id');
$langcode = $form_state
->getValue('langcode');
$form_op = $form_state
->getValue('form_op') ?: NULL;
$this->lockService
->release($entity_id, $langcode, $form_op, NULL, $entity_type);
if ($form_state
->get('translation_lock')) {
$this
->messenger()
->addStatus($this
->t('Lock broken. Anyone can now edit this content translation.'));
}
else {
$this
->messenger()
->addStatus($this
->t('Lock broken. Anyone can now edit this content.'));
}
// Redirect URL to the request destination or the canonical entity view.
if ($destination = $this->request->query
->get('destination')) {
$url = Url::fromUserInput($destination);
$form_state
->setRedirectUrl($url);
}
else {
$language = $this->languageManager
->getLanguage($form_state
->get('langcode_entity'));
$url = Url::fromRoute("entity.{$entity_type}.canonical", [
$entity_type => $entity_id,
], [
'language' => $language,
]);
$form_state
->setRedirectUrl($url);
}
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'break_lock_entity';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, ContentEntityInterface $entity = NULL, $langcode = NULL, $form_op = NULL) {
// Save langcode of lock, before checking if translation lock is enabled.
// This is needed to generate the correct entity URL for the given language.
$form_state
->set('langcode_entity', $langcode);
$translation_lock = $this->lockService
->isTranslationLockEnabled($entity
->getEntityTypeId());
if (!$translation_lock) {
$langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
}
$form_state
->set('translation_lock', $translation_lock);
$form_op_lock = $this->lockService
->isFormOperationLockEnabled($entity
->getEntityTypeId());
if (!$form_op_lock) {
$form_op = '*';
}
$form['#title'] = $this
->t('Break Lock for content @label', [
'@label' => $entity
->label(),
]);
$form['entity_id'] = [
'#type' => 'value',
'#value' => $entity
->id(),
];
$form['entity_type_id'] = [
'#type' => 'value',
'#value' => $entity
->getEntityTypeId(),
];
$form['langcode'] = [
'#type' => 'value',
'#value' => $langcode,
];
$form['form_op'] = [
'#type' => 'value',
'#value' => $form_op,
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Confirm break lock'),
];
return $form;
}
/**
* Custom access checker for the form route requirements.
*/
public function access(ContentEntityInterface $entity, $langcode, $form_op, AccountInterface $account) {
return AccessResult::allowedIf($account
->hasPermission('break content lock') || $this->lockService
->isLockedBy($entity
->id(), $langcode, $form_op, $account
->id(), $entity
->getEntityTypeId()));
}
}
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 | |
EntityBreakLockForm:: |
protected | property | Language manager service. | |
EntityBreakLockForm:: |
protected | property | Content lock service. | |
EntityBreakLockForm:: |
protected | property | Current request. | |
EntityBreakLockForm:: |
public | function | Custom access checker for the form route requirements. | |
EntityBreakLockForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
EntityBreakLockForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
EntityBreakLockForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
EntityBreakLockForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
EntityBreakLockForm:: |
public | function | EntityBreakLockForm constructor. | |
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:: |
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. |