Commit 2d87c121 authored by marcvangend's avatar marcvangend

starting the rendering side of fieldblock

parent d2747b49
......@@ -6,6 +6,8 @@
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\fieldblock\Plugin\Block\FieldBlock;
define('FIELDBLOCK_STORAGE_STATIC', 'fieldblock_storage');
......@@ -64,92 +66,34 @@ function fieldblock_field_display_submit($form, FormStateInterface $form_state)
$view_mode_config->save();
}
///**
// * Implements hook_block_info().
// */
//function fieldblock_block_info() {
// $blocks = array();
//
// $fieldblocks = fieldblock_get_block_list();
// foreach ($fieldblocks as $fieldblock_id => $description) {
// $blocks[$fieldblock_id] = array(
// 'info' => $description,
// 'cache' => DRUPAL_CACHE_PER_PAGE,
// );
// }
//
// return $blocks;
//}
/**
* Helper function for fieldblock_block_info().
* Builds a list of fields that have been made available as a block.
* @return array
* An array with all fields that are made visible via the display fields UI,
* in the form of [fieldblock identifier] => [block description].
* Implements hook_entity_view_alter().
* Stores specific fields on the current entity, to be retrieved later when the
* fieldblock is built, and hides the field from the renderable array.
*
* @todo: This should be in a post_render_cache callback. See setFieldBlock();
*/
function fieldblock_entity_view_alter(array &$build, Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display) {
$fieldblock_settings = $display->getThirdPartySettings('fieldblock');
$display_id = $display->get('id');
///**
// * Implements hook_block_view().
// * Retrieves a field, identified by the block delta, from the static cache.
// */
//function fieldblock_block_view($delta = '') {
// $block = array();
//
// $fieldblocks_storage = &drupal_static(FIELDBLOCK_STORAGE_STATIC);
// if (isset($fieldblocks_storage[$delta])) {
// // If the field has a label, use it as block title and hide it on the
// // field itself.
// $block['subject'] = '';
// if (isset($fieldblocks_storage[$delta]['#label_display']) && $fieldblocks_storage[$delta]['#label_display'] != 'hidden') {
// $block['subject'] = $fieldblocks_storage[$delta]['#title'];
// $fieldblocks_storage[$delta]['#label_display'] = 'hidden';
// }
// $block['content'] = $fieldblocks_storage[$delta];
// $block['fieldblock_name'] = $fieldblocks_storage[$delta]['fieldblock_name'];
// }
//
// return $block;
//}
$cache_tags = array();
if (isset($build['#cache']['tags'])) {
$cache_tags = $build['#cache']['tags'];
}
///**
// * Implements hook_entity_view_alter().
// * Stores fields attached to the current entity in a static cache, to be
// * retrieved in fieldblock_block_view, and hides the field from the renderable
// * array.
// */
//function fieldblock_entity_view_alter(&$build, $type) {
// // Do nothing if essential information is missing.
// if (!isset($build['#bundle'], $build['#entity_type'], $build['#view_mode'])) {
// return;
// }
//
// $entity_type = $build['#entity_type'];
// $bundle = $build['#bundle'];
// $view_mode = $build['#view_mode'];
//
// // Check whether the view mode uses custom display settings or the 'default'
// // mode.
// $view_mode_settings = field_view_mode_settings($entity_type, $bundle);
// $actual_mode = (!empty($view_mode_settings[$view_mode]['custom_settings']) ? $view_mode : 'default');
//
// $fieldblocks_storage = &drupal_static(FIELDBLOCK_STORAGE_STATIC);
//
// $variable_name = 'fieldblock-'. $entity_type .'-'. $bundle .'-'. $actual_mode;
// $fieldblock_settings = variable_get($variable_name, array());
//
// // Loop over the fieldblocks for this entity + bundle + view mode combination
// // and store the field's render array for later use.
// foreach ($fieldblock_settings as $field_name => $field_label) {
// if (isset($build[$field_name])) {
// $fieldblock_name = $variable_name .'-'. $field_name;
// $fieldblock_id = md5($fieldblock_name);
// $fieldblocks_storage[$fieldblock_id] = $build[$field_name];
// hide($build[$field_name]);
// $fieldblocks_storage[$fieldblock_id]['fieldblock_name'] = $fieldblock_name;
// }
// }
//}
foreach ($fieldblock_settings as $field_name => $field_label) {
$fieldblock_id = $display_id . ':' . $field_name;
if (count(\Drupal\Core\Render\Element::children($build[$field_name]))) {
FieldBlock::setFieldBlock($build[$field_name], $fieldblock_id, $cache_tags);
hide($build[$field_name]);
}
else {
FieldBlock::unsetFieldBlock($fieldblock_id);
}
}
}
///**
// * Give fieldblocks a meaningful html id in spite of the hashed block deltas.
......
......@@ -9,6 +9,16 @@ class FieldBlockHelper {
use StringTranslationTrait;
/**
* Builds a list of fields that have been made available as a block.
*
* @return array An array of fieldblocks.
*
* @todo decide if this function should go into the fieldblock deriver class.
*/
public static function fieldBlockGetBlockList() {
$fieldblocks = &drupal_static(__FUNCTION__);
......@@ -24,7 +34,7 @@ class FieldBlockHelper {
$mode = $entity_view_display->get('mode');
foreach ($view_display_fieldblocks as $field_name => $field_label) {
$fieldblock_id = implode('_', array($entity_type, $bundle, $mode, $field_name));
$fieldblock_id = $display_id . ':' . $field_name;
$fieldblocks[$fieldblock_id] = t('@field field (from @type: @bundle: @mode)', array(
'@field' => $field_label,
'@type' => $entity_type,
......
......@@ -7,6 +7,7 @@
namespace Drupal\fieldblock\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\CacheBackendInterface;
/**
* Provides a fieldblock.
......@@ -24,9 +25,38 @@ class FieldBlock extends BlockBase {
*/
public function build() {
$block_id = $this->getDerivativeId();
return array(
'#markup' => 'There are more like me!<br> I am ' . $block_id,
);
$block = $this::getFieldBlock($block_id);
return $block;
}
/**
* Stores a the render array of a field for use as fieldblock.
*
* @param array $build
* @param string $fieldblock_id
* @param array $cache_tags
*
* @todo: The current storage approach does not work because its too hard to control the visibility of the block.
* Do we have to switch to storing in a static property, called from a post_render_cache callback?
*/
public static function setFieldBlock($build, $fieldblock_id, $cache_tags) {
$cid = 'fieldblock:' . $fieldblock_id;
\Drupal::cache()->set($cid, $build, CacheBackendInterface::CACHE_PERMANENT, $cache_tags);
}
public static function getFieldBlock($block_id) {
$cid = 'fieldblock:' . $block_id;
if ($cache = \Drupal::cache()->get($cid)) {
return $cache->data;
}
else {
return FALSE;
}
}
public static function unsetFieldBlock($fieldblock_id) {
$cid = 'fieldblock:' . $fieldblock_id;
\Drupal::cache()->delete($cid);
}
}
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