View source
<?php
namespace Drupal\lingotek;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\Entity\DraggableListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LingotekProfileListBuilder extends DraggableListBuilder {
protected $entitiesKey = 'profile';
protected $languageManager;
protected $configFactory;
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity_type.manager')
->getStorage($entity_type
->id()), $container
->get('language_manager'), $container
->get('config.factory'));
}
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, LanguageManagerInterface $language_manager, ConfigFactoryInterface $config_factory) {
parent::__construct($entity_type, $storage);
$this->languageManager = $language_manager;
$this->configFactory = $config_factory;
}
public function load() {
$entities = $this->storage
->loadMultiple();
uasort($entities, [
$this->entityType
->getClass(),
'sort',
]);
return $entities;
}
public function getFormId() {
return 'lingotek_profile_admin_overview_form';
}
public function buildHeader() {
$header = [
'label' => t('Name'),
'auto_upload' => t('Automatic Upload'),
'auto_download' => t('Automatic Download'),
] + parent::buildHeader();
return $header;
}
public function buildRow(EntityInterface $entity) {
$row['label'] = $entity
->label();
$row['auto_upload'] = [
'#type' => 'checkbox',
'#title' => t('Set @title for automatic upload', [
'@title' => $entity
->label(),
]),
'#title_display' => 'invisible',
'#disabled' => $entity
->isLocked(),
'#default_value' => $entity
->hasAutomaticUpload(),
];
$row['auto_download'] = [
'#type' => 'checkbox',
'#title' => t('Set @title for automatic download', [
'@title' => $entity
->label(),
]),
'#title_display' => 'invisible',
'#disabled' => $entity
->isLocked(),
'#default_value' => $entity
->hasAutomaticDownload(),
];
return $row + parent::buildRow($entity);
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$form[$this->entitiesKey]['#profiles'] = $this->entities;
$form['actions']['submit']['#value'] = t('Save configuration');
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
foreach ($this->entities as $entity_id => $entity) {
if (!$entity
->isLocked() && ($entity
->hasAutomaticUpload() != $form_state
->getValue([
'profile',
$entity_id,
'auto_upload',
]) || $entity
->hasAutomaticDownload() != $form_state
->getValue([
'profile',
$entity_id,
'auto_download',
]))) {
$entity
->setAutomaticUpload($form_state
->getValue([
'profile',
$entity_id,
'auto_upload',
]));
$entity
->setAutomaticDownload($form_state
->getValue([
'profile',
$entity_id,
'auto_download',
]));
$entity
->save();
}
}
$this
->messenger()
->addStatus(t('Configuration saved.'));
}
public function getOperations(EntityInterface $entity) {
$operations = [];
if (!$entity
->isLocked() && $entity
->hasLinkTemplate('edit-form')) {
$operations['edit'] = [
'title' => $this
->t('Edit'),
'weight' => 10,
'url' => $entity
->toUrl('edit-form'),
];
}
if (!$entity
->isLocked() && $entity
->hasLinkTemplate('delete-form')) {
$operations['delete'] = [
'title' => $this
->t('Delete'),
'weight' => 100,
'url' => $entity
->toUrl('delete-form'),
];
}
return $operations;
}
}