You are here

OpenApiTestEntity.php in OpenAPI 8

Same filename and directory in other branches
  1. 8.2 tests/modules/openapi_test/src/Entity/OpenApiTestEntity.php

File

tests/modules/openapi_test/src/Entity/OpenApiTestEntity.php
View source
<?php

namespace Drupal\openapi_test\Entity;

use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;

/**
 * Defines the OpenApi Test Entity.
 *
 * @ingroup openapi_test
 *
 * @ContentEntityType(
 *   id = "openapi_test_entity",
 *   label = @Translation("OpenApi Test Entity"),
 *   bundle_label = @Translation("OpenApi Test Entity type"),
 *   base_table = "openapi_test_entity",
 *   admin_permission = "administer site configuration",
 *   entity_keys = {
 *     "id" = "id",
 *     "bundle" = "type",
 *     "label" = "name",
 *     "uuid" = "uuid"
 *   },
 *   bundle_entity_type = "openapi_test_entity_type"
 * )
 */
class OpenApiTestEntity extends ContentEntityBase implements OpenApiTestEntityInterface {
  use EntityChangedTrait;

  /**
   * {@inheritdoc}
   */
  public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
    parent::preCreate($storage_controller, $values);
    $values += [
      'user_id' => \Drupal::currentUser()
        ->id(),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getName() {
    return $this
      ->get('name')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setName($name) {
    $this
      ->set('name', $name);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields = parent::baseFieldDefinitions($entity_type);
    $fields['name'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Name'))
      ->setDescription(t('The name of the OpenApi Test Entity entity.'))
      ->setSettings([
      'max_length' => 50,
      'text_processing' => 0,
    ])
      ->setDefaultValue('')
      ->setDisplayOptions('view', [
      'label' => 'above',
      'type' => 'string',
      'weight' => -4,
    ])
      ->setDisplayOptions('form', [
      'type' => 'string_textfield',
      'weight' => -4,
    ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

    // @see \Drupal\Tests\openapi\Kernel\JsonApiGeneratorTest::testGetPaths()
    if (\Drupal::moduleHandler()
      ->moduleExists('menu_ui')) {
      $fields['menu_link'] = BaseFieldDefinition::create('entity_reference')
        ->setLabel(t('Menu link'))
        ->setSetting('target_type', 'menu_link_content')
        ->setComputed(TRUE);
    }
    return $fields;
  }

}

Classes

Namesort descending Description
OpenApiTestEntity Defines the OpenApi Test Entity.