3 namespace Drupal\ctools\Testing;
6 use Drupal\Component\Render\FormattableMarkup;
8 trait EntityCreationTrait {
11 * The entity type manager.
13 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
15 protected $entityTypeManager;
18 * Creates a custom content type based on default settings.
20 * @param string $entity_type
21 * The type of entity to create.
22 * @param array $values
23 * An array of settings to change from the defaults.
24 * Example: 'type' => 'foo'.
26 * @return \Drupal\Core\Entity\EntityInterface
29 protected function createEntity($entity_type, array $values = array()) {
30 $storage = $this->getEntityTypeManager()->getStorage($entity_type);
31 $entity = $storage->create($values);
32 $status = $entity->save();
33 \Drupal::service('router.builder')->rebuild();
35 if ($this instanceof \PHPUnit_Framework_TestCase) {
36 $this->assertSame($status, SAVED_NEW, (new FormattableMarkup('Created entity %id of type %type.', ['%id' => $entity->id(), '%type' => $entity_type]))->__toString());
39 $this->assertEqual($status, SAVED_NEW, (new FormattableMarkup('Created entity %id of type %type.', ['%id' => $entity->id(), '%type' => $entity_type]))->__toString());
46 * @return \Drupal\Core\Entity\EntityTypeManagerInterface
48 protected function getEntityTypeManager() {
49 if (!isset($this->entityTypeManager)) {
50 $this->entityTypeManager = $this->container->get('entity_type.manager');
52 return $this->entityTypeManager;