Skip to content
Snippets Groups Projects
Commit 5280fc97 authored by Arjun Kumar's avatar Arjun Kumar
Browse files

Issue #3485276: Add complete task functionality

parent 27ede30c
Branches
No related tags found
1 merge request!4Issue #3485276: Add complete task functionality
<?php
namespace Drupal\todoist_api\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use GuzzleHttp\Exception\RequestException;
use Drupal\Component\Serialization\Json;
/**
* Defines a confirmation form to confirm deletion of something by id.
*/
class CloseTaskForm extends ConfirmFormBase {
/**
* ID of the item to delete.
*
* @var int
*/
protected $id;
/**
* ID of the item to delete.
*
* @var object
*/
protected $todoistclient;
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, string $id = NULL) {
$this->id = $id;
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// @todo: Do the deletion.
$uri = 'tasks';
$content_id = $this->id;
try{
$response = \Drupal::service('todoist_api.client')->post($uri, null, $content_id);
if($response):
$this->messenger()->addStatus($this->t('One task has been completed.'));
$form_state->setRedirect('todoist_api.taskscontroller');
endif;
} catch( RequestException $exception) {
\Drupal::logger('error_rest_client')->notice($exception->getMessage());
$form_state->setErrorByName('task', $this->t('Application error. Please try again later.'));
}
}
/**
* {@inheritdoc}
*/
public function getFormId() : string {
return "task_close_form";
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('todoist_api.taskscontroller');
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
$uri = 'tasks';
$content_id = $this->id;
return $this->t('Do you want to close the %id?', ['%id' => $this->id]);
}
}
......@@ -26,3 +26,12 @@ todoist_api.deletetask:
requirements:
_permission: 'user todoist access'
id: ^\d+$
todoist_api.closetask:
path: '/admin/content/todoist/{id}/close'
defaults:
_form: '\Drupal\todoist_api\Form\CloseTaskForm'
_title: 'Complete Task'
requirements:
_permission: 'user todoist access'
id: ^\d+$
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment