View source
<?php
namespace Drupal\config_translation\Controller;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
class ConfigTranslationEntityListBuilder extends ConfigEntityListBuilder implements ConfigTranslationEntityListBuilderInterface {
protected function getFilterLabels() {
return [
'placeholder' => $this
->t('Enter label'),
'description' => $this
->t('Enter a part of the label or description to filter by.'),
];
}
public function render() {
$build = parent::render();
$filter = $this
->getFilterLabels();
usort($build['table']['#rows'], [
$this,
'sortRows',
]);
$build['filters'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'table-filter',
'js-show',
],
],
'#weight' => -10,
];
$build['filters']['text'] = [
'#type' => 'search',
'#title' => $this
->t('Search'),
'#size' => 30,
'#placeholder' => $filter['placeholder'],
'#attributes' => [
'class' => [
'table-filter-text',
],
'data-table' => '.config-translation-entity-list',
'autocomplete' => 'off',
'title' => $filter['description'],
],
];
$build['table']['#attributes']['class'][] = 'config-translation-entity-list';
$build['table']['#weight'] = 0;
$build['#attached']['library'][] = 'system/drupal.system.modules';
return $build;
}
public function buildRow(EntityInterface $entity) {
$row['label']['data'] = $entity
->label();
$row['label']['class'][] = 'table-filter-text-source';
return $row + parent::buildRow($entity);
}
public function buildHeader() {
$header['label'] = $this
->t('Label');
return $header + parent::buildHeader();
}
public function getOperations(EntityInterface $entity) {
$operations = parent::getOperations($entity);
foreach (array_keys($operations) as $operation) {
if (!($operation == 'translate')) {
unset($operations[$operation]);
}
}
return $operations;
}
public function sortRows($a, $b) {
return $this
->sortRowsMultiple($a, $b, [
'label',
]);
}
protected function sortRowsMultiple($a, $b, $keys) {
$key = array_shift($keys);
$a_value = is_array($a) && isset($a[$key]['data']) ? $a[$key]['data'] : '';
$b_value = is_array($b) && isset($b[$key]['data']) ? $b[$key]['data'] : '';
if ($a_value == $b_value && !empty($keys)) {
return $this
->sortRowsMultiple($a, $b, $keys);
}
return strnatcasecmp($a_value, $b_value);
}
public function setMapperDefinition($mapper_definition) {
return $this;
}
}