4dfde28560fd9e4a98fad91a8092702ad1267c0b
[yaffs-website] / Form / ResolverRelationshipDelete.php
1 <?php
2
3 namespace Drupal\ctools\Form;
4
5
6 use Drupal\Core\Form\ConfirmFormBase;
7 use Drupal\Core\Form\ConfirmFormHelper;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\ctools\TypedDataResolver;
10 use Drupal\user\SharedTempStoreFactory;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 abstract class ResolverRelationshipDelete extends ConfirmFormBase {
14
15   /**
16    * @var \Drupal\user\SharedTempStoreFactory
17    */
18   protected $tempstore;
19
20   /**
21    * @var \Drupal\ctools\TypedDataResolver
22    */
23   protected $resolver;
24
25   /**
26    * @var string
27    */
28   protected $tempstore_id;
29
30   /**
31    * @var string;
32    */
33   protected $machine_name;
34
35   /**
36    * @var string;
37    */
38   protected $id;
39
40   /**
41    * {@inheritdoc}
42    */
43   public static function create(ContainerInterface $container) {
44     return new static($container->get('user.shared_tempstore'), $container->get('ctools.typed_data.resolver'));
45   }
46
47   /**
48    * @param \Drupal\user\SharedTempStoreFactory $tempstore
49    *   The shared tempstore.
50    * @param \Drupal\ctools\TypedDataResolver $resolver
51    *   The the typed data resolver.
52    */
53   public function __construct(SharedTempStoreFactory $tempstore, TypedDataResolver $resolver) {
54     $this->tempstore = $tempstore;
55     $this->resolver = $resolver;
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public function getFormId() {
62     return 'ctools_resolver_relationship_delete';
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public function getQuestion($id = NULL, $cached_values = []) {
69     $context = $this->getContexts($cached_values)[$id];
70     return $this->t('Are you sure you want to delete the @label relationship?', [
71       '@label' => $context->getContextDefinition()->getLabel(),
72     ]);
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   abstract public function getCancelUrl($cached_values = []);
79
80   /**
81    * {@inheritdoc}
82    */
83   public function buildForm(array $form, FormStateInterface $form_state, $id = NULL, $tempstore_id = NULL, $machine_name = NULL) {
84     $this->tempstore_id = $tempstore_id;
85     $this->machine_name = $machine_name;
86     $this->id = $id;
87
88     $cached_values = $this->tempstore->get($this->tempstore_id)->get($this->machine_name);
89     $form ['#title'] = $this->getQuestion($id, $cached_values);
90
91     $form ['#attributes']['class'][] = 'confirmation';
92     $form ['description'] = array('#markup' => $this->getDescription());
93     $form [$this->getFormName()] = array('#type' => 'hidden', '#value' => 1);
94
95     // By default, render the form using theme_confirm_form().
96     if (!isset($form ['#theme'])) {
97       $form ['#theme'] = 'confirm_form';
98     }
99     $form['actions'] = array('#type' => 'actions');
100     $form['actions'] += $this->actions($form, $form_state, $cached_values);
101     return $form;
102   }
103
104   /**
105    * {@inheritdoc}
106    */
107   public function submitForm(array &$form, FormStateInterface $form_state) {
108     $cached_values = $this->tempstore->get($this->tempstore_id)->get($this->machine_name);
109     $form_state->setRedirectUrl($this->getCancelUrl($cached_values));
110   }
111
112   /**
113    * A custom form actions method.
114    *
115    * @param array $form
116    *   The form array.
117    * @param \Drupal\Core\Form\FormStateInterface $form_state
118    *   The current form state.
119    * @param $cached_values
120    *   The current wizard cached values.
121    *
122    * @return array
123    */
124   protected function actions(array $form, FormStateInterface $form_state, $cached_values) {
125     return array(
126       'submit' => array(
127         '#type' => 'submit',
128         '#value' => $this->getConfirmText(),
129         '#validate' => array(
130           array($this, 'validate'),
131         ),
132         '#submit' => array(
133           array($this, 'submitForm'),
134         ),
135       ),
136       'cancel' => ConfirmFormHelper::buildCancelLink($this, $this->getRequest()),
137     );
138   }
139
140   /**
141    * Extract contexts from the cached values.
142    *
143    * @param array $cached_values
144    *   The cached values.
145    *
146    * @return \Drupal\Core\Plugin\Context\ContextInterface[]
147    */
148   abstract public function getContexts($cached_values);
149
150 }