Commit 23b83f20 authored by jrockowitz's avatar jrockowitz

Add additional test block and support for block cache dependencies and access result.

parent 04c3129a
......@@ -7,6 +7,7 @@ Contents of this file
* Demo
* Installation
* Notes
* Todo
About this Module
......@@ -51,6 +52,22 @@ Notes
- All content blocks from the 'Custom Block Library' are available.
Todo
----
Write Tests
- Copy \Drupal\link\Tests\LinkFieldTest
- Check block field widget
- Check block field formatter
- Check block field type
- Check block field configuration form
- Check block field cache dependencies
- Check block field access result
Author/Maintainer
-----------------
......
......@@ -14,4 +14,12 @@ use Drupal\Core\Field\FieldItemInterface;
*/
interface BlockFieldItemInterface extends FieldItemInterface {
/**
* Get block instance.
*
* @return null|\Drupal\Core\Block\BlockPluginInterface
* Return the block instance or NULL if the block does not exist.
*/
public function getBlock();
}
......@@ -7,7 +7,6 @@
namespace Drupal\block_field\Plugin\Field\FieldFormatter;
use Drupal\block_field\Plugin\Field\FieldType\BlockFieldItem;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
......@@ -30,22 +29,29 @@ class BlockFieldFormatter extends FormatterBase {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$block_instance = $item->getBlock();
// Make sure the block exists and is accessible.
if (!$block_instance || !$block_instance->access(\Drupal::currentUser())) {
continue;
}
// @see \Drupal\block\BlockViewBuilder::buildPreRenderableBlock
// @see template_preprocess_block()
if ($block_instance = $item->getBlock()) {
$elements[$delta] = [
'#theme' => 'block',
'#attributes' => [],
'#configuration' => $block_instance->getConfiguration(),
'#plugin_id' => $block_instance->getPluginId(),
'#base_plugin_id' => $block_instance->getBaseId(),
'#derivative_plugin_id' => $block_instance->getDerivativeId(),
'#id' => $item->id,
'content' => $block_instance->build(),
];
}
}
$elements[$delta] = [
'#theme' => 'block',
'#attributes' => [],
'#configuration' => $block_instance->getConfiguration(),
'#plugin_id' => $block_instance->getPluginId(),
'#base_plugin_id' => $block_instance->getBaseId(),
'#derivative_plugin_id' => $block_instance->getDerivativeId(),
'#id' => $item->id,
'content' => $block_instance->build(),
];
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = \Drupal::service('renderer');
$renderer->addCacheableDependency($elements[$delta], $block_instance);
}
return $elements;
}
......
......@@ -76,6 +76,7 @@ class BlockFieldItem extends FieldItemBase implements BlockFieldItemInterface {
'serialize' => TRUE,
],
],
'indexes' => ['id' => ['id']],
];
}
......@@ -154,11 +155,8 @@ class BlockFieldItem extends FieldItemBase implements BlockFieldItemInterface {
parent::setValue($values, $notify);
}
/**
* Get block instance
*
* @return null|\Drupal\Core\Block\BlockPluginInterface
* {@inheritdoc}
*/
public function getBlock() {
if (empty($this->id)) {
......@@ -167,23 +165,25 @@ class BlockFieldItem extends FieldItemBase implements BlockFieldItemInterface {
/** @var \Drupal\Core\Block\BlockManagerInterface $block_manager */
$block_manager = \Drupal::service('plugin.manager.block');
/** @var \Drupal\Core\Block\BlockPluginInterface $block_instance */
$block_instance = $block_manager->createInstance($this->id, $this->settings);
$plugin_definition = $block_instance->getPluginDefinition();
// Don't render or return broken block instances.
// Don't return broken block plugin instances.
if ($plugin_definition['id'] == 'broken') {
return NULL;
}
// Make sure block content exists, if not don't render the broken block
// message.
// Don't return broken block content instances.
if ($plugin_definition['id'] == 'block_content') {
$uuid = $block_instance->getDerivativeId();
if (!\Drupal::entityManager()->loadEntityByUuid('block_content', $uuid)) {
return NULL;
}
}
return $block_instance;
}
......
......@@ -8,7 +8,6 @@
namespace Drupal\block_field\Plugin\Field\FieldWidget;
use Drupal\block\BlockInterface;
use Drupal\block_field\Plugin\Field\FieldType\BlockFieldItem;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
......
<?php
/**
* @file
* Contains \Drupal\block_field_test\Plugin\Block\BlockFieldTestAuthenticatedBlock.
*/
namespace Drupal\block_field_test\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Session\AccountInterface;
/**
* Provides a 'Block field test authenticated' block.
*
* @Block(
* id = "block_field_test_authenticated",
* admin_label = @Translation("You are logged in as..."),
* category = @Translation("Block field test")
* )
*/
class BlockFieldTestAuthenticatedBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
return [
'#theme' => 'username',
'#account' => \Drupal::currentUser()->getAccount(),
];
}
/**
* {@inheritdoc}
*/
protected function blockAccess(AccountInterface $account) {
return AccessResult::allowedIf($account->isAuthenticated());
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
return ['user'];
}
/**
* {@inheritdoc}
*/
public function getCacheTags() {
return ['user:' . \Drupal::currentUser()->id()];
}
}
......@@ -2,7 +2,7 @@
/**
* @file
* Contains \Drupal\block_field_test\Plugin\Block\BlockFieldTestBlock.
* Contains \Drupal\block_field_test\Plugin\Block\BlockFieldTestContentBlock.
*/
namespace Drupal\block_field_test\Plugin\Block;
......@@ -11,15 +11,15 @@ use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides a 'Block field test' block.
* Provides a 'Block field test content' block.
*
* @Block(
* id = "block_field_test",
* admin_label = @Translation("Block field test"),
* id = "block_field_test_content",
* admin_label = @Translation("Block field test content"),
* category = @Translation("Block field test")
* )
*/
class BlockFieldTestBlock extends BlockBase {
class BlockFieldTestContentBlock extends BlockBase {
/**
* {@inheritdoc}
......
<?php
/**
* @file
* Contains \Drupal\block_field_test\Plugin\Block\BlockFieldTestTimeBlock.
*/
namespace Drupal\block_field_test\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a 'Block field test time' block.
*
* @Block(
* id = "block_field_test_time",
* admin_label = @Translation("The time is..."),
* category = @Translation("Block field test")
* )
*/
class BlockFieldTestTimeBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
return [
'#type' => 'markup',
'#markup' => date('H:i:s'),
];
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
return 0;
}
}
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