View source
<?php
namespace Drupal\redhen_connection\Controller;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
class RedhenConnections extends ControllerBase {
public function __construct(EntityStorageInterface $storage, EntityStorageInterface $type_storage) {
$this->storage = $storage;
$this->typeStorage = $type_storage;
}
public static function create(ContainerInterface $container) {
$entity_type_manager = $container
->get('entity_type.manager');
return new static($entity_type_manager
->getStorage('redhen_connection'), $entity_type_manager
->getStorage('redhen_connection_type'));
}
public function list(Request $request) {
$connection_service = \Drupal::service('redhen_connection.connections');
$entity = redhen_connection_get_connection_entity_from_route();
$entity_type_key = $entity
->getEntityTypeId();
$connections = $connection_service
->getConnections($entity);
$header = [
'Type',
'Name',
'Operations',
];
$add_url = Url::fromRoute("{$entity_type_key}.connection.add_page", [
$entity_type_key => $entity
->id(),
], [
'absolute' => TRUE,
])
->toString();
$rows = [];
foreach ($connections as $connection) {
$view = Link::createFromRoute($connection
->label()
->render(), 'entity.redhen_connection.canonical', [
'redhen_connection' => $connection
->id(),
])
->toString();
$edit = Link::createFromRoute('Edit', 'entity.redhen_connection.edit_form', [
'redhen_connection' => $connection
->id(),
])
->toString();
$delete = Link::createFromRoute('Delete', 'entity.redhen_connection.delete_form', [
'redhen_connection' => $connection
->id(),
])
->toString();
$row = [
'data' => [
$connection
->getType(),
new FormattableMarkup("@view", [
'@view' => $view,
]),
new FormattableMarkup("@edit @delete", [
'@edit' => $edit,
'@delete' => $delete,
]),
],
];
$rows[] = $row;
}
$build = [
'table' => [
'#theme' => 'table',
'#prefix' => "<ul class='action-links'><li><a class='button button-action button--primary button--small' href=" . $add_url . ">Add Connection</a></li></ul>",
'#attributes' => [
'data-striping' => 0,
],
'#header' => $header,
'#rows' => $rows,
],
];
return $build;
}
public function access(AccountInterface $account) {
$own = FALSE;
if ($entity = redhen_connection_get_connection_entity_from_route()) {
$entity_type_key = $entity
->getEntityTypeId();
if (method_exists($entity, 'getUserId')) {
$own = $entity
->getUserId() == $account
->id();
}
elseif (method_exists($entity, 'getOwnerId')) {
$own = $entity
->getOwnerId() == $account
->id();
}
}
if ($own) {
return AccessResult::allowedIf($account
->hasPermission('view active connection entities') || $account
->hasPermission('view own active ' . $entity_type_key . ' connection'));
}
else {
return AccessResult::allowedIf($account
->hasPermission('view active connection entities'));
}
}
}