Commit 6131ff1d authored by webchick's avatar webchick

Issue #2096899 by amateescu, msonnabaum | Berdir: Add ::create() to simplify creating new entities.

parent e27542b4
......@@ -431,6 +431,13 @@ public static function loadMultiple(array $ids = NULL) {
return \Drupal::entityManager()->getStorage(static::getEntityTypeFromStaticClass())->loadMultiple($ids);
}
/**
* {@inheritdoc}
*/
public static function create(array $values = array()) {
return \Drupal::entityManager()->getStorage(static::getEntityTypeFromStaticClass())->create($values);
}
/**
* Returns the entity type ID based on the class that is called on.
*
......
......@@ -193,6 +193,18 @@ public static function load($id);
*/
public static function loadMultiple(array $ids = NULL);
/**
* Constructs a new entity object, without permanently saving it.
*
* @param array $values
* (optional) An array of values to set, keyed by property name. If the
* entity type has bundles, the bundle key has to be specified.
*
* @return static
* The entity object.
*/
public static function create(array $values = array());
/**
* Saves an entity permanently.
*
......
......@@ -137,8 +137,7 @@ function shortcut_set_switch_submit($form, &$form_state) {
if ($form_state['values']['set'] == 'new') {
// Save a new shortcut set with links copied from the user's default set.
$default_set = shortcut_default_set($account);
$set = entity_create('shortcut_set', array(
$set = ShortcutSet::create(array(
'id' => $form_state['values']['id'],
'label' => $form_state['values']['label'],
));
......
......@@ -7,6 +7,7 @@
namespace Drupal\shortcut\Tests;
use Drupal\shortcut\Entity\Shortcut;
use Drupal\system\Tests\Entity\EntityCacheTagsTestBase;
/**
......@@ -44,7 +45,7 @@ public function setUp() {
*/
protected function createEntity() {
// Create a "Llama" shortcut.
$shortcut = entity_create('shortcut', array(
$shortcut = Shortcut::create(array(
'set' => 'default',
'title' => t('Llama'),
'weight' => 0,
......
......@@ -7,6 +7,7 @@
namespace Drupal\shortcut\Tests;
use Drupal\shortcut\Entity\Shortcut;
use Drupal\shortcut\Entity\ShortcutSet;
use Drupal\shortcut\ShortcutSetInterface;
use Drupal\simpletest\WebTestBase;
......@@ -54,7 +55,7 @@ function setUp() {
$this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
// Populate the default shortcut set.
$shortcut = entity_create('shortcut', array(
$shortcut = Shortcut::create(array(
'set' => 'default',
'title' => t('Add content'),
'weight' => -20,
......@@ -62,7 +63,7 @@ function setUp() {
));
$shortcut->save();
$shortcut = entity_create('shortcut', array(
$shortcut = Shortcut::create(array(
'set' => 'default',
'title' => t('All content'),
'weight' => -19,
......@@ -88,7 +89,7 @@ function setUp() {
* Creates a generic shortcut set.
*/
function generateShortcutSet($label = '', $id = NULL) {
$set = entity_create('shortcut_set', array(
$set = ShortcutSet::create(array(
'id' => isset($id) ? $id : strtolower($this->randomName()),
'label' => empty($label) ? $this->randomString() : $label,
));
......
......@@ -903,6 +903,13 @@ public static function loadMultiple(array $ids = NULL) {
return View::loadMultiple($ids);
}
/**
* {@inheritdoc}
*/
public static function create(array $values = array()) {
return View::create($values);
}
/**
* Implements \Drupal\Core\Entity\EntityInterface::delete().
*/
......
......@@ -262,6 +262,7 @@ function setupTestLoad() {
$methods = get_class_methods('Drupal\entity_test\Entity\EntityTestMul');
unset($methods[array_search('load', $methods)]);
unset($methods[array_search('loadMultiple', $methods)]);
unset($methods[array_search('create', $methods)]);
$this->entity = $this->getMockBuilder('Drupal\entity_test\Entity\EntityTestMul')
->disableOriginalConstructor()
->setMethods($methods)
......@@ -474,6 +475,29 @@ public function testLoadMultipleSubClass() {
$this->assertSame(array(1 => $this->entity), $class::loadMultiple(array(1)));
}
/**
* @covers ::create
* @covers ::getEntityTypeFromStaticClass
*/
public function testCreate() {
$this->setupTestLoad();
$storage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface');
$storage->expects($this->once())
->method('create')
->with(array())
->will($this->returnValue($this->entity));
$this->entityManager->expects($this->once())
->method('getStorage')
->with($this->entityTypeId)
->will($this->returnValue($storage));
// Call Entity::create() statically and check that it returns the mock
// entity.
$class = get_class($this->entity);
$this->assertSame($this->entity, $class::create(array()));
}
/**
* @covers ::save
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment