Commit f1660edf authored by Igor Biki's avatar Igor Biki
Browse files

ISTWCMS-4586: Adding entity presave hook to update media/image field.

parent 59755498
......@@ -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();
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment