You are here

public static function ApiDoc::baseFieldDefinitions in Apigee API Catalog 8

Same name and namespace in other branches
  1. 8.2 src/Entity/ApiDoc.php \Drupal\apigee_api_catalog\Entity\ApiDoc::baseFieldDefinitions()

Provides base field definitions for an entity type.

Implementations typically use the class \Drupal\Core\Field\BaseFieldDefinition for creating the field definitions; for example a 'name' field could be defined as the following:

$fields['name'] = BaseFieldDefinition::create('string')
  ->setLabel(t('Name'));

By definition, base fields are fields that exist for every bundle. To provide definitions for fields that should only exist on some bundles, use \Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions().

The definitions returned by this function can be overridden for all bundles by hook_entity_base_field_info_alter() or overridden on a per-bundle basis via 'base_field_override' configuration entities.

Parameters

\Drupal\Core\Entity\EntityTypeInterface $entity_type: The entity type definition. Useful when a single class is used for multiple, possibly dynamic entity types.

Return value

\Drupal\Core\Field\FieldDefinitionInterface[] An array of base field definitions for the entity type, keyed by field name.

Overrides EditorialContentEntityBase::baseFieldDefinitions

See also

\Drupal\Core\Entity\EntityFieldManagerInterface::getFieldDefinitions()

\Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions()

File

src/Entity/ApiDoc.php, line 201

Class

ApiDoc
Defines the API Doc entity.

Namespace

Drupal\apigee_api_catalog\Entity

Code

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 API.'))
    ->setRevisionable(TRUE)
    ->setSettings([
    'max_length' => 255,
    'text_processing' => 0,
  ])
    ->setDefaultValue('')
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'string',
    'weight' => -4,
  ])
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => -4,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE)
    ->setRequired(TRUE);
  $fields['description'] = BaseFieldDefinition::create('text_long')
    ->setLabel(t('Description'))
    ->setDescription(t('Description of the API.'))
    ->setRevisionable(TRUE)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'text_default',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('view', TRUE)
    ->setDisplayOptions('form', [
    'type' => 'text_textfield',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('form', TRUE);
  $fields['spec_file_source'] = BaseFieldDefinition::create('list_string')
    ->setLabel(t('Specification source type'))
    ->setDescription(t('Indicate if the OpenAPI spec will be provided as a
                          file for upload or a URL.'))
    ->setDefaultValue(ApiDocInterface::SPEC_AS_FILE)
    ->setRequired(TRUE)
    ->setSetting('allowed_values', [
    ApiDocInterface::SPEC_AS_FILE => t('File'),
    ApiDocInterface::SPEC_AS_URL => t('URL'),
  ])
    ->setDisplayOptions('form', [
    'type' => 'options_buttons',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('form', FALSE)
    ->setDisplayConfigurable('view', FALSE);
  $fields['spec'] = BaseFieldDefinition::create('file')
    ->setLabel(t('OpenAPI specification'))
    ->setDescription(t('The spec snapshot.'))
    ->setRevisionable(TRUE)
    ->setSettings([
    'file_directory' => 'apidoc_specs',
    'file_extensions' => 'yml yaml json',
    'handler' => 'default:file',
    'text_processing' => 0,
  ])
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'apigee_api_catalog_smartdocs',
    'weight' => 0,
  ])
    ->setDisplayOptions('form', [
    'label' => 'hidden',
    'type' => 'file_generic',
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['file_link'] = BaseFieldDefinition::create('file_link')
    ->setLabel(t('URL to OpenAPI specification file'))
    ->setDescription(t('The URL to an OpenAPI file spec.'))
    ->addConstraint('ApiDocFileLink')
    ->setSettings([
    'file_extensions' => 'yml yaml json',
    'link_type' => LinkItemInterface::LINK_GENERIC,
    'title' => DRUPAL_DISABLED,
  ])
    ->setDisplayOptions('form', [
    'weight' => 0,
  ])
    ->setDisplayConfigurable('form', FALSE)
    ->setDisplayConfigurable('view', FALSE);
  $fields['spec_md5'] = BaseFieldDefinition::create('string')
    ->setLabel(t('OpenAPI specification file MD5'))
    ->setDescription(t('OpenAPI specification file MD5'))
    ->setSettings([
    'text_processing' => 0,
  ])
    ->setDisplayConfigurable('form', FALSE)
    ->setDisplayConfigurable('view', FALSE);
  $fields['api_product'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('API Product'))
    ->setDescription(t('The API Product this API is associated with.'))
    ->setRevisionable(TRUE)
    ->setSetting('target_type', 'api_product')
    ->setDisplayConfigurable('form', FALSE)
    ->setDisplayConfigurable('view', FALSE);
  $fields['status']
    ->setLabel(t('Publishing status'))
    ->setDescription(t('A boolean indicating whether the API Doc is published.'))
    ->setDisplayOptions('form', [
    'type' => 'boolean_checkbox',
    'weight' => 1,
  ])
    ->setDisplayConfigurable('form', TRUE);
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Created'))
    ->setDescription(t('The time that the entity was created.'))
    ->setRevisionable(TRUE);
  $fields['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed'))
    ->setDescription(t('The time that the entity was last edited.'))
    ->setRevisionable(TRUE);
  $fields['fetched_timestamp'] = BaseFieldDefinition::create('timestamp')
    ->setLabel(t('Spec fetched from URL timestamp'))
    ->setDescription(t('When the OpenAPI spec file was last fetched from URL as a Unix timestamp.'));

  // Store whether product access should be checked per entity.
  $fields['product_access_control'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('`product_access_control` placeholder'))
    ->setDisplayConfigurable('form', FALSE)
    ->setDisplayConfigurable('view', FALSE);
  return $fields;
}