class ViewsTestEntity

Same name and namespace in other branches
  1. 11.x core/modules/views/tests/src/Kernel/Entity/EntityViewsDataTest.php \Drupal\Tests\views\Kernel\Entity\ViewsTestEntity
  2. 10 core/modules/views/tests/src/Kernel/Entity/EntityViewsDataTest.php \Drupal\Tests\views\Kernel\Entity\ViewsTestEntity

Generic entity class for our test entity types.

Allows mocked base field definitions.

Hierarchy

Expanded class hierarchy of ViewsTestEntity

File

core/modules/views/tests/src/Kernel/Entity/EntityViewsDataTest.php, line 831

Namespace

Drupal\Tests\views\Kernel\Entity
View source
class ViewsTestEntity extends ContentEntityBase {
  
  /**
   * The mocked base fields for test entity types.
   *
   * An array keyed by entity type ID, whose values are arrays of base field
   * definitions.
   *
   * @var array
   */
  protected static $mockedBaseFieldDefinitions = [];
  
  /**
   * Sets up the mocked base field definitions.
   *
   * @param string $entity_type_id
   *   The entity type ID.
   * @param array $definitions
   *   The array of base field definitions to mock. These are added to the
   *   defaults ones from the parent class.
   */
  public static function setMockedBaseFieldDefinitions(string $entity_type_id, array $definitions) {
    static::$mockedBaseFieldDefinitions[$entity_type_id] = $definitions;
  }
  
  /**
   * {@inheritdoc}
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields = parent::baseFieldDefinitions($entity_type);
    if (isset(static::$mockedBaseFieldDefinitions[$entity_type->id()])) {
      $mocked_fields = static::$mockedBaseFieldDefinitions[$entity_type->id()];
      // Mocked fields take priority over ones from the base class.
      $fields = $mocked_fields + $fields;
    }
    return $fields;
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.