3 namespace Drupal\views_ui\Form;
5 use Drupal\Core\Entity\EntityConfirmFormBase;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\TempStore\SharedTempStoreFactory;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
12 * Builds the form to break the lock of an edited view.
16 class BreakLockForm extends EntityConfirmFormBase {
19 * Stores the Entity manager.
21 * @var \Drupal\Core\Entity\EntityManagerInterface
23 protected $entityManager;
26 * Stores the shared tempstore.
28 * @var \Drupal\Core\TempStore\SharedTempStore
33 * Constructs a \Drupal\views_ui\Form\BreakLockForm object.
35 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
37 * @param \Drupal\Core\TempStore\SharedTempStoreFactory $temp_store_factory
38 * The factory for the temp store object.
40 public function __construct(EntityManagerInterface $entity_manager, SharedTempStoreFactory $temp_store_factory) {
41 $this->entityManager = $entity_manager;
42 $this->tempStore = $temp_store_factory->get('views');
48 public static function create(ContainerInterface $container) {
50 $container->get('entity.manager'),
51 $container->get('tempstore.shared')
58 public function getFormId() {
59 return 'views_ui_break_lock_confirm';
65 public function getQuestion() {
66 return $this->t('Do you want to break the lock on view %name?', ['%name' => $this->entity->id()]);
72 public function getDescription() {
73 $locked = $this->tempStore->getMetadata($this->entity->id());
74 $account = $this->entityManager->getStorage('user')->load($locked->owner);
76 '#theme' => 'username',
77 '#account' => $account,
79 return $this->t('By breaking this lock, any unsaved changes made by @user will be lost.', ['@user' => \Drupal::service('renderer')->render($username)]);
85 public function getCancelUrl() {
86 return $this->entity->urlInfo('edit-form');
92 public function getConfirmText() {
93 return $this->t('Break lock');
99 public function buildForm(array $form, FormStateInterface $form_state) {
100 if (!$this->tempStore->getMetadata($this->entity->id())) {
101 $form['message']['#markup'] = $this->t('There is no lock on view %name to break.', ['%name' => $this->entity->id()]);
104 return parent::buildForm($form, $form_state);
110 public function submitForm(array &$form, FormStateInterface $form_state) {
111 $this->tempStore->delete($this->entity->id());
112 $form_state->setRedirectUrl($this->entity->urlInfo('edit-form'));
113 $this->messenger()->addStatus($this->t('The lock has been broken and you may now edit this view.'));