You are here

FieldValidationRuleSetListBuilder.php in Field Validation 8

File

src/FieldValidationRuleSetListBuilder.php
View source
<?php

namespace Drupal\field_validation;

use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Defines a class to build a listing of FieldValidationRuleSet entities.
 *
 * @see \Drupal\field_validation\Entity\FieldValidationRuleSet
 */
class FieldValidationRuleSetListBuilder extends ConfigEntityListBuilder {

  /**
   * The URL generator.
   *
   * @var \Drupal\Core\Routing\UrlGeneratorInterface
   */
  protected $urlGenerator;

  /**
   * Constructs a new FieldValidationRuleSetListBuilder object.
   *
   * @param EntityTypeInterface $entity_type
   *   The entity type definition.
   * @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
   *   The blocktabs entity storage class.
   * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
   *   The URL generator.
   */
  public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $entity_storage, UrlGeneratorInterface $url_generator) {
    parent::__construct($entity_type, $entity_storage);
    $this->urlGenerator = $url_generator;
  }

  /**
   * {@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('url_generator'), $container
      ->get('string_translation'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header['label'] = $this
      ->t('Rule set name');
    return $header + parent::buildHeader();
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {
    $row['label'] = $entity
      ->label();
    return $row + parent::buildRow($entity);
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultOperations(EntityInterface $entity) {
    return parent::getDefaultOperations($entity);
  }

  /**
   * {@inheritdoc}
   */
  public function render() {
    $build = parent::render();
    $build['#empty'] = $this
      ->t('There are currently no rule set. <a href=":url">Add a new one</a>.', [
      ':url' => $this->urlGenerator
        ->generateFromRoute('field_validation.field_validation_rule_set_add'),
    ]);
    return $build;
  }

}

Classes

Namesort descending Description
FieldValidationRuleSetListBuilder Defines a class to build a listing of FieldValidationRuleSet entities.