LoginDestination.php in Login Destination 8
File
src/Entity/LoginDestination.php
View source
<?php
namespace Drupal\login_destination\Entity;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Url;
use Drupal\login_destination\LoginDestinationInterface;
class LoginDestination extends ConfigEntityBase implements LoginDestinationInterface {
const REDIRECT_NOT_LISTED = 0;
const REDIRECT_LISTED = 1;
const TRIGGER_LOGIN = 'login';
const TRIGGER_REGISTRATION = 'registration';
const TRIGGER_ONE_TIME_LOGIN = 'one-time-login';
const TRIGGER_LOGOUT = 'logout';
public $name;
public $label;
public $triggers = [];
public $roles = [];
public $pages_type = self::REDIRECT_NOT_LISTED;
public $pages = '';
public $language = '';
public $enabled = TRUE;
public $destination_path;
public $weight = 0;
public function id() {
return $this->name;
}
public function getLabel() {
return $this->label;
}
public function getMachineName() {
return $this->name;
}
public function getTriggers() {
return $this->triggers;
}
public function getDestination() {
return $this->destination_path;
}
public function getPagesType() {
return $this->pages_type;
}
public function getPages() {
return $this->pages;
}
public function getLanguage() {
return $this->language;
}
public function getRoles() {
return $this->roles;
}
public function getWeight() {
return $this->weight;
}
public function viewTriggers() {
$items = [];
foreach ($this->triggers as $trigger) {
if (empty($trigger)) {
continue;
}
switch ($trigger) {
case LoginDestination::TRIGGER_REGISTRATION:
$items[] = t('Registration');
break;
case LoginDestination::TRIGGER_LOGIN:
$items[] = t('Login');
break;
case LoginDestination::TRIGGER_ONE_TIME_LOGIN:
$items[] = t('One-time login link');
break;
case LoginDestination::TRIGGER_LOGOUT:
$items[] = t('Logout');
break;
}
}
return $this
->renderItemList($items, t('All triggers'));
}
public function viewRoles() {
$roles = $this
->getAllSystemRoles();
$items = array_values(array_intersect_key($roles, $this->roles));
return $this
->renderItemList($items, t('All roles'));
}
public function viewPages() {
$type = $this->pages_type;
$pages = trim($this->pages);
if (empty($pages)) {
if ($type == self::REDIRECT_NOT_LISTED) {
return t('All pages');
}
return t('No pages');
}
$pages = explode("\n", preg_replace('/\\r/', '', $this->pages));
$items = [];
foreach ($pages as $page) {
$items[] = $type == self::REDIRECT_NOT_LISTED ? '~ ' . $page : $page;
}
return $this
->renderItemList($items, t('Empty'));
}
public function viewDestination() {
if ($this
->isDestinationCurrent()) {
$scheme = 'internal';
}
else {
$url = Url::fromUri($this->destination_path);
$label = $this->destination_path;
if ($url
->isExternal()) {
return Html::escape($label);
}
$scheme = parse_url($this->destination_path, PHP_URL_SCHEME);
}
if ($scheme === 'internal') {
return t('Internal destination');
}
if ($scheme === 'entity') {
$params = $url
->getRouteParameters();
$entity = \Drupal::entityTypeManager()
->getStorage('node')
->load(reset($params));
return $entity
->get('title')->value;
}
return Html::escape($this->destination_path);
}
public function isDestinationCurrent() {
return Unicode::strpos($this->destination_path, '<current>') !== FALSE;
}
protected function renderItemList(array $array, $empty_message) {
$items = [];
foreach ($array as $value) {
if (!empty($value)) {
$items[] = Html::escape($value);
}
}
if (count($items) === 0) {
return $empty_message;
}
$item_list = [
'#theme' => 'item_list',
'#items' => $items,
'#list_type' => 'ul',
];
return \Drupal::service('renderer')
->render($item_list);
}
public function getAllSystemRoles() {
$role_options = [];
foreach (user_roles(TRUE) as $role) {
$role_options[$role
->id()] = $role
->label();
}
return $role_options;
}
public function isEnabled() {
return $this->enabled;
}
}