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

Issue #3472829 by manav: Create list to show all the tasks.

parent cf43078e
No related branches found
No related tags found
No related merge requests found
<?php
namespace Drupal\todoist_api\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use GuzzleHttp\ClientInterface;
class TodoistapiTasks extends ControllerBase {
/**
* Guzzle\Client instance.
*
* @var \GuzzleHttp\ClientInterface
*/
protected $httpClient;
/**
* {@inheritdoc}
*/
public function __construct(ClientInterface $http_client) {
$this->httpClient = $http_client;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('http_client')
);
}
/**
* Posts route callback.
*
* @param int $limit
* The total number of posts we want to fetch.
* @param string $sort
* The sorting order.
*
* @return array
* A render array used to show the Posts list.
*/
/**
* A simple controller method to explain what the tablesort example is about.
*/
public function build() {
// We are going to output the results in a table with a nice header.
$header = [
// The header gives the table the information it needs in order to make
// the query calls for ordering. TableSort uses the field information
// to know what database column to sort by.
['data' => $this->t('Task'), 'field' => 'content'],
['data' => $this->t('Task added date'), 'field' => 'description'],
['data' => $this->t('Task due date'), 'field' => 'created_at'],
['data' => $this->t('Action'), 'field' => 'created_at']
];
// $result = [
// ['number' => '1', 'alpha' => 'aaa', 'random' => '912cv21'],
// ['number' => '2', 'alpha' => 'bbb', 'random' => '0kuykuh'],
// ['number' => '3', 'alpha' => 'ccc', 'random' => '0kuykuh'],
// ['number' => '4', 'alpha' => 'ddd', 'random' => '0kuykuh'],
// ['number' => '5', 'alpha' => 'eee', 'random' => '0kuykuh'],
// ['number' => '6', 'alpha' => 'fff', 'random' => '0kuykuh'],
// ];
$options = ['headers' => ['Authorization' => 'Bearer a0dc04e0c04e0e19ac7621cd5385f5111920bf2f']];
$request = $this->httpClient->request('GET', 'https://api.todoist.com/rest/v2/tasks', $options);
$response_data = json_decode($request->getBody()->getContents(), TRUE);
//dump($response_data);
//die;
$rows = [];
foreach ($response_data as $row) {
$created_date = (new \DateTime($row['created_at']))->getTimestamp();
//dump($row);
$rows[] = ['data' => [
$row['content'],
$row['description'],
$created_date
]
];
}
//dump($rows);
// Build the table for the nice output.
$build = [
'#markup' => '<p>' . $this->t('The layout here is a themed as a table
that is sortable by clicking the header name.') . '</p>',
];
$build['tablesort_table'] = [
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
];
return $build;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment