3 namespace Drupal\ctools\Form;
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;
13 abstract class ResolverRelationshipDelete extends ConfirmFormBase {
16 * @var \Drupal\user\SharedTempStoreFactory
21 * @var \Drupal\ctools\TypedDataResolver
28 protected $tempstore_id;
33 protected $machine_name;
43 public static function create(ContainerInterface $container) {
44 return new static($container->get('user.shared_tempstore'), $container->get('ctools.typed_data.resolver'));
48 * @param \Drupal\user\SharedTempStoreFactory $tempstore
49 * The shared tempstore.
50 * @param \Drupal\ctools\TypedDataResolver $resolver
51 * The the typed data resolver.
53 public function __construct(SharedTempStoreFactory $tempstore, TypedDataResolver $resolver) {
54 $this->tempstore = $tempstore;
55 $this->resolver = $resolver;
61 public function getFormId() {
62 return 'ctools_resolver_relationship_delete';
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(),
78 abstract public function getCancelUrl($cached_values = []);
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;
88 $cached_values = $this->tempstore->get($this->tempstore_id)->get($this->machine_name);
89 $form ['#title'] = $this->getQuestion($id, $cached_values);
91 $form ['#attributes']['class'][] = 'confirmation';
92 $form ['description'] = array('#markup' => $this->getDescription());
93 $form [$this->getFormName()] = array('#type' => 'hidden', '#value' => 1);
95 // By default, render the form using theme_confirm_form().
96 if (!isset($form ['#theme'])) {
97 $form ['#theme'] = 'confirm_form';
99 $form['actions'] = array('#type' => 'actions');
100 $form['actions'] += $this->actions($form, $form_state, $cached_values);
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));
113 * A custom form actions method.
117 * @param \Drupal\Core\Form\FormStateInterface $form_state
118 * The current form state.
119 * @param $cached_values
120 * The current wizard cached values.
124 protected function actions(array $form, FormStateInterface $form_state, $cached_values) {
128 '#value' => $this->getConfirmText(),
129 '#validate' => array(
130 array($this, 'validate'),
133 array($this, 'submitForm'),
136 'cancel' => ConfirmFormHelper::buildCancelLink($this, $this->getRequest()),
141 * Extract contexts from the cached values.
143 * @param array $cached_values
146 * @return \Drupal\Core\Plugin\Context\ContextInterface[]
148 abstract public function getContexts($cached_values);