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; } }