Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fieldblock
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
fieldblock
Commits
64bfe89b
Commit
64bfe89b
authored
10 years ago
by
Marc van Gend
Browse files
Options
Downloads
Patches
Plain Diff
cleanup and inline docs
parent
af672662
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
fieldblock.module
+11
-10
11 additions, 10 deletions
fieldblock.module
src/Plugin/Block/FieldBlock.php
+25
-15
25 additions, 15 deletions
src/Plugin/Block/FieldBlock.php
src/Plugin/Derivative/FieldBlock.php
+2
-1
2 additions, 1 deletion
src/Plugin/Derivative/FieldBlock.php
with
38 additions
and
26 deletions
fieldblock.module
+
11
−
10
View file @
64bfe89b
...
...
@@ -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 cache
d
them in a post render cache
* Takes fields out of the current entity and cache
s
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
);
}
}
}
This diff is collapsed.
Click to expand it.
src/Plugin/Block/FieldBlock.php
+
25
−
15
View file @
64bfe89b
...
...
@@ -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 $
field
block_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
(
$
field
block_id
)
{
if
(
isset
(
self
::
$fieldBlocks
[
$
field
block_id
]))
{
return
self
::
$fieldBlocks
[
$
field
block_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
::
$f
ieldBlock
s
[
$context
[
'fieldblock_id'
]
]
=
$context
[
'build'
];
self
::
setF
ieldBlock
(
$context
[
'fieldblock_id'
]
,
$context
[
'build'
]
)
;
return
$element
;
}
...
...
This diff is collapsed.
Click to expand it.
src/Plugin/Derivative/FieldBlock.php
+
2
−
1
View file @
64bfe89b
...
...
@@ -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.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment