Created
February 12, 2019 21:50
-
-
Save kbrinner/930adb5c08415bd9891015ce4e0fa87e to your computer and use it in GitHub Desktop.
Create content programmatically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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