You are here

public function CropTypeListBuilder::buildRow in Crop API 8

Same name and namespace in other branches
  1. 8.2 src/CropTypeListBuilder.php \Drupal\crop\CropTypeListBuilder::buildRow()

Builds a row for an entity in the entity listing.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity for this row of the list.

Return value

array A render array structure of fields for this entity.

Overrides EntityListBuilder::buildRow

See also

\Drupal\Core\Entity\EntityListBuilder::render()

File

src/CropTypeListBuilder.php, line 86

Class

CropTypeListBuilder
Defines a class to build a listing of crop type entities.

Namespace

Drupal\crop

Code

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