Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
WCMS
uw_ct_profile
Commits
f1660edf
Commit
f1660edf
authored
Mar 16, 2021
by
Igor Biki
Browse files
ISTWCMS-4586: Adding entity presave hook to update media/image field.
parent
59755498
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
0 deletions
+23
-0
uw_ct_profile.module
uw_ct_profile.module
+23
-0
No files found.
uw_ct_profile.module
View file @
f1660edf
...
...
@@ -5,6 +5,7 @@
* Profile module file.
*/
use
Drupal\Core\Entity\EntityInterface
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\node\Entity\Node
;
...
...
@@ -69,3 +70,25 @@ function _uw_ct_profile_alter_fields(): array {
'field_uw_ct_profile_title'
=>
'field_uw_ct_contact_title'
,
];
}
/**
* Implements hook_entity_presave().
*/
function
uw_ct_profile_entity_presave
(
EntityInterface
$entity
)
{
// Sync image only for profile entity, only when entity is created, there is
// contact link provided, and image field is empty.
if
(
$entity
->
bundle
()
===
'uw_ct_profile'
&&
$entity
->
isNew
()
&&
$entity
->
field_uw_ct_profile_contact
->
entity
&&
!
$entity
->
field_uw_ct_profile_image
->
value
)
{
// Get contact entity from Link with contact field.
$contact
=
$entity
->
field_uw_ct_profile_contact
->
entity
;
// If contact has attached image.
if
(
$image
=
$contact
->
field_uw_ct_contact_image
->
entity
)
{
// Update image field of profile with values from contact.
$entity
->
field_uw_ct_profile_image
->
target_id
=
$image
->
id
();
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment