Pathologic was missing because of a .git folder inside.
[yaffs-website] / web / modules / contrib / crop / src / CropTypeListBuilder.php
1 <?php
2
3 namespace Drupal\crop;
4
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;
14
15 /**
16  * Defines a class to build a listing of crop type entities.
17  *
18  * @see \Drupal\crop\Entity\CropType
19  */
20 class CropTypeListBuilder extends ConfigEntityListBuilder {
21
22   /**
23    * The url generator service.
24    *
25    * @var \Drupal\Core\Routing\UrlGeneratorInterface
26    */
27   protected $urlGenerator;
28
29   /**
30    * The entity query factory.
31    *
32    * @var \Drupal\Core\Entity\Query\QueryFactory
33    */
34   protected $queryFactory;
35
36   /**
37    * Constructs a CropTypeListBuilder object.
38    *
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.
47    */
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;
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
58     return new static(
59       $entity_type,
60       $container->get('entity.manager')->getStorage($entity_type->id()),
61       $container->get('url_generator'),
62       $container->get('entity.query')
63     );
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function buildHeader() {
70     $header = [];
71     $header['name'] = t('Name');
72     $header['description'] = [
73       'data' => $this->t('Description'),
74       'class' => [RESPONSIVE_PRIORITY_MEDIUM],
75     ];
76     $header['aspect_ratio'] = [
77       'data' => $this->t('Aspect Ratio'),
78     ];
79     $header['usage'] = $this->t('Used in');
80     return $header + parent::buildHeader();
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function buildRow(EntityInterface $entity) {
87     $row = [];
88     $row['name'] = [
89       'data' => $entity->label(),
90       'class' => ['menu-label'],
91     ];
92     $row['description'] = Xss::filterAdmin($entity->description);
93     $row['aspect_ratio'] = $entity->getAspectRatio();
94
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())
98       ->execute();
99     $image_styles = ImageStyle::loadMultiple($image_style_ids);
100
101     /** @var \Drupal\image\Entity\ImageStyle $image_style */
102     $usage = [];
103     foreach ($image_styles as $image_style) {
104       if (count($usage) < 2) {
105         $usage[] = $image_style->link();
106       }
107     }
108
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),
115       ]);
116     }
117     else {
118       $usage_message = implode(', ', $usage);
119     }
120     $row['usage']['data']['#markup'] = $usage_message;
121
122     return $row + parent::buildRow($entity);
123   }
124
125   /**
126    * {@inheritdoc}
127    */
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'),
132     ]);
133     return $build;
134   }
135
136 }