Commit 3f7f953f authored by alexpott's avatar alexpott

Issue #2377113 by JeroenT, mglaman, LinL, pcambra: Replace all instances of...

Issue #2377113 by JeroenT, mglaman, LinL, pcambra: Replace all instances of entity_load('block_content') and entity_load_multiple('block_content') with static method calls
parent 38ea2f11
...@@ -27,6 +27,13 @@ class BlockContentForm extends ContentEntityForm { ...@@ -27,6 +27,13 @@ class BlockContentForm extends ContentEntityForm {
*/ */
protected $blockContentStorage; protected $blockContentStorage;
/**
* The custom block type storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $blockContentTypeStorage;
/** /**
* The language manager. * The language manager.
* *
...@@ -48,12 +55,15 @@ class BlockContentForm extends ContentEntityForm { ...@@ -48,12 +55,15 @@ class BlockContentForm extends ContentEntityForm {
* The entity manager. * The entity manager.
* @param \Drupal\Core\Entity\EntityStorageInterface $block_content_storage * @param \Drupal\Core\Entity\EntityStorageInterface $block_content_storage
* The custom block storage. * The custom block storage.
* @param \Drupal\Core\Entity\EntityStorageInterface $block_content_type_storage
* The custom block type storage.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager. * The language manager.
*/ */
public function __construct(EntityManagerInterface $entity_manager, EntityStorageInterface $block_content_storage, LanguageManagerInterface $language_manager) { public function __construct(EntityManagerInterface $entity_manager, EntityStorageInterface $block_content_storage, EntityStorageInterface $block_content_type_storage, LanguageManagerInterface $language_manager) {
parent::__construct($entity_manager); parent::__construct($entity_manager);
$this->blockContentStorage = $block_content_storage; $this->blockContentStorage = $block_content_storage;
$this->blockContentTypeStorage = $block_content_type_storage;
$this->languageManager = $language_manager; $this->languageManager = $language_manager;
} }
...@@ -65,6 +75,7 @@ public static function create(ContainerInterface $container) { ...@@ -65,6 +75,7 @@ public static function create(ContainerInterface $container) {
return new static( return new static(
$entity_manager, $entity_manager,
$entity_manager->getStorage('block_content'), $entity_manager->getStorage('block_content'),
$entity_manager->getStorage('block_content_type'),
$container->get('language_manager') $container->get('language_manager')
); );
} }
...@@ -80,7 +91,7 @@ public static function create(ContainerInterface $container) { ...@@ -80,7 +91,7 @@ public static function create(ContainerInterface $container) {
protected function prepareEntity() { protected function prepareEntity() {
$block = $this->entity; $block = $this->entity;
// Set up default values, if required. // Set up default values, if required.
$block_type = entity_load('block_content_type', $block->bundle()); $block_type = $this->blockContentTypeStorage->load($block->bundle());
if (!$block->isNew()) { if (!$block->isNew()) {
$block->setRevisionLog(NULL); $block->setRevisionLog(NULL);
} }
...@@ -170,7 +181,7 @@ public function save(array $form, FormStateInterface $form_state) { ...@@ -170,7 +181,7 @@ public function save(array $form, FormStateInterface $form_state) {
$block->save(); $block->save();
$context = array('@type' => $block->bundle(), '%info' => $block->label()); $context = array('@type' => $block->bundle(), '%info' => $block->label());
$logger = $this->logger('block_content'); $logger = $this->logger('block_content');
$block_type = entity_load('block_content_type', $block->bundle()); $block_type = $this->blockContentTypeStorage->load($block->bundle());
$t_args = array('@type' => $block_type->label(), '%info' => $block->label()); $t_args = array('@type' => $block_type->label(), '%info' => $block->label());
if ($insert) { if ($insert) {
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
namespace Drupal\block_content; namespace Drupal\block_content;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\content_translation\ContentTranslationHandler; use Drupal\content_translation\ContentTranslationHandler;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
...@@ -37,7 +38,7 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En ...@@ -37,7 +38,7 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function entityFormTitle(EntityInterface $entity) { protected function entityFormTitle(EntityInterface $entity) {
$block_type = entity_load('block_content_type', $entity->bundle()); $block_type = BlockContentType::load($entity->bundle());
return t('<em>Edit @type</em> @title', array('@type' => $block_type->label(), '@title' => $entity->label())); return t('<em>Edit @type</em> @title', array('@type' => $block_type->label(), '@title' => $entity->label()));
} }
......
...@@ -8,16 +8,47 @@ ...@@ -8,16 +8,47 @@
namespace Drupal\block_content\Plugin\Derivative; namespace Drupal\block_content\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase; use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Retrieves block plugin definitions for all custom blocks. * Retrieves block plugin definitions for all custom blocks.
*/ */
class BlockContent extends DeriverBase { class BlockContent extends DeriverBase implements ContainerDeriverInterface {
/**
* The custom block storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $blockContentStorage;
/**
* Constructs a BlockContent object.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $block_content_storage
* The custom block storage.
*/
public function __construct(EntityStorageInterface $block_content_storage) {
$this->blockContentStorage = $block_content_storage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
$entity_manager = $container->get('entity.manager');
return new static(
$entity_manager->getStorage('block_content')
);
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getDerivativeDefinitions($base_plugin_definition) { public function getDerivativeDefinitions($base_plugin_definition) {
$block_contents = entity_load_multiple('block_content'); $block_contents = $this->blockContentStorage->loadMultiple();
/** @var $block_content \Drupal\block_content\Entity\BlockContent */ /** @var $block_content \Drupal\block_content\Entity\BlockContent */
foreach ($block_contents as $block_content) { foreach ($block_contents as $block_content) {
$this->derivatives[$block_content->uuid()] = $base_plugin_definition; $this->derivatives[$block_content->uuid()] = $base_plugin_definition;
......
...@@ -7,9 +7,9 @@ ...@@ -7,9 +7,9 @@
namespace Drupal\block_content\Tests; namespace Drupal\block_content\Tests;
use Drupal\block_content\Entity\BlockContent;
use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\Unicode;
use Drupal\Core\Database\Database; use Drupal\Core\Database\Database;
use Drupal\block_content\Entity\BlockContent;
/** /**
* Create a block and test saving it. * Create a block and test saving it.
...@@ -209,7 +209,7 @@ public function testBlockDelete() { ...@@ -209,7 +209,7 @@ public function testBlockDelete() {
'settings[label]' => $edit['info[0][value]'], 'settings[label]' => $edit['info[0][value]'],
'region' => 'sidebar_first', 'region' => 'sidebar_first',
); );
$block = entity_load('block_content', 1); $block = BlockContent::load(1);
$url = 'admin/structure/block/add/block_content:' . $block->uuid() . '/' . $this->config('system.theme')->get('default'); $url = 'admin/structure/block/add/block_content:' . $block->uuid() . '/' . $this->config('system.theme')->get('default');
$this->drupalPostForm($url, $instance, t('Save block')); $this->drupalPostForm($url, $instance, t('Save block'));
...@@ -263,7 +263,7 @@ public function testConfigDependencies() { ...@@ -263,7 +263,7 @@ public function testConfigDependencies() {
'settings[label]' => $block->label(), 'settings[label]' => $block->label(),
'region' => 'sidebar_first', 'region' => 'sidebar_first',
); );
$block = entity_load('block_content', 1); $block = BlockContent::load(1);
$url = 'admin/structure/block/add/block_content:' . $block->uuid() . '/' . $this->config('system.theme')->get('default'); $url = 'admin/structure/block/add/block_content:' . $block->uuid() . '/' . $this->config('system.theme')->get('default');
$this->drupalPostForm($url, $instance, t('Save block')); $this->drupalPostForm($url, $instance, t('Save block'));
......
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
namespace Drupal\block_content\Tests; namespace Drupal\block_content\Tests;
use Drupal\block_content\Entity\BlockContent;
/** /**
* Create a block with revisions. * Create a block with revisions.
* *
...@@ -88,7 +90,7 @@ public function testRevisions() { ...@@ -88,7 +90,7 @@ public function testRevisions() {
// Verify that the non-default revision id is greater than the default // Verify that the non-default revision id is greater than the default
// revision id. // revision id.
$default_revision = entity_load('block_content', $loaded->id()); $default_revision = BlockContent::load($loaded->id());
$this->assertTrue($loaded->getRevisionId() > $default_revision->getRevisionId(), 'Revision id is greater than default revision id.'); $this->assertTrue($loaded->getRevisionId() > $default_revision->getRevisionId(), 'Revision id is greater than default revision id.');
} }
......
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
*/ */
namespace Drupal\block_content\Tests; namespace Drupal\block_content\Tests;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\Component\Utility\Html; use Drupal\Component\Utility\Html;
use Drupal\Core\Url; use Drupal\Core\Url;
...@@ -58,7 +60,7 @@ public function testBlockContentTypeCreation() { ...@@ -58,7 +60,7 @@ public function testBlockContentTypeCreation() {
// Create a block type programmaticaly. // Create a block type programmaticaly.
$type = $this->createBlockContentType('other'); $type = $this->createBlockContentType('other');
$block_type = entity_load('block_content_type', 'other'); $block_type = BlockContentType::load('other');
$this->assertTrue($block_type, 'The new block type has been created.'); $this->assertTrue($block_type, 'The new block type has been created.');
$this->drupalGet('block/add/' . $type->id()); $this->drupalGet('block/add/' . $type->id());
...@@ -70,7 +72,7 @@ public function testBlockContentTypeCreation() { ...@@ -70,7 +72,7 @@ public function testBlockContentTypeCreation() {
'label' => 'title for foo', 'label' => 'title for foo',
); );
$this->drupalPostForm('admin/structure/block/block-content/types/add', $edit, t('Save')); $this->drupalPostForm('admin/structure/block/block-content/types/add', $edit, t('Save'));
$block_type = entity_load('block_content_type', 'foo'); $block_type = BlockContentType::load('foo');
$this->assertTrue($block_type, 'The new block type has been created.'); $this->assertTrue($block_type, 'The new block type has been created.');
$field_definitions = \Drupal::entityManager()->getFieldDefinitions('block_content', 'foo'); $field_definitions = \Drupal::entityManager()->getFieldDefinitions('block_content', 'foo');
......
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
*/ */
namespace Drupal\block_content\Tests; namespace Drupal\block_content\Tests;
use Drupal\block_content\Entity\BlockContent;
use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\Unicode;
/** /**
...@@ -31,7 +33,7 @@ public function testPageEdit() { ...@@ -31,7 +33,7 @@ public function testPageEdit() {
// Check that the block exists in the database. // Check that the block exists in the database.
$blocks = \Drupal::entityQuery('block_content')->condition('info', $edit['info[0][value]'])->execute(); $blocks = \Drupal::entityQuery('block_content')->condition('info', $edit['info[0][value]'])->execute();
$block = entity_load('block_content', reset($blocks)); $block = BlockContent::load(reset($blocks));
$this->assertTrue($block, 'Custom block found in database.'); $this->assertTrue($block, 'Custom block found in database.');
// Load the edit page. // Load the edit page.
...@@ -55,7 +57,8 @@ public function testPageEdit() { ...@@ -55,7 +57,8 @@ public function testPageEdit() {
$this->drupalPostForm(NULL, $edit, t('Save')); $this->drupalPostForm(NULL, $edit, t('Save'));
// Ensure that the block revision has been created. // Ensure that the block revision has been created.
$revised_block = entity_load('block_content', $block->id(), TRUE); \Drupal::entityManager()->getStorage('block_content')->resetCache(array($block->id()));
$revised_block = BlockContent::load($block->id());
$this->assertNotIdentical($block->getRevisionId(), $revised_block->getRevisionId(), 'A new revision has been created.'); $this->assertNotIdentical($block->getRevisionId(), $revised_block->getRevisionId(), 'A new revision has been created.');
// Test deleting the block. // Test deleting the block.
......
...@@ -55,7 +55,7 @@ protected function setUp() { ...@@ -55,7 +55,7 @@ protected function setUp() {
*/ */
public function testBlockMigration() { public function testBlockMigration() {
/** @var BlockContent $block */ /** @var BlockContent $block */
$block = entity_load('block_content', 1); $block = BlockContent::load(1);
$this->assertIdentical('My block 1', $block->label()); $this->assertIdentical('My block 1', $block->label());
$this->assertIdentical('1', $block->getRevisionId()); $this->assertIdentical('1', $block->getRevisionId());
$this->assertTrue(REQUEST_TIME <= $block->getChangedTime() && $block->getChangedTime() <= time()); $this->assertTrue(REQUEST_TIME <= $block->getChangedTime() && $block->getChangedTime() <= time());
...@@ -63,7 +63,7 @@ public function testBlockMigration() { ...@@ -63,7 +63,7 @@ public function testBlockMigration() {
$this->assertIdentical('<h3>My first custom block body</h3>', $block->body->value); $this->assertIdentical('<h3>My first custom block body</h3>', $block->body->value);
$this->assertIdentical('full_html', $block->body->format); $this->assertIdentical('full_html', $block->body->format);
$block = entity_load('block_content', 2); $block = BlockContent::load(2);
$this->assertIdentical('My block 2', $block->label()); $this->assertIdentical('My block 2', $block->label());
$this->assertIdentical('2', $block->getRevisionId()); $this->assertIdentical('2', $block->getRevisionId());
$this->assertTrue(REQUEST_TIME <= $block->getChangedTime() && $block->getChangedTime() <= time()); $this->assertTrue(REQUEST_TIME <= $block->getChangedTime() && $block->getChangedTime() <= time());
......
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