Last active
April 18, 2017 21:35
-
-
Save abarriosr/6b8603b8b78a4f39c0296ed8a3398c53 to your computer and use it in GitHub Desktop.
umg_custom_mapping
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name = UMG Custom Mapping | |
description = Converts Video embed field to media youtube and events venue fields to venue entity. | |
package = Content Hub | |
dependencies[] = content_hub_connector | |
dependencies[] = media_internet | |
dependencies[] = media_youtube | |
core = 7.x |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Acquia\ContentHubClient\Attribute; | |
use Drupal\content_hub_connector\Cdf; | |
/** | |
* Implements hook_content_hub_connector_cdf_from_hub_alter(). | |
*/ | |
function umg_custom_mapping_content_hub_connector_cdf_from_hub_alter(Cdf $cdf) { | |
static $files = array(); | |
static $venues = array(); | |
if ($cdf->getType() == 'node') { | |
// Make sure we execute this only if we are importing data. | |
if ($_SERVER['REQUEST_METHOD'] !== 'POST') { | |
return; | |
} | |
$bundle = $cdf->getAttribute('type'); | |
$field_video = $cdf->getAttribute('field_video'); | |
$field_venue_name = $cdf->getAttribute('field_venue_name'); | |
$field_venue_address = $cdf->getAttribute('field_venue_address'); | |
// If it is 'video' node bundle and has 'field_video' attribute. | |
if ($bundle && $bundle->getValue() === 'video' && $field_video) { | |
$video_embed_field_values = $field_video->getValues(); | |
$media_youtube_values = array(); | |
foreach ($video_embed_field_values as $language => $item) { | |
if (!empty($item) && valid_url($item)) { | |
$file = FALSE; | |
$md5 = md5($item); | |
if (!empty($files[$md5])) { | |
$file = $files[$md5]; | |
} | |
else { | |
try { | |
$provider = media_internet_get_provider($item); | |
// Try to reuse existing file. | |
$file = $provider->getFileObject(); | |
if (empty($file->fid)) { | |
$file = $provider->save(); | |
} | |
$files[$md5] = $file; | |
} catch (Exception $e) { | |
} | |
} | |
if (!empty($file) && $file->uuid) { | |
$media_youtube_values[$language] = '[' . $file->uuid . ']'; | |
} | |
} | |
} | |
if (!empty($media_youtube_values)) { | |
$attribute = new Attribute(Attribute::TYPE_STRING); | |
$attribute->setValues($media_youtube_values); | |
$cdf->setAttribute('field_video', $attribute); | |
} | |
} | |
elseif ($bundle && $bundle->getValue() === 'event' && $field_venue_name && $field_venue_address) { | |
$venue_name = $field_venue_name->getValue(); | |
$venue_name = reset($venue_name); | |
$venue_address = $field_venue_address->getValue(); | |
$key = md5($venue_name); | |
if (empty($venues[$key])) { | |
$venue_address = json_decode(reset($venue_address), TRUE); | |
$node = umg_custom_mapping_retrieve_venue($venue_name, $venue_address); | |
$venues[$key] = $node; | |
} | |
else { | |
$node = $venues[$key]; | |
} | |
// Adding attribute. | |
$attribute = new Attribute(Attribute::TYPE_REFERENCE); | |
$attribute->setValue($node->uuid); | |
$cdf->setAttribute('field_venue', $attribute); | |
} | |
} | |
} | |
/** | |
* Creates or loads a Venue. | |
* | |
* @param string $venue_name | |
* The venue name. | |
* @param array $venue_address | |
* The venue address. | |
* | |
* @return object | |
* The node object. | |
* | |
* @throws \Exception | |
*/ | |
function umg_custom_mapping_retrieve_venue($venue_name, $venue_address) { | |
$node = umg_custom_mapping_search_node_by_title('venue', $venue_name); | |
if (!$node) { | |
$node = (object) array( | |
'type' => 'venue', | |
'title' => $venue_name, | |
'field_venue_address' => array( | |
LANGUAGE_NONE => array( | |
0 => $venue_address, | |
), | |
), | |
); | |
node_save($node); | |
} | |
return $node; | |
} | |
/** | |
* Searches a node by title. | |
* | |
* @param string $bundle | |
* The node bundle. | |
* @param string $title | |
* The node title. | |
* | |
* @return bool|mixed | |
* The node object if found, FALSE otherwise. | |
*/ | |
function umg_custom_mapping_search_node_by_title($bundle, $title) { | |
$query = new EntityFieldQuery(); | |
$query->entityCondition('entity_type', 'node') | |
->entityCondition('bundle', $bundle) | |
->propertyCondition('title', $title); | |
$result = $query->execute(); | |
if (isset($result['node'])) { | |
$node = reset($result['node']); | |
return node_load($node->nid); | |
} | |
return FALSE; | |
} | |
/** | |
* Implements hook_entity_diff_alter(). | |
* | |
* Fixes the compare changes functionality for the media_youtube module fields. | |
*/ | |
function umg_custom_mapping_entity_diff_alter(&$entity_diffs, $context) { | |
$field = 'field_video'; | |
// $field = 'field_video_media'; // @todo: test field - update this to actual field name. | |
if (isset($entity_diffs[$field]['#old'])) { | |
$entity_diffs[$field]['#old'] = preg_replace('/media-([^\s-]+)-\d+/', 'media-\1-[INDEX]', $entity_diffs[$field]['#old']); | |
$entity_diffs[$field]['#old'] = preg_replace('/file-(\d+)--\d+/', 'file-\1', $entity_diffs[$field]['#old']); | |
} | |
if (isset($entity_diffs[$field]['#new'])) { | |
$entity_diffs[$field]['#new'] = preg_replace('/media-([^\s-]+)-\d+/', 'media-\1-[INDEX]', $entity_diffs[$field]['#new']); | |
$entity_diffs[$field]['#new'] = preg_replace('/file-(\d+)--\d+/', 'file-\1', $entity_diffs[$field]['#new']); | |
} | |
// List of fields to be ignored by content hub diff. | |
$ignore_fields = variable_get('umg_custom_mapping_ignore_fields', array( | |
'field_event_sync_feed', | |
'field_event_deleted', | |
)); | |
foreach ($ignore_fields as $field) { | |
if (isset($entity_diffs[$field])) { | |
$entity_diffs[$field]['#old'] = ''; | |
$entity_diffs[$field]['#new'] = ''; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment