View source
<?php
namespace Drupal\cms_content_sync_migrate_acquia_content_hub;
use Drupal\cms_content_sync\Entity\EntityStatus;
use Drupal\cms_content_sync\Entity\Flow;
use Drupal\cms_content_sync\PushIntent;
use Drupal\cms_content_sync_migrate_acquia_content_hub\Form\MigrationBase;
use Drupal\Core\Controller\ControllerBase;
class CreateStatusEntities extends ControllerBase {
public function prepare($flow_id, $flow_configurations, $pool_id, $type, $tags = '') {
$operations = [];
if ($type == 'push') {
foreach ($flow_configurations as $type => $type_config) {
foreach ($type_config as $bundle => $bundle_config) {
if ($bundle_config['push_configuration']['behavior'] != PushIntent::PUSH_AUTOMATICALLY) {
continue;
}
$entity_type = \Drupal::entityTypeManager()
->getDefinition($type);
$ids = \Drupal::entityQuery($type)
->condition($entity_type
->getKey('bundle'), $bundle)
->execute();
foreach ($ids as $id) {
$operations[] = [
__NAMESPACE__ . '\\CreateStatusEntities::execute',
[
$type,
$id,
$flow_id,
$pool_id,
'push',
],
];
}
}
}
return $operations;
}
$tags = MigrationBase::getTermsFromFilter($tags);
if (empty($tags)) {
return $operations;
}
$ids = [];
foreach ($tags as $tag) {
$ids[] = $tag
->id();
}
$query = \Drupal::database()
->select('taxonomy_index', 'ti');
$query
->fields('ti', [
'nid',
]);
$query
->condition('ti.tid', $ids, 'IN');
$result = $query
->execute()
->fetchCol();
foreach ($result as $nid) {
$operations[] = [
__NAMESPACE__ . '\\CreateStatusEntities::execute',
[
'node',
$nid,
$flow_id,
$pool_id,
'pull',
],
];
}
return $operations;
}
public static function execute($entity_type, $entity_id, $flow_id, $pool_id, $type) {
$entity = \Drupal::entityTypeManager()
->getStorage($entity_type)
->load($entity_id);
if (!$entity) {
return;
}
$entity_status = EntityStatus::getInfoForEntity($entity_type, $entity
->uuid(), $flow_id, $pool_id);
if (!$entity_status) {
$data = [
'flow' => $flow_id,
'pool' => $pool_id,
'entity_type' => $entity_type,
'entity_uuid' => $entity
->uuid(),
'entity_type_version' => Flow::getEntityTypeVersion($entity_type, $entity
->bundle()),
'flags' => 0,
'source_url' => NULL,
];
if ($entity_type == 'node' && $type == 'pull') {
$data['last_' . $type] = $entity
->getChangedTime();
}
$entity_status = EntityStatus::create($data);
if ($type == 'push') {
$entity_status
->isPushEnabled(TRUE);
$entity_status
->isSourceEntity(TRUE);
}
$entity_status
->save();
}
}
}