You are here

public function ContentLoader::entityExists in YAML Content 8

Query if a target entity already exists and should be updated.

@todo Potentially move this into a separate helper class.

Parameters

string $entity_type: The type of entity being imported.

array $content_data: The import content structure representing the entity being searched for.

Return value

\Drupal\Core\Entity\EntityInterface|false Return a matching entity if one is found, or FALSE otherwise.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

1 call to ContentLoader::entityExists()
ContentLoader::createEntity in src/ContentLoader/ContentLoader.php
Create the entity based on basic properties.

File

src/ContentLoader/ContentLoader.php, line 595

Class

ContentLoader
ContentLoader class for parsing and importing YAML content.

Namespace

Drupal\yaml_content\ContentLoader

Code

public function entityExists($entity_type, array $content_data) {

  // Some entities require special handling to determine if it exists.
  switch ($entity_type) {

    // Always create new paragraphs since they're not reusable.
    // @todo Should new revisions be incorporated here?
    case 'paragraph':
      break;
    case 'media':

      // @todo Add special handling to check file name or path.
      break;
    default:

      // Load entity type handler.
      $entity_handler = $this
        ->getEntityStorage($entity_type);

      // @todo Load this through dependency injection instead.
      $query = \Drupal::entityQuery($entity_type);
      foreach ($content_data as $key => $value) {
        if ($key != 'entity' && !is_array($value)) {
          $query
            ->condition($key, $value);
        }
      }
      $entity_ids = $query
        ->execute();
      if ($entity_ids) {
        $entity_id = array_shift($entity_ids);
        $entity = $entity_handler
          ->load($entity_id);
      }
  }
  return isset($entity) ? $entity : FALSE;
}