b73e618cc2d2fc5175c4229ba2ecbb1d9097c36c
[yaffs-website] / web / core / modules / views_ui / src / Form / BreakLockForm.php
1 <?php
2
3 namespace Drupal\views_ui\Form;
4
5 use Drupal\Core\Entity\EntityConfirmFormBase;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\user\SharedTempStoreFactory;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Builds the form to break the lock of an edited view.
13  */
14 class BreakLockForm extends EntityConfirmFormBase {
15
16   /**
17    * Stores the Entity manager.
18    *
19    * @var \Drupal\Core\Entity\EntityManagerInterface
20    */
21   protected $entityManager;
22
23   /**
24    * Stores the user tempstore.
25    *
26    * @var \Drupal\user\SharedTempStore
27    */
28   protected $tempStore;
29
30   /**
31    * Constructs a \Drupal\views_ui\Form\BreakLockForm object.
32    *
33    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
34    *   The Entity manager.
35    * @param \Drupal\user\SharedTempStoreFactory $temp_store_factory
36    *   The factory for the temp store object.
37    */
38   public function __construct(EntityManagerInterface $entity_manager, SharedTempStoreFactory $temp_store_factory) {
39     $this->entityManager = $entity_manager;
40     $this->tempStore = $temp_store_factory->get('views');
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public static function create(ContainerInterface $container) {
47     return new static(
48       $container->get('entity.manager'),
49       $container->get('user.shared_tempstore')
50     );
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function getFormId() {
57     return 'views_ui_break_lock_confirm';
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public function getQuestion() {
64     return $this->t('Do you want to break the lock on view %name?', ['%name' => $this->entity->id()]);
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function getDescription() {
71     $locked = $this->tempStore->getMetadata($this->entity->id());
72     $account = $this->entityManager->getStorage('user')->load($locked->owner);
73     $username = [
74       '#theme' => 'username',
75       '#account' => $account,
76     ];
77     return $this->t('By breaking this lock, any unsaved changes made by @user will be lost.', ['@user' => \Drupal::service('renderer')->render($username)]);
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public function getCancelUrl() {
84     return $this->entity->urlInfo('edit-form');
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public function getConfirmText() {
91     return $this->t('Break lock');
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function buildForm(array $form, FormStateInterface $form_state) {
98     if (!$this->tempStore->getMetadata($this->entity->id())) {
99       $form['message']['#markup'] = $this->t('There is no lock on view %name to break.', ['%name' => $this->entity->id()]);
100       return $form;
101     }
102     return parent::buildForm($form, $form_state);
103   }
104
105   /**
106    * {@inheritdoc}
107    */
108   public function submitForm(array &$form, FormStateInterface $form_state) {
109     $this->tempStore->delete($this->entity->id());
110     $form_state->setRedirectUrl($this->entity->urlInfo('edit-form'));
111     drupal_set_message($this->t('The lock has been broken and you may now edit this view.'));
112   }
113
114 }