LoginDestinationListBuilder.php in Login Destination 8
File
src/Controller/LoginDestinationListBuilder.php
View source
<?php
namespace Drupal\login_destination\Controller;
use Drupal\Core\Config\Entity\DraggableListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\login_destination\Entity\LoginDestination;
class LoginDestinationListBuilder extends DraggableListBuilder {
protected $entitiesKey = 'login_destination';
public function getFormId() {
return 'login_destination_overview';
}
public function buildHeader() {
$header = [
'label' => $this
->t('Label'),
'destination' => $this
->t('Destination'),
'triggers' => $this
->t('Triggers'),
'pages' => $this
->t('Pages'),
'language' => $this
->t('Language'),
'roles' => $this
->t('Roles'),
'enabled' => $this
->t('Enabled'),
];
return $header + parent::buildHeader();
}
public function buildRow(EntityInterface $entity) {
$row['label'] = $entity
->getLabel();
if ($entity
->isDestinationCurrent()) {
$row['destination'] = [
'#markup' => $entity
->viewDestination(),
];
}
else {
$row['destination'] = [
'#type' => 'link',
'#title' => $entity
->viewDestination(),
'#url' => Url::fromUri($entity
->getDestination()),
];
}
$row['triggers'] = [
'#markup' => $entity
->viewTriggers(),
];
$row['pages'] = [
'#markup' => $entity
->viewPages(),
];
$row['language'] = [
'#markup' => $entity
->getLanguage() != "" ? $entity
->getLanguage() : $this
->t('All languages'),
];
$row['roles'] = [
'#markup' => $entity
->viewRoles(),
];
$row['enabled'] = [
'#type' => 'checkbox',
'#default_value' => $entity
->isEnabled(),
];
return $row + parent::buildRow($entity);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
foreach ($form_state
->getValue($this->entitiesKey) as $id => $value) {
$this->entities[$id]
->set('enabled', $value['enabled']);
$this->entities[$id]
->save();
}
}
}