Skip to content
Snippets Groups Projects

ISTWCMS-3755: adding logic to update profile fields when contact is updated

Merged Eric Bremner requested to merge feature/ISTWCMS-3755-ebremner-profiles-contacts-sync into 1.0.x
@@ -6,6 +6,7 @@ use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\core_event_dispatcher\Event\Entity\EntityUpdateEvent;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Drupal\node\Entity\Node;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
@@ -48,6 +49,7 @@ class UwCtContactEventSubscriber implements EventSubscriberInterface {
* Entity update event.
*/
public function entityUpdate(EntityUpdateEvent $event) {
/**
* Potentially contact entity.
* @var \Drupal\node\Entity\Node $contact
@@ -55,39 +57,34 @@ class UwCtContactEventSubscriber implements EventSubscriberInterface {
$contact = $event->getEntity();
// Trigger only for contact content type update.
if ($contact && $contact->bundle() === 'uw_ct_contact') {
if (
$contact &&
$contact->bundle() === 'uw_ct_contact'
) {
$profiles = $this->entityTypeManager->getStorage('node')->loadByProperties([
'type' => 'uw_ct_profile',
'field_uw_ct_profile_contact' => $contact->id(),
]);
// Ensure that we have at least something for the old
// and new media ids.
$old_mid = NULL;
$new_mid = NULL;
// If there is an old listing image get the media id.
if ($old = $contact->original->get('field_uw_contact_listing_image')->getValue()) {
$old_mid = $old[0]['target_id'];
}
// If there is a new listing image, get the media id.
if ($new = $contact->get('field_uw_contact_listing_image')->getValue()) {
$new_mid = $new[0]['target_id'];
}
// There could be more than one profile link to one contact.
if ($profiles) {
/**
* Profile entity.
* @var \Drupal\node\Entity\Node $profile
*/
foreach ($profiles as $profile) {
$contact_image_id = NULL;
if ($contact_image = $contact->field_uw_ct_contact_image->getEntity()) {
$contact_image_id = $contact_image->id();
}
// Title field is user as Name field.
$profile->set('title', $contact->title->value);
$profile->set('field_uw_ct_profile_sort_name', $contact->field_uw_ct_contact_sort_name->value);
$profile->set('field_uw_ct_profile_affiliation', $contact->field_uw_ct_contact_affiliation->value);
$profile->set('field_uw_ct_profile_title', $contact->field_uw_ct_contact_title->value);
if ($contact_image_id) {
$profile->field_uw_ct_profile_image->target_id = $contact_image_id;
}
$profile->save();
}
// If the old and new are not the same, then we have a change.
if ($new_mid !== $old_mid) {
$this->updateProfileListingImage($contact->id(), $old_mid, $new_mid);
}
// Update the profile fields.
$this->updateProfileFields($contact);
// Invalidate OFIS cache if Contact is unpublished
// and there is watiam value provided.
if (!$contact->isPublished() && $watiam = $contact->field_uw_ct_contact_watiam_id->value) {
@@ -96,4 +93,86 @@ class UwCtContactEventSubscriber implements EventSubscriberInterface {
}
}
/**
* Function to update the fields of profiles.
*
* @param \Drupal\node\Entity\Node $contact
* The contact node.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public function updateProfileFields(Node $contact): void {
// Get the fields to update from profiles module.
$fields = _uw_ct_profile_alter_fields();
// Get the nodes that have the contact sync.
$nodes = $this->entityTypeManager
->getStorage('node')
->loadByProperties([
'type' => 'uw_ct_profile',
'field_uw_ct_profile_contact' => $contact->id(),
]);
// Step through each node and fields and sync them.
foreach ($nodes as $node) {
foreach ($fields as $profile_field => $contact_field) {
$node->$profile_field = $contact->$contact_field;
}
$node->save();
}
}
/**
* Function to change the listing image on profiles.
*
* @param int $nid
* The node id of the contact.
* @param int $old_mid
* The old media id of the listing image.
* @param int $new_mid
* The new media id of the listing image.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public function updateProfileListingImage(int $nid, int $old_mid = NULL, int $new_mid = NULL): void {
// Ensure that we get the right properties to load the nodes.
// We need to remove the image if there is no image to look
// for, since trying load with NULL for the listing image,
// cause the site to error out.
if (!$old_mid) {
$properties = [
'type' => 'uw_ct_profile',
'field_uw_ct_profile_contact' => $nid,
];
}
else {
$properties = [
'type' => 'uw_ct_profile',
'field_uw_ct_profile_contact' => $nid,
'field_uw_ct_profile_image' => $old_mid,
];
}
// Get the nodes that have the contact sync and the same listing image.
$nodes = $this->entityTypeManager
->getStorage('node')
->loadByProperties($properties);
// Step through all the profile nodes and update the
// listing image.
foreach ($nodes as $node) {
if ($node->field_uw_ct_profile_image->target_id) {
$node->field_uw_ct_profile_image->target_id = $new_mid;
$node->save();
}
}
}
}
Loading