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 {
*/
protected $blockContentStorage;
/**
* The custom block type storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $blockContentTypeStorage;
/**
* The language manager.
*
......@@ -48,12 +55,15 @@ class BlockContentForm extends ContentEntityForm {
* The entity manager.
* @param \Drupal\Core\Entity\EntityStorageInterface $block_content_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
* 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);
$this->blockContentStorage = $block_content_storage;
$this->blockContentTypeStorage = $block_content_type_storage;
$this->languageManager = $language_manager;
}
......@@ -65,6 +75,7 @@ public static function create(ContainerInterface $container) {
return new static(
$entity_manager,
$entity_manager->getStorage('block_content'),
$entity_manager->getStorage('block_content_type'),
$container->get('language_manager')
);
}
......@@ -80,7 +91,7 @@ public static function create(ContainerInterface $container) {
protected function prepareEntity() {
$block = $this->entity;
// 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()) {
$block->setRevisionLog(NULL);
}
......@@ -170,7 +181,7 @@ public function save(array $form, FormStateInterface $form_state) {
$block->save();
$context = array('@type' => $block->bundle(), '%info' => $block->label());
$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());
if ($insert) {
......
......@@ -7,6 +7,7 @@
namespace Drupal\block_content;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\Core\Entity\EntityInterface;
use Drupal\content_translation\ContentTranslationHandler;
use Drupal\Core\Form\FormStateInterface;
......@@ -37,7 +38,7 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En
* {@inheritdoc}
*/
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()));
}
......
......@@ -8,16 +8,47 @@
namespace Drupal\block_content\Plugin\Derivative;
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.
*/
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}
*/
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 */
foreach ($block_contents as $block_content) {
$this->derivatives[$block_content->uuid()] = $base_plugin_definition;
......
......@@ -7,9 +7,9 @@
namespace Drupal\block_content\Tests;
use Drupal\block_content\Entity\BlockContent;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Database\Database;
use Drupal\block_content\Entity\BlockContent;
/**
* Create a block and test saving it.
......@@ -209,7 +209,7 @@ public function testBlockDelete() {
'settings[label]' => $edit['info[0][value]'],
'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');
$this->drupalPostForm($url, $instance, t('Save block'));
......@@ -263,7 +263,7 @@ public function testConfigDependencies() {
'settings[label]' => $block->label(),
'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');
$this->drupalPostForm($url, $instance, t('Save block'));
......
......@@ -7,6 +7,8 @@
namespace Drupal\block_content\Tests;
use Drupal\block_content\Entity\BlockContent;
/**
* Create a block with revisions.
*
......@@ -88,7 +90,7 @@ public function testRevisions() {
// Verify that the non-default revision id is greater than the default
// 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.');
}
......
......@@ -6,6 +6,8 @@
*/
namespace Drupal\block_content\Tests;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
......@@ -58,7 +60,7 @@ public function testBlockContentTypeCreation() {
// Create a block type programmaticaly.
$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->drupalGet('block/add/' . $type->id());
......@@ -70,7 +72,7 @@ public function testBlockContentTypeCreation() {
'label' => 'title for foo',
);
$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.');
$field_definitions = \Drupal::entityManager()->getFieldDefinitions('block_content', 'foo');
......
......@@ -6,6 +6,8 @@
*/
namespace Drupal\block_content\Tests;
use Drupal\block_content\Entity\BlockContent;
use Drupal\Component\Utility\Unicode;
/**
......@@ -31,7 +33,7 @@ public function testPageEdit() {
// Check that the block exists in the database.
$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.');
// Load the edit page.
......@@ -55,7 +57,8 @@ public function testPageEdit() {
$this->drupalPostForm(NULL, $edit, t('Save'));
// 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.');
// Test deleting the block.
......
......@@ -55,7 +55,7 @@ protected function setUp() {
*/
public function testBlockMigration() {
/** @var BlockContent $block */
$block = entity_load('block_content', 1);
$block = BlockContent::load(1);
$this->assertIdentical('My block 1', $block->label());
$this->assertIdentical('1', $block->getRevisionId());
$this->assertTrue(REQUEST_TIME <= $block->getChangedTime() && $block->getChangedTime() <= time());
......@@ -63,7 +63,7 @@ public function testBlockMigration() {
$this->assertIdentical('<h3>My first custom block body</h3>', $block->body->value);
$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('2', $block->getRevisionId());
$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