5 use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
6 use Drupal\Core\Entity\Query\QueryFactory;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9 use Drupal\Core\Entity\EntityStorageInterface;
10 use Drupal\Core\Routing\UrlGeneratorInterface;
11 use Drupal\Core\Entity\EntityInterface;
12 use Drupal\Component\Utility\Xss;
13 use Drupal\image\Entity\ImageStyle;
16 * Defines a class to build a listing of crop type entities.
18 * @see \Drupal\crop\Entity\CropType
20 class CropTypeListBuilder extends ConfigEntityListBuilder {
23 * The url generator service.
25 * @var \Drupal\Core\Routing\UrlGeneratorInterface
27 protected $urlGenerator;
30 * The entity query factory.
32 * @var \Drupal\Core\Entity\Query\QueryFactory
34 protected $queryFactory;
37 * Constructs a CropTypeListBuilder object.
39 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
40 * The entity type definition.
41 * @param \Drupal\Core\Entity\EntityStorageInterface $storage
42 * The entity storage class.
43 * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
44 * The url generator service.
45 * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory
46 * The entity query factory.
48 public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, UrlGeneratorInterface $url_generator, QueryFactory $query_factory) {
49 parent::__construct($entity_type, $storage);
50 $this->urlGenerator = $url_generator;
51 $this->queryFactory = $query_factory;
57 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
60 $container->get('entity.manager')->getStorage($entity_type->id()),
61 $container->get('url_generator'),
62 $container->get('entity.query')
69 public function buildHeader() {
71 $header['name'] = t('Name');
72 $header['description'] = [
73 'data' => $this->t('Description'),
74 'class' => [RESPONSIVE_PRIORITY_MEDIUM],
76 $header['aspect_ratio'] = [
77 'data' => $this->t('Aspect Ratio'),
79 $header['usage'] = $this->t('Used in');
80 return $header + parent::buildHeader();
86 public function buildRow(EntityInterface $entity) {
89 'data' => $entity->label(),
90 'class' => ['menu-label'],
92 $row['description'] = Xss::filterAdmin($entity->description);
93 $row['aspect_ratio'] = $entity->getAspectRatio();
95 // Load all image styles used by the current crop type.
96 $image_style_ids = $this->queryFactory->get('image_style')
97 ->condition('effects.*.data.crop_type', $entity->id())
99 $image_styles = ImageStyle::loadMultiple($image_style_ids);
101 /** @var \Drupal\image\Entity\ImageStyle $image_style */
103 foreach ($image_styles as $image_style) {
104 if (count($usage) < 2) {
105 $usage[] = $image_style->link();
109 $other_image_styles = array_splice($image_styles, 2);
110 if ($other_image_styles) {
111 $usage_message = t('@first, @second and @count more', [
112 '@first' => $usage[0],
113 '@second' => $usage[1],
114 '@count' => count($other_image_styles),
118 $usage_message = implode(', ', $usage);
120 $row['usage']['data']['#markup'] = $usage_message;
122 return $row + parent::buildRow($entity);
128 public function render() {
129 $build = parent::render();
130 $build['table']['#empty'] = t('No crop types available. <a href="@link">Add crop type</a>.', [
131 '@link' => $this->urlGenerator->generateFromRoute('crop.type_add'),