X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fcrop%2Fsrc%2FCropTypeListBuilder.php;fp=web%2Fmodules%2Fcontrib%2Fcrop%2Fsrc%2FCropTypeListBuilder.php;h=a175961bab2672143ec30481128564bf0c32192c;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/modules/contrib/crop/src/CropTypeListBuilder.php b/web/modules/contrib/crop/src/CropTypeListBuilder.php new file mode 100644 index 000000000..a175961ba --- /dev/null +++ b/web/modules/contrib/crop/src/CropTypeListBuilder.php @@ -0,0 +1,136 @@ +urlGenerator = $url_generator; + $this->queryFactory = $query_factory; + } + + /** + * {@inheritdoc} + */ + public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { + return new static( + $entity_type, + $container->get('entity.manager')->getStorage($entity_type->id()), + $container->get('url_generator'), + $container->get('entity.query') + ); + } + + /** + * {@inheritdoc} + */ + public function buildHeader() { + $header = []; + $header['name'] = t('Name'); + $header['description'] = [ + 'data' => $this->t('Description'), + 'class' => [RESPONSIVE_PRIORITY_MEDIUM], + ]; + $header['aspect_ratio'] = [ + 'data' => $this->t('Aspect Ratio'), + ]; + $header['usage'] = $this->t('Used in'); + return $header + parent::buildHeader(); + } + + /** + * {@inheritdoc} + */ + public function buildRow(EntityInterface $entity) { + $row = []; + $row['name'] = [ + 'data' => $entity->label(), + 'class' => ['menu-label'], + ]; + $row['description'] = Xss::filterAdmin($entity->description); + $row['aspect_ratio'] = $entity->getAspectRatio(); + + // Load all image styles used by the current crop type. + $image_style_ids = $this->queryFactory->get('image_style') + ->condition('effects.*.data.crop_type', $entity->id()) + ->execute(); + $image_styles = ImageStyle::loadMultiple($image_style_ids); + + /** @var \Drupal\image\Entity\ImageStyle $image_style */ + $usage = []; + foreach ($image_styles as $image_style) { + if (count($usage) < 2) { + $usage[] = $image_style->link(); + } + } + + $other_image_styles = array_splice($image_styles, 2); + if ($other_image_styles) { + $usage_message = t('@first, @second and @count more', [ + '@first' => $usage[0], + '@second' => $usage[1], + '@count' => count($other_image_styles), + ]); + } + else { + $usage_message = implode(', ', $usage); + } + $row['usage']['data']['#markup'] = $usage_message; + + return $row + parent::buildRow($entity); + } + + /** + * {@inheritdoc} + */ + public function render() { + $build = parent::render(); + $build['table']['#empty'] = t('No crop types available. Add crop type.', [ + '@link' => $this->urlGenerator->generateFromRoute('crop.type_add'), + ]); + return $build; + } + +}