Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
drupal
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
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
drupal
Commits
f2f0725d
Commit
f2f0725d
authored
May 6, 2014
by
Angie Byron
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#2258091
by yched: Reorder methods in EntityViewBuilder into more logical sequence.
parent
e76731ab
No related branches found
No related tags found
2 merge requests
!7452
Issue #1797438. HTML5 validation is preventing form submit and not fully...
,
!789
Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
core/lib/Drupal/Core/Entity/EntityViewBuilder.php
+115
-115
115 additions, 115 deletions
core/lib/Drupal/Core/Entity/EntityViewBuilder.php
with
115 additions
and
115 deletions
core/lib/Drupal/Core/Entity/EntityViewBuilder.php
+
115
−
115
View file @
f2f0725d
...
@@ -91,36 +91,47 @@ public static function createInstance(ContainerInterface $container, EntityTypeI
...
@@ -91,36 +91,47 @@ public static function createInstance(ContainerInterface $container, EntityTypeI
/**
/**
* {@inheritdoc}
* {@inheritdoc}
*/
*/
public
function
buildComponents
(
array
&
$build
,
array
$entities
,
array
$displays
,
$view_mode
,
$langcode
=
NULL
)
{
public
function
view
(
EntityInterface
$entity
,
$view_mode
=
'full'
,
$langcode
=
NULL
)
{
$entities_by_bundle
=
array
();
$build_list
=
$this
->
viewMultiple
(
array
(
$entity
),
$view_mode
,
$langcode
);
foreach
(
$entities
as
$id
=>
$entity
)
{
// Initialize the field item attributes for the fields being displayed.
// The default ::buildMultiple() #pre_render callback won't run, because we
// The entity can include fields that are not displayed, and the display
// extract a child element of the default renderable array. Thus we must
// can include components that are not fields, so we want to act on the
// assign an alternative #pre_render callback that applies the necessary
// intersection. However, the entity can have many more fields than are
// transformations and then still calls ::buildMultiple().
// displayed, so we avoid the cost of calling $entity->getProperties()
$build
=
$build_list
[
0
];
// by iterating the intersection as follows.
$build
[
'#pre_render'
][]
=
array
(
$this
,
'build'
);
foreach
(
$displays
[
$entity
->
bundle
()]
->
getComponents
()
as
$name
=>
$options
)
{
if
(
$entity
->
hasField
(
$name
))
{
return
$build
;
foreach
(
$entity
->
get
(
$name
)
as
$item
)
{
$item
->
_attributes
=
array
();
}
}
}
}
// Group the entities by bundle.
$entities_by_bundle
[
$entity
->
bundle
()][
$id
]
=
$entity
;
/**
* {@inheritdoc}
*/
public
function
viewMultiple
(
array
$entities
=
array
(),
$view_mode
=
'full'
,
$langcode
=
NULL
)
{
if
(
!
isset
(
$langcode
))
{
$langcode
=
$this
->
languageManager
->
getCurrentLanguage
(
Language
::
TYPE_CONTENT
)
->
id
;
}
}
// Invoke hook_entity_prepare_view().
$build_list
=
array
(
$this
->
moduleHandler
()
->
invokeAll
(
'entity_prepare_view'
,
array
(
$this
->
entityTypeId
,
$entities
,
$displays
,
$view_mode
));
'#sorted'
=>
TRUE
,
'#pre_render'
=>
array
(
array
(
$this
,
'buildMultiple'
)),
'#langcode'
=>
$langcode
,
);
$weight
=
0
;
foreach
(
$entities
as
$key
=>
$entity
)
{
// Ensure that from now on we are dealing with the proper translation
// object.
$entity
=
$this
->
entityManager
->
getTranslationFromContext
(
$entity
,
$langcode
);
//
L
et
the displays build their render array
s.
//
S
et
build default
s.
foreach
(
$entities_by_bundle
as
$bundle
=
>
$
bundle_entities
)
{
$build_list
[
$key
]
=
$
this
->
getBuildDefaults
(
$entity
,
$view_mode
,
$langcode
);
$
display_build
=
$displays
[
$bundle
]
->
buildMultiple
(
$bundle_entities
)
;
$
entityType
=
$this
->
entityTypeId
;
foreach
(
$bundle_entities
as
$id
=>
$entity
)
{
$this
->
moduleHandler
()
->
alter
(
array
(
$entityType
.
'_build_defaults'
,
'entity_build_defaults'
),
$build_list
[
$key
],
$entity
,
$view_mode
,
$langcode
);
$build
[
$id
]
+=
$display_build
[
$id
];
}
$build_list
[
$key
][
'#weight'
]
=
$weight
++
;
}
}
return
$build_list
;
}
}
/**
/**
...
@@ -175,70 +186,6 @@ protected function getBuildDefaults(EntityInterface $entity, $view_mode, $langco
...
@@ -175,70 +186,6 @@ protected function getBuildDefaults(EntityInterface $entity, $view_mode, $langco
return
$build
;
return
$build
;
}
}
/**
* Specific per-entity building.
*
* @param array $build
* The render array that is being created.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to be prepared.
* @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The entity view display holding the display options configured for the
* entity components.
* @param string $view_mode
* The view mode that should be used to prepare the entity.
* @param string $langcode
* (optional) For which language the entity should be prepared, defaults to
* the current content language.
*/
protected
function
alterBuild
(
array
&
$build
,
EntityInterface
$entity
,
EntityViewDisplayInterface
$display
,
$view_mode
,
$langcode
=
NULL
)
{
}
/**
* {@inheritdoc}
*/
public
function
view
(
EntityInterface
$entity
,
$view_mode
=
'full'
,
$langcode
=
NULL
)
{
$build_list
=
$this
->
viewMultiple
(
array
(
$entity
),
$view_mode
,
$langcode
);
// The default ::buildMultiple() #pre_render callback won't run, because we
// extract a child element of the default renderable array. Thus we must
// assign an alternative #pre_render callback that applies the necessary
// transformations and then still calls ::buildMultiple().
$build
=
$build_list
[
0
];
$build
[
'#pre_render'
][]
=
array
(
$this
,
'build'
);
return
$build
;
}
/**
* {@inheritdoc}
*/
public
function
viewMultiple
(
array
$entities
=
array
(),
$view_mode
=
'full'
,
$langcode
=
NULL
)
{
if
(
!
isset
(
$langcode
))
{
$langcode
=
$this
->
languageManager
->
getCurrentLanguage
(
Language
::
TYPE_CONTENT
)
->
id
;
}
$build_list
=
array
(
'#sorted'
=>
TRUE
,
'#pre_render'
=>
array
(
array
(
$this
,
'buildMultiple'
)),
'#langcode'
=>
$langcode
,
);
$weight
=
0
;
foreach
(
$entities
as
$key
=>
$entity
)
{
// Ensure that from now on we are dealing with the proper translation
// object.
$entity
=
$this
->
entityManager
->
getTranslationFromContext
(
$entity
,
$langcode
);
// Set build defaults.
$build_list
[
$key
]
=
$this
->
getBuildDefaults
(
$entity
,
$view_mode
,
$langcode
);
$entityType
=
$this
->
entityTypeId
;
$this
->
moduleHandler
()
->
alter
(
array
(
$entityType
.
'_build_defaults'
,
'entity_build_defaults'
),
$build_list
[
$key
],
$entity
,
$view_mode
,
$langcode
);
$build_list
[
$key
][
'#weight'
]
=
$weight
++
;
}
return
$build_list
;
}
/**
/**
* Builds an entity's view; augments entity defaults.
* Builds an entity's view; augments entity defaults.
*
*
...
@@ -335,6 +282,66 @@ public function buildMultiple(array $build_list) {
...
@@ -335,6 +282,66 @@ public function buildMultiple(array $build_list) {
return
$build_list
;
return
$build_list
;
}
}
/**
* {@inheritdoc}
*/
public
function
buildComponents
(
array
&
$build
,
array
$entities
,
array
$displays
,
$view_mode
,
$langcode
=
NULL
)
{
$entities_by_bundle
=
array
();
foreach
(
$entities
as
$id
=>
$entity
)
{
// Initialize the field item attributes for the fields being displayed.
// The entity can include fields that are not displayed, and the display
// can include components that are not fields, so we want to act on the
// intersection. However, the entity can have many more fields than are
// displayed, so we avoid the cost of calling $entity->getProperties()
// by iterating the intersection as follows.
foreach
(
$displays
[
$entity
->
bundle
()]
->
getComponents
()
as
$name
=>
$options
)
{
if
(
$entity
->
hasField
(
$name
))
{
foreach
(
$entity
->
get
(
$name
)
as
$item
)
{
$item
->
_attributes
=
array
();
}
}
}
// Group the entities by bundle.
$entities_by_bundle
[
$entity
->
bundle
()][
$id
]
=
$entity
;
}
// Invoke hook_entity_prepare_view().
$this
->
moduleHandler
()
->
invokeAll
(
'entity_prepare_view'
,
array
(
$this
->
entityTypeId
,
$entities
,
$displays
,
$view_mode
));
// Let the displays build their render arrays.
foreach
(
$entities_by_bundle
as
$bundle
=>
$bundle_entities
)
{
$display_build
=
$displays
[
$bundle
]
->
buildMultiple
(
$bundle_entities
);
foreach
(
$bundle_entities
as
$id
=>
$entity
)
{
$build
[
$id
]
+=
$display_build
[
$id
];
}
}
}
/**
* Specific per-entity building.
*
* @param array $build
* The render array that is being created.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to be prepared.
* @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The entity view display holding the display options configured for the
* entity components.
* @param string $view_mode
* The view mode that should be used to prepare the entity.
* @param string $langcode
* (optional) For which language the entity should be prepared, defaults to
* the current content language.
*/
protected
function
alterBuild
(
array
&
$build
,
EntityInterface
$entity
,
EntityViewDisplayInterface
$display
,
$view_mode
,
$langcode
=
NULL
)
{
}
/**
* {@inheritdoc}
*/
public
function
getCacheTag
()
{
return
array
(
$this
->
entityTypeId
.
'_view'
=>
TRUE
);
}
/**
/**
* {@inheritdoc}
* {@inheritdoc}
*/
*/
...
@@ -352,6 +359,24 @@ public function resetCache(array $entities = NULL) {
...
@@ -352,6 +359,24 @@ public function resetCache(array $entities = NULL) {
}
}
}
}
/**
* Returns TRUE if the view mode is cacheable.
*
* @param string $view_mode
* Name of the view mode that should be rendered.
*
* @return bool
* TRUE if the view mode can be cached, FALSE otherwise.
*/
protected
function
isViewModeCacheable
(
$view_mode
)
{
if
(
$view_mode
==
'default'
)
{
// The 'default' is not an actual view mode.
return
TRUE
;
}
$view_modes_info
=
$this
->
entityManager
->
getViewModes
(
$this
->
entityTypeId
);
return
!
empty
(
$view_modes_info
[
$view_mode
][
'cache'
]);
}
/**
/**
* {@inheritdoc}
* {@inheritdoc}
*/
*/
...
@@ -413,29 +438,4 @@ public function viewFieldItem(FieldItemInterface $item, $display = array()) {
...
@@ -413,29 +438,4 @@ public function viewFieldItem(FieldItemInterface $item, $display = array()) {
return
$output
;
return
$output
;
}
}
/*
* Returns TRUE if the view mode is cacheable.
*
* @param string $view_mode
* Name of the view mode that should be rendered.
*
* @return bool
* TRUE if the view mode can be cached, FALSE otherwise.
*/
protected
function
isViewModeCacheable
(
$view_mode
)
{
if
(
$view_mode
==
'default'
)
{
// The 'default' is not an actual view mode.
return
TRUE
;
}
$view_modes_info
=
$this
->
entityManager
->
getViewModes
(
$this
->
entityTypeId
);
return
!
empty
(
$view_modes_info
[
$view_mode
][
'cache'
]);
}
/**
* {@inheritdoc}
*/
public
function
getCacheTag
()
{
return
array
(
$this
->
entityTypeId
.
'_view'
=>
TRUE
);
}
}
}
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