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

Merge branch 'feature/ISTWCMS-4586-ibiki-profile-link-contact' into '8.x-1.x'

Feature/istwcms 4586 ibiki profile link contact

See merge request !4
parents 9427ca94 651da8eb
......@@ -15,7 +15,7 @@ field_name: field_uw_ct_profile_image
entity_type: node
bundle: uw_ct_profile
label: 'Listing page image'
description: ''
description: 'Leaving image empty will sync image from linked Contact. This happens only on create (not update).'
required: false
translatable: false
default_value: { }
......
......@@ -3,9 +3,8 @@ description: 'Add featured people profiles.'
type: module
core_version_requirement: '^8 || ^9'
dependencies:
- 'drupal:autocomplete_deluxe'
- 'autocomplete_deluxe:autocomplete_deluxe'
- 'drupal:content_moderation'
- 'drupal:entity_browser_block_layout'
- 'drupal:field'
- 'drupal:language'
- 'drupal:link'
......@@ -14,8 +13,9 @@ dependencies:
- 'drupal:menu_ui'
- 'drupal:node'
- 'drupal:path'
- 'drupal:require_on_publish'
- 'drupal:taxonomy'
- 'drupal:user'
- 'drupal:uw_ct_contact'
- 'drupal:uw_media'
- 'entity_browser_block_layout:entity_browser_block_layout'
- 'require_on_publish:require_on_publish'
- 'uw_ct_contact:uw_ct_contact'
- 'uw_media:uw_media'
<?php
/**
* @file
* Profile module file.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\Entity\Node;
/**
* Implements hook_form_FORM_ID_alter().
*/
function uw_ct_profile_form_node_uw_ct_profile_form_alter(&$form, FormStateInterface $form_state, $form_id): void {
$form['field_uw_ct_profile_contact']['widget'][0]['target_id']['#ajax'] = [
'callback' => '_uw_ct_profile_node_form_callback',
'event' => 'change',
'wrapper' => $form['#id'],
];
if ($form['field_uw_ct_profile_contact']['widget'][0]['target_id']) {
foreach (_uw_ct_profile_alter_fields() as $profile_field => $contact_field) {
$form[$profile_field]['widget'][0]['value']['#attributes']['readonly'] = 'readonly';
}
}
}
/**
* Implements hook_form_FORM_ID_EDIT_FORM_alter().
*/
function uw_ct_profile_form_node_uw_ct_profile_edit_form_alter(&$form, FormStateInterface $form_state, $form_id): void {
uw_ct_profile_form_node_uw_ct_profile_form_alter($form, $form_state, $form_id);
}
/**
* Ajax call back function used in form alter.
*/
function _uw_ct_profile_node_form_callback(array &$form, FormStateInterface $form_state): array {
$contact_nid = $form_state->getValue('field_uw_ct_profile_contact')[0]['target_id'];
foreach (_uw_ct_profile_alter_fields() as $profile_field => $contact_field) {
if ($contact_nid && is_numeric($contact_nid) && $contact = Node::load($contact_nid)) {
// Copy value from the contact associated with this profile node.
$form[$profile_field]['widget'][0]['value']['#value'] = $contact->$contact_field->value;
// Set read only flag. Can't use #disabled (#states) since disabled fields
// are not sent with submit (omitted). Validation will fail on req fields.
$form[$profile_field]['widget'][0]['value']['#attributes']['readonly'] = 'readonly';
}
else {
// Clear the read only flag, allow field to be edited.
unset($form[$profile_field]['widget'][0]['value']['#attributes']['readonly']);
}
}
return $form;
}
/**
* Fields to sync.
*
* @return string[]
* Mapping of fields profile => contact.
*/
function _uw_ct_profile_alter_fields(): array {
return [
'title' => 'title',
'field_uw_ct_profile_sort_name' => 'field_uw_ct_contact_sort_name',
'field_uw_ct_profile_affiliation' => 'field_uw_ct_contact_affiliation',
'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