Skip to content
Snippets Groups Projects
Commit 3345afe9 authored by Alberto Paderno's avatar Alberto Paderno
Browse files

Merge branch '3458249-change-restexampleclientcontroller' into '4.0.x'

Issue #3458249: Do not use ControllerBase as parent class for RestExampleClientController

See merge request !56
parents 7248a870 5117df45
No related branches found
No related tags found
No related merge requests found
Pipeline #212924 passed with warnings
...@@ -2,46 +2,69 @@ ...@@ -2,46 +2,69 @@
namespace Drupal\rest_example\Controller; namespace Drupal\rest_example\Controller;
use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Link; use Drupal\Core\Link;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\rest_example\RestExampleClientCalls; use Drupal\rest_example\RestExampleClientCalls;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Controller routines for rest example routes. * Controller class for the REST Example routes.
* *
* @ingroup rest_example * @ingroup rest_example
*/ */
class RestExampleClientController extends ControllerBase { class RestExampleClientController implements ContainerInjectionInterface {
use MessengerTrait;
use StringTranslationTrait;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/** /**
* RestExampleClientCalls object. * The service to make REST calls.
* *
* @var \Drupal\rest_example\RestExampleClientCalls * @var \Drupal\rest_example\RestExampleClientCalls
*/ */
private $restExampleClientCalls; protected $restClient;
/** /**
* RestExampleClientController constructor. * Constructs a new \Drupal\rest_example\Controller\RestExampleClientController object.
* *
* @param \Drupal\rest_example\RestExampleClientCalls $rest_example_client_calls * @param \Drupal\rest_example\RestExampleClientCalls $rest_client
* RestExampleClientCalls service. * The service to make REST calls.
*/ */
public function __construct(RestExampleClientCalls $rest_example_client_calls) { public function __construct(
$this->restExampleClientCalls = $rest_example_client_calls; ConfigFactoryInterface $config_factory,
RestExampleClientCalls $rest_client,
) {
$this->configFactory = $config_factory;
$this->restClient = $rest_client;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
return new static( $controller = new static(
$container->get('config.factory'),
$container->get('rest_example_client_calls') $container->get('rest_example_client_calls')
); );
$controller->setMessenger($container->get('messenger'));
$controller->setStringTranslation($container->get('string_translation'));
return $controller;
} }
/** /**
* Retrieve a list of all nodes available on the remote site. * Retrieves the list of all nodes available on the remote site.
* *
* Building the list as a table by calling the RestExampleClientCalls::index() * Building the list as a table by calling the RestExampleClientCalls::index()
* and builds the list from the response of that. * and builds the list from the response of that.
...@@ -50,14 +73,6 @@ public static function create(ContainerInterface $container) { ...@@ -50,14 +73,6 @@ public static function create(ContainerInterface $container) {
* @throws \GuzzleHttp\Exception\GuzzleException * @throws \GuzzleHttp\Exception\GuzzleException
*/ */
public function indexAction() { public function indexAction() {
if (NULL === $this->configFactory->get('rest_example.settings')->get('server_url')) {
$this->messenger()->addWarning($this->t('The remote endpoint service address have not been set. Please go and provide the credentials and the endpoint address on the <a href="@url">config page</a>.', ['@url' => base_path() . 'examples/rest-client-settings']));
}
$build = [];
$nodes = $this->restExampleClientCalls->index();
$build['intro'] = [ $build['intro'] = [
'#markup' => $this->t('This is a list of nodes, of type <em>Rest Example Test</em>, on the remote server. From here you can create new node, edit and delete existing ones.'), '#markup' => $this->t('This is a list of nodes, of type <em>Rest Example Test</em>, on the remote server. From here you can create new node, edit and delete existing ones.'),
]; ];
...@@ -71,17 +86,27 @@ public function indexAction() { ...@@ -71,17 +86,27 @@ public function indexAction() {
$this->t('Edit'), $this->t('Edit'),
$this->t('Delete'), $this->t('Delete'),
], ],
'#empty' => t('There are no items on the remote system yet'), '#empty' => $this->t('There are no items on the remote system yet'),
]; ];
if (!empty($nodes)) { if ($this->configFactory->get('rest_example.settings')->get('server_url')) {
foreach ($nodes as $delta => $node) { $this->messenger()->addWarning(
$build['node_table'][$delta]['title']['#plain_text'] = $node['title']; $this->t('The remote endpoint service address has not been set. Please go and provide the credentials and the endpoint address on the <a href="@url">config page</a>.', ['@url' => base_path() . 'examples/rest-client-settings'])
$build['node_table'][$delta]['type']['#plain_text'] = $node['type']; );
$build['node_table'][$delta]['created']['#plain_text'] = $node['created']; }
$build['node_table'][$delta]['edit']['#plain_text'] = Link::createFromRoute($this->t('Edit'), 'rest_example.client_actions_edit', ['id' => $node['nid']])->toString(); else {
$build['node_table'][$delta]['delete']['#plain_tex'] = Link::createFromRoute($this->t('Delete'), 'rest_example.client_actions_delete', ['id' => $node['nid']])->toString(); $nodes = $this->restClient->index();
if (!empty($nodes)) {
foreach ($nodes as $delta => $node) {
$build['node_table'][$delta]['title']['#plain_text'] = $node['title'];
$build['node_table'][$delta]['type']['#plain_text'] = $node['type'];
$build['node_table'][$delta]['created']['#plain_text'] = $node['created'];
$build['node_table'][$delta]['edit']['#plain_text'] = Link::createFromRoute($this->t('Edit'), 'rest_example.client_actions_edit', ['id' => $node['nid']])->toString();
$build['node_table'][$delta]['delete']['#plain_tex'] = Link::createFromRoute($this->t('Delete'), 'rest_example.client_actions_delete', ['id' => $node['nid']])->toString();
}
} }
} }
return $build; return $build;
......
...@@ -59,7 +59,7 @@ class RestExampleClientCalls { ...@@ -59,7 +59,7 @@ class RestExampleClientCalls {
* @param \GuzzleHttp\ClientInterface $client * @param \GuzzleHttp\ClientInterface $client
* The HTTP client. * The HTTP client.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The Config Factory. * The config factory.
*/ */
public function __construct(ClientInterface $client, ConfigFactoryInterface $config_factory) { public function __construct(ClientInterface $client, ConfigFactoryInterface $config_factory) {
$this->client = $client; $this->client = $client;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment