Skip to content

Instantly share code, notes, and snippets.

@nidaismailshah
Last active August 29, 2018 14:07
Show Gist options
  • Select an option

  • Save nidaismailshah/3c290849ceafc0e88bfcc86770351eef to your computer and use it in GitHub Desktop.

Select an option

Save nidaismailshah/3c290849ceafc0e88bfcc86770351eef to your computer and use it in GitHub Desktop.
Enables Acquia Content Hub to work with content translations and custom languages on drupal 7
<?php
/**
* To make Content hub work with Drupal 7 content translation module and custom languages (with hyphen in the langcode)
* these hook implementations will need to be added to a custom module on the both the publisher and the subscriber site.
* Two text fields field_tnid and field_custom_language will also need to be created on the required content types to
* carry the tnid and custom language information across.
* The fields should be hidden from the node view and node edit form as these should only be used by this code.
*/
/**
* Implements hook_content_hub_connector_drupal_to_cdf_alter().
*/
function content_hub_custom_content_hub_connector_drupal_to_cdf_alter($entity_type, $entity) {
if ($entity_type == 'node') {
// Custom languages.
if (strpos($entity->language, '-') !== false) {
$entity->field_custom_language['und'][0]['value'] = $entity->language;
}
// Content Translation.
if ($entity->tnid) {
$entity->field_tnid['und'][0]['value'] = array_values(entity_get_uuid_by_id('node', array($entity->tnid)))[0];
}
else {
$entity->field_tnid['und'][0]['value'] = $entity->uuid;
}
}
}
/**
* Implements hook_content_hub_connector_drupal_from_cdf_alter().
*/
function content_hub_custom_content_hub_connector_drupal_from_cdf_alter($entity_type, $entity) {
if ($entity_type == 'node') {
// Custom Languages.
if ($entity->field_custom_language['und'][0]['value']) {
$entity->language = $entity->field_custom_language['und'][0]['value'];
}
// Content Translation.
$nodes = entity_uuid_load('node', array($entity->field_tnid['und'][0]['value']));
if (!empty($nodes)) {
// When the source has already been imported.
$entity->tnid = array_values($nodes)[0]->nid;
// Update the tnid of any previously imported nodes from the same translation set.
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->fieldCondition('field_tnid', 'value',$entity->field_tnid['und'][0]['value'], '=');
$result = $query->execute();
if (isset($result['node'])) {
$nids = array_keys($result['node']);
$nodes = entity_load('node', $nids);
foreach ($nodes as $node) {
$node->tnid = $entity->tnid;
entity_save('node', $node);
}
}
}
else {
// When the source node hasn't been imported and but there are other nodes from the same translation set,
// Get them and put them into same translation set.
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->fieldCondition('field_tnid', 'value', $entity->field_tnid['und'][0]['value'], '=');
$result = $query->execute();
if (isset($result['node'])) {
$nids = array_keys($result['node']);
$nodes = entity_load('node', $nids);
// Since the source node has not been imported, set the first node from the result as the source.
$entity->tnid = $nids[0];
foreach ($nodes as $node) {
$node->tnid = $nids[0];
entity_save('node', $node);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment