You are here

public function SchemaFactory::create in Schemata 8

Assemble a schema object based on the requested entity type.

Parameters

string $entity_type: URL input specifying an entity type to be processed.

string $bundle: URL input specifying an entity bundle to be processed. May be NULL for support of entities that do not have bundles.

Return value

\Drupal\schemata\Schema\SchemaInterface A Schema object which can be processed as a Rest Resource response.

File

src/SchemaFactory.php, line 82

Class

SchemaFactory
Create an object of type Drupal\schemata\Schema\SchemaInterface.

Namespace

Drupal\schemata

Code

public function create($entity_type, $bundle = NULL) {
  try {
    $entity_type_plugin = $this
      ->getSourceEntityPlugin($entity_type);
  } catch (\Exception $e) {
    $this->logger
      ->error($e
      ->getMessage());

    // @todo Handle these exceptions in https://www.drupal.org/node/2868562.
    return NULL;
  }
  if ($entity_type_plugin
    ->getBundleEntityType()) {
    $bundles = $this->entityTypeBundleInfo
      ->getBundleInfo($entity_type);
  }
  if (!empty($bundle) && !array_key_exists($bundle, $bundles)) {
    $this->logger
      ->warning('Specified Entity Bundle "%bundle" does not exist.', [
      '%bundle' => $bundle,
    ]);
    return NULL;
  }
  $class = $entity_type === 'node' && !empty($bundle) ? NodeSchema::class : Schema::class;
  $data_definition = $this
    ->dataDefinitionFactory($entity_type, $bundle);
  $schema = new $class($data_definition, $bundle, $data_definition
    ->getPropertyDefinitions());
  $this->logger
    ->notice('Schema generated for Entity Type (%entity_type) and Bundle (%bundle).', [
    '%entity_type' => $entity_type,
    '%bundle' => empty($bundle) ? 'N/A' : $bundle,
  ]);
  return $schema;
}