Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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\Core\TempStore\SharedTempStoreFactory;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Builds the form to break the lock of an edited view.
13  *
14  * @internal
15  */
16 class BreakLockForm extends EntityConfirmFormBase {
17
18   /**
19    * Stores the Entity manager.
20    *
21    * @var \Drupal\Core\Entity\EntityManagerInterface
22    */
23   protected $entityManager;
24
25   /**
26    * Stores the shared tempstore.
27    *
28    * @var \Drupal\Core\TempStore\SharedTempStore
29    */
30   protected $tempStore;
31
32   /**
33    * Constructs a \Drupal\views_ui\Form\BreakLockForm object.
34    *
35    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
36    *   The Entity manager.
37    * @param \Drupal\Core\TempStore\SharedTempStoreFactory $temp_store_factory
38    *   The factory for the temp store object.
39    */
40   public function __construct(EntityManagerInterface $entity_manager, SharedTempStoreFactory $temp_store_factory) {
41     $this->entityManager = $entity_manager;
42     $this->tempStore = $temp_store_factory->get('views');
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public static function create(ContainerInterface $container) {
49     return new static(
50       $container->get('entity.manager'),
51       $container->get('tempstore.shared')
52     );
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public function getFormId() {
59     return 'views_ui_break_lock_confirm';
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function getQuestion() {
66     return $this->t('Do you want to break the lock on view %name?', ['%name' => $this->entity->id()]);
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function getDescription() {
73     $locked = $this->tempStore->getMetadata($this->entity->id());
74     $account = $this->entityManager->getStorage('user')->load($locked->owner);
75     $username = [
76       '#theme' => 'username',
77       '#account' => $account,
78     ];
79     return $this->t('By breaking this lock, any unsaved changes made by @user will be lost.', ['@user' => \Drupal::service('renderer')->render($username)]);
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function getCancelUrl() {
86     return $this->entity->urlInfo('edit-form');
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public function getConfirmText() {
93     return $this->t('Break lock');
94   }
95
96   /**
97    * {@inheritdoc}
98    */
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()]);
102       return $form;
103     }
104     return parent::buildForm($form, $form_state);
105   }
106
107   /**
108    * {@inheritdoc}
109    */
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.'));
114   }
115
116 }