content_workflow_bynder-1.0.0/content_workflow_bynder.module
content_workflow_bynder.module
<?php /** * @file * Main module file for Content Workflow module. */ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\content_workflow_bynder\Entity\MappingInterface; use Drupal\content_workflow_bynder\Plugin\migrate\source\ContentWorkflowBynderMigrateSource; use Drupal\migrate\Plugin\MigrationInterface; use Drupal\content_workflow_bynder\MigrateExecutable; use Drupal\content_workflow_bynder\MigrateMessageCapture; /** * Implements hook_entity_base_field_info(). * * Add a 'CWB mapping ID' and 'CWB ID fields' base field to all node types. * * {@inheritdoc} */ function content_workflow_bynder_entity_base_field_info(EntityTypeInterface $entity_type) { $fields = []; if ($entity_type->id() === 'file') { $fields['cwb_file_id'] = BaseFieldDefinition::create('string') ->setLabel(t('CWB ID')) ->setDescription(t('The ID of Content Workflow content.')) ->setReadOnly(TRUE); } return $fields; } /** * Implements hook_entity_delete(). */ function content_workflow_bynder_entity_delete(EntityInterface $entity) { content_workflow_bynder_on_entity_delete($entity, true); } /** * Implements hook_entity_translation_delete(). */ function content_workflow_bynder_entity_translation_delete(EntityInterface $entity) { content_workflow_bynder_on_entity_delete($entity); } /** * Batch operation callback for importing items. */ function content_workflow_bynder_import_process($cwb_ids, array $import_options, MappingInterface $mapping, &$context = []) { $client = \Drupal::service('content_workflow_bynder.client'); $migrationIds = $mapping->getMigrations(); if (!isset($context['results']['success'])) { $context['results']['success'] = 0; } if (!isset($context['results']['failed'])) { $context['results']['failed'] = 0; } if (!empty($migrationIds)) { foreach ($migrationIds as $migrationId) { /** @var \Drupal\migrate\Plugin\Migration $migration */ $migration = \Drupal::service('plugin.manager.migration') ->createInstance($migrationId); if ($migration) { // Do not use the item list feature of the parent migration class, // limit the source directly as this is more efficient and sources // may be multiplied to support repeatable components. $source = $migration->getSourcePlugin(); if (!$source instanceof ContentWorkflowBynderMigrateSource) { $context['results']['messages'][] = (string) t('Migration source must be an instance of ContentWorkflowBynderMigrateSource'); \Drupal::logger('content_workflow_bynder')->error('Migration source must be an instance of ContentWorkflowBynderMigrateSource'); } $source->setItemIds($cwb_ids); $messages = new MigrateMessageCapture(); $executable = new MigrateExecutable($migration, $messages, [ 'import_options' => $import_options, 'client' => $client, ]); $status = ''; try { $status = $executable->import(); $context['results']['success'] += $executable->getCreatedCount() + $executable->getUpdatedCount(); $context['results']['failed'] += $executable->getFailedCount() + $executable->getIgnoredCount(); if ($executable->getFailedCount() + $executable->getIgnoredCount() > 0) { $executable->rollback(); } } catch (\Exception $e) { \Drupal::logger('content_workflow_bynder')->error($e->getMessage()); } switch ($status) { case MigrationInterface::RESULT_FAILED: if ($migration->getStatus() !== MigrationInterface::STATUS_IDLE) { $migration->setStatus(MigrationInterface::STATUS_IDLE); } // Add and log any captured messages. foreach ($messages->getMessages() as $message) { $context['results']['messages'][] = (string) $message; \Drupal::logger('content_workflow_bynder')->error($message); } break; } } } } } /** * Deletes the tracked table records for this entity and rolls back the migration. * * @param \Drupal\Core\Entity\EntityInterface $entity * * @throws \Drupal\migrate\MigrateException */ function content_workflow_bynder_on_entity_delete(EntityInterface $entity, $skipLanguage = false) { $entityId = $entity->id(); $entityType = $entity->getEntityTypeId(); $langcode = $entity->language()->getId(); /** @var \Drupal\Core\Database\Connection $connection */ $connection = \Drupal::service('database'); $query = $connection->select('content_workflow_bynder_entity_mapping') ->fields('content_workflow_bynder_entity_mapping', [ 'entity_id', 'entity_type', 'cwb_id', 'migration_id', ]) ->condition('entity_id', $entityId) ->condition('entity_type', $entityType); if (!$skipLanguage) { $query->condition('langcode', $langcode); } $results = $query->execute() ->fetchAll(); if (empty($results)) { return; } $deleteQuery = $connection->delete('content_workflow_bynder_entity_mapping') ->condition('entity_id', $entityId) ->condition('entity_type', $entityType); if (!$skipLanguage) { $deleteQuery->condition('langcode', $langcode); } $deleteQuery->execute(); foreach ($results as $result) { $migration = \Drupal::service('plugin.manager.migration') ->createInstance($result->migration_id); if ($migration) { $messages = new MigrateMessageCapture(); $executable = new MigrateExecutable($migration, $messages, [ 'idlist' => $result->cwb_id, ]); $executable->rollback(); } } }