RestExampleClientDelete.php in Examples for Developers 3.x
File
modules/rest_example/src/Form/RestExampleClientDelete.php
View source
<?php
namespace Drupal\rest_example\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Url;
use Drupal\rest_example\RestExampleClientCalls;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RestExampleClientDelete extends ConfirmFormBase {
private $restExampleClientCalls;
public function __construct(RestExampleClientCalls $rest_example_client_calls) {
$this->restExampleClientCalls = $rest_example_client_calls;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('rest_example_client_calls'));
}
public function getFormId() {
return 'rest_example_client_delete';
}
public function getQuestion() {
return $this
->t('Are you sure that you want to delete this content.');
}
public function getCancelUrl() {
return new Url('rest_example.client_actions_index');
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if (is_null($form_state
->get('node_id')) || !is_numeric($form_state
->get('node_id'))) {
$form_state
->setErrorByName('delete', $this
->t('The ID passed in the URL is not an integer'));
}
}
public function buildForm(array $form, FormStateInterface $form_state, $id = NULL) {
$form = parent::buildForm($form, $form_state);
$form_state
->set('node_id', $id);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$node_id = $form_state
->get('node_id');
$node = [
'nid' => $node_id,
];
$this->restExampleClientCalls
->delete($node);
$this
->messenger()
->addStatus($this
->t('Node was successfully deleted'));
$form_state
->setRedirect('rest_example.client_actions_index');
}
}