You are here

public function BackgroundImageListBuilder::buildRow in Background Image 2.0.x

Same name and namespace in other branches
  1. 8 src/BackgroundImageListBuilder.php \Drupal\background_image\BackgroundImageListBuilder::buildRow()
  2. 2.x src/BackgroundImageListBuilder.php \Drupal\background_image\BackgroundImageListBuilder::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/BackgroundImageListBuilder.php, line 40

Class

BackgroundImageListBuilder
Provides a list controller for background_image entity.

Namespace

Drupal\background_image

Code

public function buildRow(EntityInterface $entity) {

  /* @var \Drupal\background_image\BackgroundImageInterface $entity */
  $background_image = $entity instanceof BackgroundImageInterface ? $entity : NULL;
  if (!$background_image) {
    return parent::buildRow($entity);
  }
  $manager = BackgroundImageManager::service();
  $build = [];
  if ($file = $background_image
    ->getImageFile()) {
    $build = [
      '#theme' => 'image_style',
      '#style_name' => $manager
        ->getPreloadImageStyle(),
      '#uri' => $file
        ->getFileUri(),
    ];

    // Add the file entity to the cache dependencies.
    // This will clear our cache when this entity updates.

    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');
    $renderer
      ->addCacheableDependency($build, $file);
  }
  if ($build) {
    $row['image'] = [
      'data' => [
        '#type' => 'link',
        '#title' => $build,
        '#url' => $background_image
          ->toUrl(),
      ],
    ];
  }
  else {
    $row['image'] = $this
      ->t('N/A');
  }
  $row['type'] = $background_image
    ->getTypeLabel(TRUE);
  $settings = [];
  foreach ($background_image
    ->getSettings()
    ->getOverridden() as $key => $value) {
    if ($key === 'preload') {
      continue;
    }
    if (is_bool($value)) {
      $value = $value ? $this
        ->t('Yes') : $this
        ->t('No');
    }
    $label = Unicode::ucfirst(str_replace([
      '_',
      '-',
      '.',
    ], ' ', $key));
    $settings[] = is_array($value) ? $label : $label . ': ' . $value;
  }
  $row['settings'] = $settings ? implode(', ', $settings) : $this
    ->t('None');
  return $row + parent::buildRow($background_image);
}