diff --git a/web/modules/custom/zoocha_video_sync/src/Services/VideoPublishingService.php b/web/modules/custom/zoocha_video_sync/src/Services/VideoPublishingService.php new file mode 100644 index 0000000..e9f5ee3 --- /dev/null +++ b/web/modules/custom/zoocha_video_sync/src/Services/VideoPublishingService.php @@ -0,0 +1,105 @@ +entityTypeManager = $entity_type_manager; + $this->logger = $logger; + } + + /** + * Synchronizes the video state with its parent node. + * + * @param \Drupal\node\NodeInterface $node + * The node entity being processed. + */ + public function synchronizeVideoState(NodeInterface $node) { + // Ensure the node is of type 'landing_page'. + if ($node->bundle() === 'landing_page') { + $components = $node->get('field_components')->referencedEntities(); + foreach ($components as $component) { + // Check if the component has a field for remote videos. + if ($component->hasField('field_remote_video')) { + $videos = $component->get('field_remote_video')->referencedEntities(); + foreach ($videos as $video) { + // Ensure the entity is a media and is of the correct bundle. + if ($video instanceof Media && $video->bundle() === 'remote_video') { + // If the node is unpublished, unpublish all videos. + if (!$node->isPublished()) { + $this->updateVideoPublishingStatus($video, FALSE); + } + else { + // Otherwise, retain the video's current published status. + $this->retainVideoPublishingStatus($video); + } + } + } + } + } + } + } + + /** + * Updates the publishing status of a video. + * + * @param \Drupal\media\Entity\Media $video + * The video entity. + * @param bool $publish + * Whether to publish or unpublish the video. + */ + private function updateVideoPublishingStatus(Media $video, $publish) { + // Update the status field based on the publication state. + $video->set('status', $publish ? 1 : 0); + // Save the media entity with error handling. + try { + $video->save(); + } + catch (\Exception $e) { + $this->logger->error('Failed to update video status for media ID ' . $video->id() . ': ' . $e->getMessage()); + } + } + + /** + * Retains the current publishing status of the video. + * + * @param \Drupal\media\Entity\Media $video + * The video entity. + */ + private function retainVideoPublishingStatus(Media $video) { + // This method is a placeholder if additional logic is needed in the future. + // Currently, it is left empty as no action is required to retain the status. + } +} diff --git a/web/modules/custom/zoocha_video_sync/zoocha_video_sync.info.yml b/web/modules/custom/zoocha_video_sync/zoocha_video_sync.info.yml new file mode 100644 index 0000000..9cc73e5 --- /dev/null +++ b/web/modules/custom/zoocha_video_sync/zoocha_video_sync.info.yml @@ -0,0 +1,9 @@ +name: Zoocha Video Sync +type: module +description: Synchronizes the publication state of videos with their parent node, allowing individual video state control when the parent is published. +core_version_requirement: ^10 +package: Custom +dependencies: + - drupal:node + - drupal:media +version: '1.0' diff --git a/web/modules/custom/zoocha_video_sync/zoocha_video_sync.module b/web/modules/custom/zoocha_video_sync/zoocha_video_sync.module new file mode 100644 index 0000000..4233dc5 --- /dev/null +++ b/web/modules/custom/zoocha_video_sync/zoocha_video_sync.module @@ -0,0 +1,27 @@ +bundle() === 'landing_page') { + /** @var \Drupal\zoocha_video_sync\Services\VideoPublishingService $service */ + $service = \Drupal::service('zoocha_video_sync.video_publishing_service'); + // Synchronize video state with error handling. + try { + $service->synchronizeVideoState($entity); + } + catch (\Exception $e) { + \Drupal::logger('zoocha_video_sync')->error('Error synchronizing video state for node ID ' . $entity->id() . ': ' . $e->getMessage()); + } + } +} diff --git a/web/modules/custom/zoocha_video_sync/zoocha_video_sync.services.yml b/web/modules/custom/zoocha_video_sync/zoocha_video_sync.services.yml new file mode 100644 index 0000000..8b7af59 --- /dev/null +++ b/web/modules/custom/zoocha_video_sync/zoocha_video_sync.services.yml @@ -0,0 +1,9 @@ +services: + zoocha_video_sync.video_publishing_service: + class: Drupal\zoocha_video_sync\Services\VideoPublishingService + arguments: ['@entity_type.manager', '@logger.channel.zoocha_video_sync'] + + logger.channel.zoocha_video_sync: + class: Drupal\Core\Logger\LoggerChannelFactory + factory: ['@logger.factory', 'get'] + arguments: ['zoocha_video_sync']