Skip to content
Snippets Groups Projects

#3425489 Changed list builder to show draggable weights.

Open #3425489 Changed list builder to show draggable weights.
1 unresolved thread
1 unresolved thread
@@ -2,14 +2,62 @@
namespace Drupal\environment_indicator;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Config\Entity\DraggableListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Link;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a listing of environments.
*/
class EnvironmentIndicatorListBuilder extends ConfigEntityListBuilder {
class EnvironmentIndicatorListBuilder extends DraggableListBuilder {
/**
* The renderer service.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* Constructs a new EnvironmentIndicatorListBuilder object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
*/
public function __construct(
EntityTypeInterface $entity_type,
EntityTypeManagerInterface $entity_type_manager,
RendererInterface $renderer,
) {
parent::__construct(
$entity_type,
$entity_type_manager->getStorage($entity_type->id())
);
$this->renderer = $renderer;
}
/**
* {@inheritdoc}
*/
public static function createInstance(
ContainerInterface $container,
EntityTypeInterface $entity_type,
) {
return new static(
$entity_type,
$container->get('entity_type.manager'),
$container->get('renderer')
);
}
/**
* {@inheritdoc}
@@ -22,45 +70,78 @@ class EnvironmentIndicatorListBuilder extends ConfigEntityListBuilder {
* {@inheritdoc}
*/
public function buildHeader() {
$row['name'] = $this->t('Environment name');
$row['url'] = $this->t('Environment url');
$row += parent::buildHeader();
return $row;
$header = parent::buildHeader();
$operations = $header['operations'];
$weight = $header['weight'];
unset($header['operations']);
unset($header['weight']);
$header['preview'] = $this->t('Preview');
$header['url'] = $this->t('Environment URL');
if (!empty($this->weightKey)) {
$header['weight'] = $weight;
}
$header['operations'] = $operations;
return $header;
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
/** @var \Drupal\environment_indicator\Entity\EnvironmentIndicator $entity */
$row = [
'style' => 'color: ' . $entity->getFgColor() . '; background-color: ' . $entity->getBgColor() . ';',
];
$row['data']['name'] = [
'data' => $entity->label(),
];
$row['data']['url'] = [
'data' => $entity->getUrl(),
$row = parent::buildRow($entity);
$operations = $row['operations'];
unset($row['operations']);
$label_style = 'padding: 1rem; background-color: ' . $entity->getBgColor() . '; color: ' . $entity->getFgColor();
$row['preview'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $entity->label(),
'#attributes' => [
'style' => $label_style,
],
];
if (empty($this->weightKey)) {
unset($row['weight']);
$row['preview'] = $this->renderer->renderRoot($row['preview']);
    • I'm confused about why we need to render something ourselves here -- and only when the entity has no weight.

      This needs a comment to explain what is going on.

Please register or sign in to reply
$row['url'] = $entity->getUrl();
}
else {
$weight_element = $row['weight'];
unset($row['weight']);
$row['data'] += parent::buildRow($entity);
$row['url'] = [
'#markup' => $entity->getUrl(),
];
$row['weight'] = $weight_element;
}
$row['operations'] = $operations;
return $row;
}
/**
* {@inheritdoc}
*/
public function render() {
$build['action_header']['#markup'] = '<h3>' . $this->t('Available actions:') . '</h3>';
$entities = $this->load();
// If there are not multiple vocabularies, disable dragging by unsetting the
// weight key.
// Disable dragging by unsetting the weight key if there are 1 or fewer.
if (count($entities) <= 1) {
unset($this->weightKey);
}
$build = parent::render();
$build['table']['#empty'] = $this->t('No environment switchers available. <a href=":link">Add environment</a>.', [':link' => Url::fromRoute('entity.environment_indicator.add')->toString()]);
// Add an empty message if there are no entities.
if (isset($build['table'])) {
$build['table']['#empty'] = [
'#markup' => $this->t('No environments available. @link', [
'@link' => Link::fromTextAndUrl(
$this->t('Add environment.'),
Url::fromRoute('entity.environment_indicator.add')
)->toString(),
]),
];
}
return $build;
}
Loading