|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Drupal\phpstan_fixtures\Entity; |
| 4 | + |
| 5 | +use Drupal\Core\Entity\ContentEntityBase; |
| 6 | +use Drupal\Core\Entity\EntityTypeInterface; |
| 7 | +use Drupal\Core\Field\BaseFieldDefinition; |
| 8 | +use Drupal\user\UserInterface; |
| 9 | + |
| 10 | +final class ReflectionEntityTest extends ContentEntityBase { |
| 11 | + |
| 12 | + public static function baseFieldDefinitions(EntityTypeInterface $entity_type) |
| 13 | + { |
| 14 | + $fields = parent::baseFieldDefinitions($entity_type); |
| 15 | + $fields['user_id'] = BaseFieldDefinition::create('entity_reference') |
| 16 | + ->setLabel(t('User ID')) |
| 17 | + ->setDescription(t('The ID of the associated user.')) |
| 18 | + ->setSetting('target_type', 'user') |
| 19 | + ->setSetting('handler', 'default') |
| 20 | + // Default EntityTest entities to have the root user as the owner, to |
| 21 | + // simplify testing. |
| 22 | + ->setDefaultValue([0 => ['target_id' => 1]]) |
| 23 | + ->setTranslatable(TRUE) |
| 24 | + ->setDefaultValueCallback([static::class, 'getDefaultEntityOwner']) |
| 25 | + ->setDisplayOptions('form', [ |
| 26 | + 'type' => 'entity_reference_autocomplete', |
| 27 | + 'weight' => -1, |
| 28 | + 'settings' => [ |
| 29 | + 'match_operator' => 'CONTAINS', |
| 30 | + 'size' => '60', |
| 31 | + 'placeholder' => '', |
| 32 | + ], |
| 33 | + ]); |
| 34 | + return $fields; |
| 35 | + } |
| 36 | + |
| 37 | + public function getOwnerId(): int { |
| 38 | + return $this->get('user_id')->target_id; |
| 39 | + } |
| 40 | + |
| 41 | + public function setOwnerId($uid): ReflectionEntityTest { |
| 42 | + $this->set('user_id', $uid); |
| 43 | + |
| 44 | + return $this; |
| 45 | + } |
| 46 | + |
| 47 | + public function getOwner(): UserInterface { |
| 48 | + return $this->get('user_id')->entity; |
| 49 | + } |
| 50 | + |
| 51 | + public function setOwner(UserInterface $account): ReflectionEntityTest { |
| 52 | + $this->set('user_id', $account); |
| 53 | + |
| 54 | + return $this; |
| 55 | + } |
| 56 | + |
| 57 | + public static function getDefaultEntityOwner(): int { |
| 58 | + return \Drupal::currentUser()->id(); |
| 59 | + } |
| 60 | + |
| 61 | +} |
0 commit comments