Skip to content

Commit

Permalink
Add update hook to make documents translatable.
Browse files Browse the repository at this point in the history
  • Loading branch information
LOBsTerr committed Apr 22, 2022
1 parent 446add9 commit b38f0a8
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
11 changes: 11 additions & 0 deletions modules/newsroom_connector_item/newsroom_connector_item.install
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ function newsroom_connector_item_update_9004(&$sandbox) {
_newsroom_connector_item_install_field('field_newsroom_is_machine_trans');
}

/**
* Make documents field translatable.
*/
function newsroom_connector_item_update_9005(&$sandbox) {
$fields = \Drupal::service('entity_field.manager')->getFieldDefinitions('node', 'newsroom_item');
$field_config = $fields['field_newsroom_documents']->getConfig('newsroom_item');
$field_config
->setTranslatable(TRUE)
->save();
}

/**
* Install field from module config files.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Drupal\newsroom_connector_item\EventSubscriber;

use Drupal\entity_data\EntityData;
use Drupal\migrate\Event\MigrateEvents;
use Drupal\migrate\Event\MigratePostRowSaveEvent;
use Drupal\newsroom_connector_item\Plugin\newsroom\NewsroomItemNewsroomProcessor;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Handle newsroom item imports.
*/
class NewsroomItemMigrateSubscriber implements EventSubscriberInterface {

/**
* The event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $dispatcher;

/**
* EntityData service.
*
* @var \Drupal\entity_data\EntityData
*/
protected $entityData;

/**
* EntityData service.
*
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
* The event dispatcher.
* @param \Drupal\entity_data\EntityData $entity_data
* The entity data service.
*/
public function __construct(EventDispatcherInterface $dispatcher, EntityData $entity_data) {
$this->dispatcher = $dispatcher;
$this->entityData = $entity_data;
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events = [];
$events[MigrateEvents::POST_ROW_SAVE][] = ['updateDocuments'];
return $events;
}

/**
* Event callback to sync source and destination.
*
* @param \Drupal\migrate\Event\MigratePostRowSaveEvent $event
* The migration import event.
*/
public function updateDocuments(MigratePostRowSaveEvent $event) {
$migration_id = $event->getMigration()->id();
if (strpos($migration_id, 'newsroom_item_translations') !== FALSE || $migration_id == NewsroomItemNewsroomProcessor::MIGRATION_ITEM) {
$nid = $event->getDestinationIdValues()[0] ?? NULL;
if (empty($nid)) {
return;
}
$row = $event->getRow();
$related_documents_urls = $row->getSourceProperty('related_documents_url');
$related_documents_machine_translations = $row->getSourceProperty('related_documents_machine_translation');

if (!empty($related_documents_urls)) {
if (is_array($related_documents_urls)) {
foreach ($related_documents_urls as $key => $item) {
$this->entityData->set('newsroom_connector_item', $nid, $related_documents_urls[$key], 'node', $related_documents_machine_translations[$key] == 'True' ? 1 : 0);
}
}
else {
$this->entityData->set('newsroom_connector_item', $nid, $related_documents_urls, 'node', $related_documents_machine_translations == 'True' ? 1 : 0);
}
}
}
}

}

0 comments on commit b38f0a8

Please sign in to comment.