class RestExampleClientController
Same name in other branches
- 3.x modules/rest_example/src/Controller/RestExampleClientController.php \Drupal\rest_example\Controller\RestExampleClientController
Controller class for the REST Example routes.
Hierarchy
- class \Drupal\rest_example\Controller\RestExampleClientController implements \Drupal\Core\DependencyInjection\ContainerInjectionInterface uses \Drupal\Core\Messenger\MessengerTrait, \Drupal\Core\StringTranslation\StringTranslationTrait
Expanded class hierarchy of RestExampleClientController
Related topics
File
-
modules/
rest_example/ src/ Controller/ RestExampleClientController.php, line 18
Namespace
Drupal\rest_example\ControllerView source
class RestExampleClientController implements ContainerInjectionInterface {
use MessengerTrait;
use StringTranslationTrait;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The service to make REST calls.
*
* @var \Drupal\rest_example\RestExampleClientCalls
*/
protected $restClient;
/**
* Constructs a new \Drupal\rest_example\Controller\RestExampleClientController object.
*
* @param \Drupal\rest_example\RestExampleClientCalls $rest_client
* The service to make REST calls.
*/
public function __construct(ConfigFactoryInterface $config_factory, RestExampleClientCalls $rest_client) {
$this->configFactory = $config_factory;
$this->restClient = $rest_client;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$controller = new static($container->get('config.factory'), $container->get('rest_example_client_calls'));
$controller->setMessenger($container->get('messenger'));
$controller->setStringTranslation($container->get('string_translation'));
return $controller;
}
/**
* Retrieves the list of all nodes available on the remote site.
*
* Building the list as a table by calling the RestExampleClientCalls::index()
* and builds the list from the response of that.
*
* @throws \RuntimeException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function indexAction() {
$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.'),
];
$build['node_table'] = [
'#type' => 'table',
'#header' => [
$this->t('Title'),
$this->t('Type'),
$this->t('Created'),
$this->t('Edit'),
$this->t('Delete'),
],
'#empty' => $this->t('There are no items on the remote system yet'),
];
if ($this->configFactory
->get('rest_example.settings')
->get('server_url')) {
$this->messenger()
->addWarning($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',
]));
}
else {
$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;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
MessengerTrait::$messenger | protected | property | The messenger. | 16 | |
MessengerTrait::messenger | public | function | Gets the messenger. | 16 | |
MessengerTrait::setMessenger | public | function | Sets the messenger. | ||
RestExampleClientController::$configFactory | protected | property | The config factory. | ||
RestExampleClientController::$restClient | protected | property | The service to make REST calls. | ||
RestExampleClientController::create | public static | function | Instantiates a new instance of this class. | Overrides ContainerInjectionInterface::create | |
RestExampleClientController::indexAction | public | function | Retrieves the list of all nodes available on the remote site. | ||
RestExampleClientController::__construct | public | function | Constructs a new \Drupal\rest_example\Controller\RestExampleClientController object. | ||
StringTranslationTrait::$stringTranslation | protected | property | The string translation service. | 3 | |
StringTranslationTrait::formatPlural | protected | function | Formats a string containing a count of items. | ||
StringTranslationTrait::getNumberOfPlurals | protected | function | Returns the number of plurals supported by a given language. | ||
StringTranslationTrait::getStringTranslation | protected | function | Gets the string translation service. | ||
StringTranslationTrait::setStringTranslation | public | function | Sets the string translation service to use. | 2 | |
StringTranslationTrait::t | protected | function | Translates a string to the current language or to a given language. |