SimpleMegaMenuListBuilder.php in Simple Mega Menu 8
File
src/SimpleMegaMenuListBuilder.php
View source
<?php
namespace Drupal\simple_megamenu;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Url;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Link;
class SimpleMegaMenuListBuilder extends EntityListBuilder {
protected $entityTypeManager;
protected $bundleInfo;
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, EntityTypeManagerInterface $entityTypeManager, EntityTypeBundleInfoInterface $bundleInfo) {
parent::__construct($entity_type, $storage);
$this->entityTypeManager = $entityTypeManager;
$this->bundleInfo = $bundleInfo;
}
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('entity_type.manager'), $container
->get('entity_type.bundle.info'));
}
public function buildHeader() {
$header['id'] = $this
->t('Simple mega menu ID');
$header['name'] = $this
->t('Name');
$header['type'] = $this
->t('Type');
$header['target_menu'] = $this
->t('Target Menu');
return $header + parent::buildHeader();
}
public function buildRow(EntityInterface $entity) {
$row['id'] = $entity
->id();
$row['name'] = Link::fromTextAndUrl($entity
->label(), new Url('entity.simple_mega_menu.edit_form', [
'simple_mega_menu' => $entity
->id(),
]));
$bundle = $entity
->bundle();
$simple_mega_menu_type = $this->entityTypeManager
->getStorage('simple_mega_menu_type')
->load($bundle);
$row['type'] = $simple_mega_menu_type
->label();
$target_menus = $simple_mega_menu_type
->getTargetMenu();
$target_menus = array_filter($target_menus);
$menu_labels = [];
foreach ($target_menus as $menu_name) {
$menu_labels[] = $this->entityTypeManager
->getStorage('menu')
->load($menu_name)
->label();
}
$labels = implode(', ', $menu_labels);
$row['target_menu'] = new FormattableMarkup('@labels', [
'@labels' => $labels,
]);
return $row + parent::buildRow($entity);
}
}