SearchEngineListBuilder.php 2.52 KB
Newer Older
thejimbirch's avatar
thejimbirch committed
1 2 3 4 5 6 7 8 9
<?php

namespace Drupal\simple_sitemap_engines\Controller;

use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
gbyte's avatar
gbyte committed
10
use Drupal\simple_sitemap\Form\FormHelper;
thejimbirch's avatar
thejimbirch committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Search engine entity list builder.
 */
class SearchEngineListBuilder extends ConfigEntityListBuilder {

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * SearchEngineListBuilder constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
   *   The entity storage class.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter service.
   */
  public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter) {
    parent::__construct($entity_type, $storage);
    $this->dateFormatter = $date_formatter;
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static(
      $entity_type,
      $container->get('entity_type.manager')->getStorage($entity_type->id()),
      $container->get('date.formatter')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header['label'] = $this->t('Name');
    $header['url'] = $this->t('Submission URL');
    $header['status'] = $this->t('Status');
    $header['last_submitted'] = $this->t('Last submitted');
    return $header;
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {
    /** @var \Drupal\simple_sitemap_engines\Entity\SearchEngine $entity */

    $row['label'] = $entity->label();
    $row['url'] = $entity->url;
    $row['status'] = $entity->status ? $this->t('Enabled') : $this->t('Disabled');
    $row['last_submitted'] = $entity->last_submitted
      ? $this->dateFormatter->format($entity->last_submitted, 'short')
      : $this->t('Never');

    return $row;
  }

gbyte's avatar
gbyte committed
78 79 80 81 82 83 84 85 86
  public function render() {
    return ['simple_sitemap_engines' => [
      '#prefix' => FormHelper::getDonationText(),
      '#title' => $this->t('Submission status'),
      '#type' => 'fieldset',
      'table' => parent::render(),
    ]];
  }

thejimbirch's avatar
thejimbirch committed
87
}