Skip to content

Instantly share code, notes, and snippets.

@kbrinner
Created February 12, 2019 21:50
Show Gist options
  • Select an option

  • Save kbrinner/930adb5c08415bd9891015ce4e0fa87e to your computer and use it in GitHub Desktop.

Select an option

Save kbrinner/930adb5c08415bd9891015ce4e0fa87e to your computer and use it in GitHub Desktop.
Create content programmatically
/**
* Creates install content.
*
* @param array $content
* Content keyed by entity-type and UUID.
*
* @return array
* An array of entities created, keyed like the parameter.
*/
function _create_content(array $content) {
$entities = [];
foreach ($content as $entity_type_id => $items) {
try {
$storage = \Drupal::entityTypeManager()->getStorage($entity_type_id);
}
catch (Exception $e) {
watchdog_exception('moodys_helper', $e, t("Entity type doesn't exist or storage handler couldn't be loaded."));
}
foreach ($items as $uuid => $item) {
$entity = $storage->create($item + ['uuid' => $uuid]);
try {
$entity->save();
}
catch (Exception $e) {
watchdog_exception('moodys_helper', $e, t("Error trying to create new content."));
return $entities;
}
$entities[$entity_type_id][$entity->id()] = $entity;
}
}
return $entities;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment