Skip to content

Instantly share code, notes, and snippets.

@mikeybeck
Last active November 2, 2023 02:35
Show Gist options
  • Select an option

  • Save mikeybeck/09408cd0e8fd6b4701b70c2f55476dad to your computer and use it in GitHub Desktop.

Select an option

Save mikeybeck/09408cd0e8fd6b4701b70c2f55476dad to your computer and use it in GitHub Desktop.
Drupal views migration patch
diff --git a/src/Plugin/migrate/source/d7/ViewsMigration.php b/src/Plugin/migrate/source/d7/ViewsMigration.php
index 7c8f3e8..e78a407 100644
--- a/src/Plugin/migrate/source/d7/ViewsMigration.php
+++ b/src/Plugin/migrate/source/d7/ViewsMigration.php
@@ -7,6 +7,7 @@
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\views_migration\Plugin\migrate\source\BaseViewsMigration;
+use Drush\Drush;
/**
* Drupal 7 views source from database.
@@ -37,8 +38,7 @@ public function query() {
*/
public function prepareRow(Row $row) {
$this->row = $row;
- $viewId = $row->getSourceProperty('name');
- $this->view = strtolower($viewId);
+ $this->view = $row->getSourceProperty('name');
$vid = $row->getSourceProperty('vid');
$base_table = $row->getSourceProperty('base_table');
$base_table_plugin = $this->getViewBaseTableMigratePlugin($base_table);
@@ -47,9 +47,6 @@ public function prepareRow(Row $row) {
try {
if (!in_array($base_table, $available_views_tables)) {
- if (php_sapi_name() === 'cli') {
- echo "\n\033[32m [Ignored]\033[0m - \033[1mThe view ({$viewId}) base table ({$base_table}) is not exist in your database.\033[0m";
- }
throw new MigrateSkipRowException('The views base table ' . $base_table . ' is not exist in your database.');
}
}
@@ -90,12 +87,11 @@ public function prepareRow(Row $row) {
$source_displays[] = $result;
}
// Prepare the options for all displays.
- $masterDisplay = [];
foreach ($source_displays as $source_display) {
$display_options = $source_display['display_options'];
- $id = strtolower($source_display['id']);
+ $id = $source_display['id'];
$this->display = $id;
- $display_options = unserialize($display_options, ['allowed_classes' => FALSE]);
+ $display_options = unserialize($display_options);
$display[$id]['display_plugin'] = $source_display['display_plugin'];
$display[$id]['id'] = $source_display['id'];
$display[$id]['display_title'] = $source_display['display_title'];
@@ -104,15 +100,25 @@ public function prepareRow(Row $row) {
if (isset($source_display['display_plugin'])) {
$this->getViewsPluginMigratePlugin('display', $source_display['display_plugin'])->prepareDisplayOptions($display[$id]);
}
- $display[$id]['display_options'] = $this->convertDisplayPlugins($display[$id]['display_options'], $entity_type);
+ $display[$id]['display_options'] = $this->convertDisplayPlugins($display[$id]['display_options']);
$display[$id]['display_options'] = $this->convertHandlerDisplayOptions($display[$id]['display_options'], $entity_type);
- $this->checkHandlerDisplayRelationships($display[$id]['display_options'], $entity_type, $masterDisplay, $viewId, $id);
+
+ //file_put_contents('/tmp/views-migration-debug-before.log', var_export($display[$id]['display_options'], TRUE));
+ $display[$id]['display_options'] = $this->fixCiviCRMEntityRelationships($display[$id]['display_options']);
+ $display[$id]['display_options'] = $this->fixCiviCRMDrupalUser($display[$id]['display_options']);
+ /*foreach($display[$id]['display_options']['fields'] as $dok => $dov) {
+ var_export(
+ [
+ $dok => $dov['relationship']
+ ]);
+ }*/
+
+
$display[$id]['display_options'] = $this->removeNonExistFields($display[$id]['display_options']);
+ //file_put_contents('/tmp/views-migration-debug-after.log', var_export($display[$id]['display_options'], TRUE));
+
$this->logBrokenHandlers($display[$id]['display_options']);
$this->display = NULL;
- if ($id == 'default') {
- $masterDisplay = $display[$id]['display_options'];
- }
}
$row->setSourceProperty('display', $display);
$this->row = NULL;
@@ -120,4 +126,142 @@ public function prepareRow(Row $row) {
return parent::prepareRow($row);
}
+ private function fixCiviCRMDrupalUser(array $display_options) {
+ $fields_adding = [];
+ $fields_removing = [];
+ foreach($display_options['fields'] as $f_key => $field_options) {
+ switch($f_key) {
+ case 'drupal_id':
+ $keys_to_copy = [
+ 'group_type',
+ 'label',
+ 'exclude',
+ 'alter',
+ 'element_type',
+ 'element_class',
+ 'element_label_type',
+ 'element_label_class',
+ 'element_label_colon',
+ 'element_wrapper_type',
+ 'element_wrapper_class',
+ 'element_default_classes',
+ 'empty',
+ 'hide_empty',
+ 'empty_zero',
+ 'hide_alter_empty',
+ ];
+
+ $new_field = [];
+ foreach($keys_to_copy as $key) {
+ $new_field[$key] = $field_options[$key];
+ }
+ $new_field_id = 'uid';
+ $new_field['id'] = $new_field_id;
+ $new_field['table'] = 'users_field_data';
+ $new_field['field'] = 'uid';
+ $new_field['relationship'] = 'user';
+ $new_field['group_type'] = 'group';
+ $new_field['entity_type'] = 'user';
+ $new_field['entity_type'] = 'uid';
+ $new_field['plugin_id'] = 'field';
+ $fields_adding[$new_field_id] = $new_field;
+ $fields_removing[] = $f_key;
+ break;
+ default:
+ continue 2;
+ }
+ }
+ $display_options['fields'] = array_filter($display_options['fields'], function($key) {
+ return !isset($fields_removing[$key]);
+ }, ARRAY_FILTER_USE_KEY);
+ $display_options['fields'] = array_merge($display_options['fields'], $fields_adding);
+
+
+ // Now we need to fix any associated relationships
+ //echo "#############################################################" .PHP_EOL;
+ $relationships_adding = [];
+ $relationships_removing = [];
+ foreach($display_options['relationships'] as $r_id => $r_options) {
+ if ($r_id == 'drupal_id') {
+ $r_options['id'] = 'user';
+ $r_options['field'] = 'user';
+ $r_options['plugin_id'] = 'civcirm_entity_civicrm_contact_user';
+ }
+ $relationships_adding['user'] = $r_options;
+ $relationships_removing[] = $r_id;
+ }
+
+ $display_options['relationships'] = array_filter($display_options['relationships'], function($key) {
+ return !isset($relationships_removing[$key]);
+ }, ARRAY_FILTER_USE_KEY);
+ $display_options['relationships'] = array_merge($display_options['relationships'], $relationships_adding);
+
+
+ //var_export($display_options['relationships']);
+ // echo "#############################################################" . PHP_EOL;
+ return $display_options;
+ }
+
+ private function fixCiviCRMEntityRelationships(array $display_options) {
+ $relationship_key_mapping = [];
+ $field_key_mapping = [];
+ foreach($display_options['relationships'] as $r_key => $relationship_options) {
+ switch($r_key) {
+ case 'relationship_id_a':
+ case 'relationship_id_b':
+ //echo "CASE 1" . PHP_EOL;
+ $direction = substr($r_key, -1);
+ $new_relationship = $relationship_options;
+ $new_relationship['id'] = 'reverse__civicrm_relationship__contact_id_' . $direction;
+ $new_relationship['field'] = 'reverse__civicrm_relationship__contact_id_' . $direction;
+ $new_relationship['plugin_id'] = 'civicrm_entity_civicrm_relationship';
+ break;
+ case 'contact_id_a_':
+ case 'contact_id_b_':
+ //echo "CASE 2" . PHP_EOL;
+ $direction = substr($r_key, -2, 1);
+ $new_relationship = $relationship_options;
+ $new_relationship['id'] = 'contact_id_' . $direction;
+ $new_relationship['field'] = 'contact_id_' . $direction;
+ $new_relationship['plugin_id'] = 'civicrm_entity_civicrm_relationship';
+ break;
+ default:
+ //echo "CASE 3: ". $r_key . PHP_EOL;
+ # If no match skip future processing continue loop
+ continue 2;
+ }
+ $relationship_key_mapping[$r_key] = $new_relationship['id'];
+
+ if (!empty($new_relationship['relationship']) && $new_relationship['relationship'] != 'none') {
+ if (!empty($relationship_key_mapping[$new_relationship['relationship']])) {
+ $new_relationship['relationship'] = $relationship_key_mapping[$new_relationship['relationship']];
+ }
+ }
+
+ $display_options['relationships'][$new_relationship['id']] = $new_relationship;
+
+
+
+ unset($display_options['relationships'][$r_key]);
+ }
+ // Update fields based on the relationship - do outside of the
+ // loop so we only checking each field once.
+ foreach($display_options['fields'] as $field_key => &$field_options) {
+ if (empty($field_options['relationship'])) {
+ continue;
+ }
+
+ $old_relationship_id = $field_options['relationship'];
+ if (!empty($relationship_key_mapping[$old_relationship_id])) {
+ $new_relationship_id = $relationship_key_mapping[$old_relationship_id];
+ $field_options['relationship'] = $new_relationship_id;
+ }
+
+ }
+ /*var_export([
+ 'display_options new relationships' => $display_options['relationships'],
+ 'display options new fields' => $display_options['fields'],
+ ]);*/
+ return $display_options;
+ }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment