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
5c954755
Commit
5c954755
authored
6 years ago
by
Erik Stielstra
Committed by
Erik Stielstra
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#2875632
by Gertjan.k, Sutharsan: Paragraph fields always display the default language
parent
1b60e4a8
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Plugin/Block/FieldBlock.php
+34
-3
34 additions, 3 deletions
src/Plugin/Block/FieldBlock.php
with
34 additions
and
3 deletions
src/Plugin/Block/FieldBlock.php
+
34
−
3
View file @
5c954755
...
@@ -8,12 +8,14 @@ use Drupal\Core\Entity\ContentEntityInterface;
...
@@ -8,12 +8,14 @@ use Drupal\Core\Entity\ContentEntityInterface;
use
Drupal\Core\Entity\EntityFieldManagerInterface
;
use
Drupal\Core\Entity\EntityFieldManagerInterface
;
use
Drupal\Core\Entity\EntityInterface
;
use
Drupal\Core\Entity\EntityInterface
;
use
Drupal\Core\Entity\EntityTypeManagerInterface
;
use
Drupal\Core\Entity\EntityTypeManagerInterface
;
use
Drupal\Core\Entity\Plugin\DataType\EntityAdapter
;
use
Drupal\Core\Field\BaseFieldDefinition
;
use
Drupal\Core\Field\BaseFieldDefinition
;
use
Drupal\Core\Field\FieldDefinitionInterface
;
use
Drupal\Core\Field\FieldDefinitionInterface
;
use
Drupal\Core\Field\FormatterInterface
;
use
Drupal\Core\Field\FormatterInterface
;
use
Drupal\Core\Field\FormatterPluginManager
;
use
Drupal\Core\Field\FormatterPluginManager
;
use
Drupal\Core\Form\FormHelper
;
use
Drupal\Core\Form\FormHelper
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\Core\Language\LanguageManagerInterface
;
use
Drupal\Core\Form\SubformStateInterface
;
use
Drupal\Core\Form\SubformStateInterface
;
use
Drupal\Core\Plugin\ContainerFactoryPluginInterface
;
use
Drupal\Core\Plugin\ContainerFactoryPluginInterface
;
use
Drupal\Core\Routing\RouteMatchInterface
;
use
Drupal\Core\Routing\RouteMatchInterface
;
...
@@ -59,6 +61,13 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
...
@@ -59,6 +61,13 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
*/
*/
protected
$routeMatch
;
protected
$routeMatch
;
/**
* The language manager
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
private
$languageManager
;
/**
/**
* The entity to be used when displaying the block.
* The entity to be used when displaying the block.
*
*
...
@@ -84,12 +93,13 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
...
@@ -84,12 +93,13 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match.
* The current route match.
*/
*/
public
function
__construct
(
array
$configuration
,
$plugin_id
,
$plugin_definition
,
EntityTypeManagerInterface
$entityTypeManager
,
EntityFieldManagerInterface
$entityFieldManager
,
FormatterPluginManager
$formatter_plugin_manager
,
RouteMatchInterface
$route_match
)
{
public
function
__construct
(
array
$configuration
,
$plugin_id
,
$plugin_definition
,
EntityTypeManagerInterface
$entityTypeManager
,
EntityFieldManagerInterface
$entityFieldManager
,
FormatterPluginManager
$formatter_plugin_manager
,
RouteMatchInterface
$route_match
,
LanguageManagerInterface
$languageManager
)
{
parent
::
__construct
(
$configuration
,
$plugin_id
,
$plugin_definition
);
parent
::
__construct
(
$configuration
,
$plugin_id
,
$plugin_definition
);
$this
->
entityTypeManager
=
$entityTypeManager
;
$this
->
entityTypeManager
=
$entityTypeManager
;
$this
->
entityFieldManager
=
$entityFieldManager
;
$this
->
entityFieldManager
=
$entityFieldManager
;
$this
->
formatterPluginManager
=
$formatter_plugin_manager
;
$this
->
formatterPluginManager
=
$formatter_plugin_manager
;
$this
->
routeMatch
=
$route_match
;
$this
->
routeMatch
=
$route_match
;
$this
->
languageManager
=
$languageManager
;
}
}
/**
/**
...
@@ -102,7 +112,8 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
...
@@ -102,7 +112,8 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
$container
->
get
(
'entity_type.manager'
),
$container
->
get
(
'entity_type.manager'
),
$container
->
get
(
'entity_field.manager'
),
$container
->
get
(
'entity_field.manager'
),
$container
->
get
(
'plugin.manager.field.formatter'
),
$container
->
get
(
'plugin.manager.field.formatter'
),
$container
->
get
(
'current_route_match'
));
$container
->
get
(
'current_route_match'
),
$container
->
get
(
'language_manager'
));
}
}
/**
/**
...
@@ -370,7 +381,7 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
...
@@ -370,7 +381,7 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
$entity
=
$this
->
getEntity
();
$entity
=
$this
->
getEntity
();
if
(
$entity
)
{
if
(
$entity
)
{
$build
[
'field'
]
=
$
entity
->
get
(
$this
->
configuration
[
'field_name'
]
)
->
view
([
$build
[
'field'
]
=
$
this
->
getTranslatedFieldFromEntity
(
$entity
)
->
view
([
'label'
=>
'hidden'
,
'label'
=>
'hidden'
,
'type'
=>
$this
->
configuration
[
'formatter_id'
],
'type'
=>
$this
->
configuration
[
'formatter_id'
],
'settings'
=>
$this
->
configuration
[
'formatter_settings'
],
'settings'
=>
$this
->
configuration
[
'formatter_settings'
],
...
@@ -383,6 +394,26 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
...
@@ -383,6 +394,26 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
return
$build
;
return
$build
;
}
}
/**
* Ensure that the field gets correctly translated into the current language
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
*
* @return \Drupal\Core\Field\FieldItemListInterface
*/
private
function
getTranslatedFieldFromEntity
(
ContentEntityInterface
$entity
)
{
$language
=
$this
->
languageManager
->
getCurrentLanguage
()
->
getId
();
$field
=
$entity
->
get
(
$this
->
configuration
[
'field_name'
]);
if
(
$entity
->
hasTranslation
(
$language
))
{
$translatedEntity
=
$entity
->
getTranslation
(
$language
);
$adapter
=
EntityAdapter
::
createFromEntity
(
$translatedEntity
);
$field
->
setContext
(
$this
->
configuration
[
'field_name'
],
$adapter
);
}
return
$field
;
}
/**
/**
* {@inheritdoc}
* {@inheritdoc}
*/
*/
...
...
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