From 64bfe89b9cb5686514b8b4f223b003fabc5ee87c Mon Sep 17 00:00:00 2001 From: marcvangend <info@marcvangend.nl> Date: Tue, 7 Oct 2014 23:40:44 +0200 Subject: [PATCH] cleanup and inline docs --- fieldblock.module | 21 ++++++++------- src/Plugin/Block/FieldBlock.php | 40 +++++++++++++++++----------- src/Plugin/Derivative/FieldBlock.php | 3 ++- 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/fieldblock.module b/fieldblock.module index ae901ac..dfa6d57 100644 --- a/fieldblock.module +++ b/fieldblock.module @@ -7,16 +7,15 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Entity\Entity\EntityViewDisplay; -use Drupal\fieldblock\Plugin\Block\FieldBlock; - -define('FIELDBLOCK_STORAGE_STATIC', 'fieldblock_storage'); +use Drupal\Core\Entity\Display\EntityViewDisplayInterface; /** * Implements hook_form_alter(). + * * Adds a column to the "display fields" table-form, with a checkbox for each * field. */ -function fieldblock_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) { +function fieldblock_form_field_ui_display_overview_form_alter(&$form, FormStateInterface &$form_state, $form_id) { $entity_type = $form['#entity_type']; $bundle = $form['#bundle']; $mode = $form['#mode']; @@ -45,6 +44,11 @@ function fieldblock_form_field_ui_display_overview_form_alter(&$form, &$form_sta * Form submit handler for field_ui_display_overview_form. * Stores which fields are published as blocks as a third_party_settings array * in the EntityViewDisplay object of the entity type / bundle / view mode. + * + * @param mixed[] $form + * A form API array. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The state of the submitted form. */ function fieldblock_field_display_submit($form, FormStateInterface $form_state) { $entity_type = $form['#entity_type']; @@ -74,13 +78,14 @@ function fieldblock_field_display_submit($form, FormStateInterface $form_state) /** * Implements hook_entity_view_alter(). * - * Takes fields out of the current entity and cached them in a post render cache + * Takes fields out of the current entity and caches them in a post render cache * context. The #post_render_cache callback makes this data available to the * fieldblock when it is built, We also hide the field from the render array. + * * @see \Drupal\fieldblock\Plugin\Block\FieldBlock::fieldBlockPostRenderCache * @see https://www.drupal.org/node/2151609 */ -function fieldblock_entity_view_alter(array &$build, Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display) { +function fieldblock_entity_view_alter(array &$build, Drupal\Core\Entity\EntityInterface $entity, EntityViewDisplayInterface $display) { $fieldblock_settings = $display->getThirdPartySettings('fieldblock'); $display_id = $display->get('id'); @@ -90,15 +95,11 @@ function fieldblock_entity_view_alter(array &$build, Drupal\Core\Entity\EntityIn if (count(\Drupal\Core\Render\Element::children($build[$field_name]))) { // This is where we take out the field and cache it in a post render // cache context. - // $build['#post_render_cache']['Drupal\fieldblock\Plugin\Block\FieldBlock::fieldBlockPostRenderCache'][] = array( 'build' => $build[$field_name], 'fieldblock_id' => $fieldblock_id, ); hide($build[$field_name]); } - else { - FieldBlock::unsetFieldBlock($fieldblock_id); - } } } diff --git a/src/Plugin/Block/FieldBlock.php b/src/Plugin/Block/FieldBlock.php index 7f179a4..fd769bb 100644 --- a/src/Plugin/Block/FieldBlock.php +++ b/src/Plugin/Block/FieldBlock.php @@ -7,7 +7,6 @@ namespace Drupal\fieldblock\Plugin\Block; use Drupal\Core\Block\BlockBase; -use Drupal\Core\Cache\CacheBackendInterface; /** * Provides a fieldblock. @@ -21,7 +20,7 @@ use Drupal\Core\Cache\CacheBackendInterface; class FieldBlock extends BlockBase { /** - * Implements \Drupal\block\BlockBase::blockBuild(). + * {@inheritdoc} */ public function build() { $block_id = $this->getDerivativeId(); @@ -30,27 +29,38 @@ class FieldBlock extends BlockBase { } /** - * @var array Static storage for fields that are grabbed from the entity's - * render array, to be retrieved when fieldblocks are built. + * @var array[] + * Static storage for fields that are grabbed from the entity's render + * array, to be retrieved when fieldblocks are built. */ protected static $fieldBlocks; /** - * @param string $block_id + * @param string $fieldblock_id * The identifier of the fieldblock. - * @return array | false + * @return mixed[]|false * The render array of the field that is published as block or false if the * field is not available. */ - public static function getFieldBlock($block_id) { - if (isset(self::$fieldBlocks[$block_id])) { - return self::$fieldBlocks[$block_id]; + public static function getFieldBlock($fieldblock_id) { + if (isset(self::$fieldBlocks[$fieldblock_id])) { + return self::$fieldBlocks[$fieldblock_id]; } else { return FALSE; } } + /** + * @param string $fieldblock_id + * The identifier of the fieldblock. + * @param mixed[] $render_array + * The render array of the field that will be published as block. + */ + public static function setFieldBlock($fieldblock_id, array $render_array) { + self::$fieldBlocks[$fieldblock_id] = $render_array; + } + /** * #post_render_cache callback, temporarily stores a field's render array in a * static variable and returns the original element as post render cache @@ -62,15 +72,15 @@ class FieldBlock extends BlockBase { * the cached field will only be retrieved and displayed as a block when the * entity is viewed. * - * @param array $element - * The element (entity) being rendered. - * @param array $context + * @param mixed[] $element + * The render array being rendered. + * @param mixed[] $context * Array containing the fieldblock ID and the field's render array. - * @return array - * The render array of the entity. + * @return mixed[] + * The render array being rendered. */ public static function fieldBlockPostRenderCache(array $element, array $context) { - self::$fieldBlocks[$context['fieldblock_id']] = $context['build']; + self::setFieldBlock($context['fieldblock_id'], $context['build']); return $element; } diff --git a/src/Plugin/Derivative/FieldBlock.php b/src/Plugin/Derivative/FieldBlock.php index de4f25e..6cadc56 100644 --- a/src/Plugin/Derivative/FieldBlock.php +++ b/src/Plugin/Derivative/FieldBlock.php @@ -37,7 +37,8 @@ class FieldBlock extends DeriverBase { /** * Builds a list of fields that have been made available as a block. * - * @return array An array of fieldblocks. + * @return string[] + * An array of fieldblocks in the form of fieldblock_id => admin label. */ protected function fieldBlockGetBlockList() { // @todo Check if we actually need static caching. -- GitLab