You are here

public function ContentLoader::populateEntityFields in YAML Content 8

Populate entity field data into an entity object.

@todo Add events for overall pre/post field import process. @todo Throw more specific exceptions.

Parameters

\Drupal\Core\Entity\FieldableEntityInterface $entity: The entity object being populated for import.

array $fields: Content import data for entity fields keyed by field name.

Throws

\Drupal\Core\Field\FieldException

\Drupal\Core\Entity\EntityStorageException

1 call to ContentLoader::populateEntityFields()
ContentLoader::buildEntity in src/ContentLoader/ContentLoader.php
Build an entity from the provided content data.

File

src/ContentLoader/ContentLoader.php, line 447

Class

ContentLoader
ContentLoader class for parsing and importing YAML content.

Namespace

Drupal\yaml_content\ContentLoader

Code

public function populateEntityFields(FieldableEntityInterface $entity, array $fields) {
  foreach ($fields as $field_name => $field_data) {
    try {
      if ($entity
        ->hasField($field_name)) {
        $field_instance = $entity
          ->get($field_name);

        // Dispatch field import event prior to populating fields.
        $field_import_event = new FieldImportEvent($this, $entity, $field_instance, $field_data);
        $this
          ->getEventDispatcher()
          ->dispatch(YamlContentEvents::IMPORT_FIELD, $field_import_event);
        $this
          ->populateField($field_instance, $field_data);
      }
      else {
        throw new FieldException('Undefined field: ' . $field_name);
      }
    } catch (MissingDataException $exception) {
      watchdog_exception('yaml_content', $exception);
    } catch (PluginNotFoundException $exception) {
      watchdog_exception('yaml_content', $exception);
    }
  }
}