From 6131ff1d8d76a09e9ce3733c0f067e63be2c0fe3 Mon Sep 17 00:00:00 2001 From: webchick <webchick@24967.no-reply.drupal.org> Date: Tue, 20 May 2014 14:27:11 -0700 Subject: [PATCH] Issue #2096899 by amateescu, msonnabaum | Berdir: Add ::create() to simplify creating new entities. --- core/lib/Drupal/Core/Entity/Entity.php | 7 ++++++ .../Drupal/Core/Entity/EntityInterface.php | 12 ++++++++++ core/modules/shortcut/shortcut.admin.inc | 3 +-- .../src/Tests/ShortcutCacheTagsTest.php | 3 ++- .../shortcut/src/Tests/ShortcutTestBase.php | 7 +++--- .../views_ui/lib/Drupal/views_ui/ViewUI.php | 7 ++++++ .../Tests/Core/Entity/EntityUnitTest.php | 24 +++++++++++++++++++ 7 files changed, 57 insertions(+), 6 deletions(-) diff --git a/core/lib/Drupal/Core/Entity/Entity.php b/core/lib/Drupal/Core/Entity/Entity.php index b69ba1dfb209..557ddf3c4a8b 100644 --- a/core/lib/Drupal/Core/Entity/Entity.php +++ b/core/lib/Drupal/Core/Entity/Entity.php @@ -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. * diff --git a/core/lib/Drupal/Core/Entity/EntityInterface.php b/core/lib/Drupal/Core/Entity/EntityInterface.php index 4e483df13eda..4a2f69ccc161 100644 --- a/core/lib/Drupal/Core/Entity/EntityInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityInterface.php @@ -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. * diff --git a/core/modules/shortcut/shortcut.admin.inc b/core/modules/shortcut/shortcut.admin.inc index f2f938d68f83..2444eecd59c3 100644 --- a/core/modules/shortcut/shortcut.admin.inc +++ b/core/modules/shortcut/shortcut.admin.inc @@ -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'], )); diff --git a/core/modules/shortcut/src/Tests/ShortcutCacheTagsTest.php b/core/modules/shortcut/src/Tests/ShortcutCacheTagsTest.php index 6a9c78aa1223..a221b0e25388 100644 --- a/core/modules/shortcut/src/Tests/ShortcutCacheTagsTest.php +++ b/core/modules/shortcut/src/Tests/ShortcutCacheTagsTest.php @@ -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, diff --git a/core/modules/shortcut/src/Tests/ShortcutTestBase.php b/core/modules/shortcut/src/Tests/ShortcutTestBase.php index 461a40cc78e9..6d9e2405b9d6 100644 --- a/core/modules/shortcut/src/Tests/ShortcutTestBase.php +++ b/core/modules/shortcut/src/Tests/ShortcutTestBase.php @@ -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, )); diff --git a/core/modules/views_ui/lib/Drupal/views_ui/ViewUI.php b/core/modules/views_ui/lib/Drupal/views_ui/ViewUI.php index e1e668032e4b..52d99b259877 100644 --- a/core/modules/views_ui/lib/Drupal/views_ui/ViewUI.php +++ b/core/modules/views_ui/lib/Drupal/views_ui/ViewUI.php @@ -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(). */ diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php index 9e600c6cdb77..3ecc8de06698 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php @@ -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 */ -- GitLab