Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DGIR-48 : Unpublish and Publish Collections feature workng #5

Merged
merged 24 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
ef4dd54
DGIR-48 : JS confirmation popup with Unpublish and Publish Collection…
Prashant-bd Nov 20, 2023
8c384be
Merge branch 'main' into dgir-48
Prashant-bd Nov 20, 2023
db19c88
DGIR-48 : Confirmation screen and drush command
Prashant-bd Nov 21, 2023
d10366a
DGIR-48 : Fix linting errors
Prashant-bd Nov 22, 2023
e0b341a
DGIR-48 : Fix linting errors
Prashant-bd Nov 22, 2023
50fc234
DGIR-48 : Fix linting errors
Prashant-bd Nov 22, 2023
24fb02c
DGIR-48 : Fix linting errors
Prashant-bd Nov 22, 2023
f243314
DGIR-48 : Remove unwanted library file
Prashant-bd Nov 22, 2023
59b7b9a
DGIR-48 : Change field validation and hook
Prashant-bd Nov 22, 2023
70fe2a7
Resolving merge conflicts
nchiasson-dgi Dec 7, 2023
e99c4f0
Merge pull request #1 from nchiasson-dgi/dgir-48
nchiasson-dgi Dec 7, 2023
237b54d
Updating with PR feedback
nchiasson-dgi Dec 7, 2023
b41574f
Merge pull request #2 from nchiasson-dgi/dgir-48
nchiasson-dgi Dec 7, 2023
e674d80
Resolving coder complaints
nchiasson-dgi Dec 7, 2023
6d8e451
Supplying the necessary dependencies
nchiasson-dgi Dec 7, 2023
ed60101
Updating PR to make functional
nchiasson-dgi Dec 7, 2023
8eb6b52
Removing unused use statements.
nchiasson-dgi Dec 7, 2023
d35367e
Merge pull request #3 from nchiasson-dgi/dgir-48
nchiasson-dgi Dec 7, 2023
166d117
Fixing a spacing and deprecated error message
nchiasson-dgi Dec 7, 2023
5c02989
Merge pull request #5 from discoverygarden/main
Prashant-bd Dec 8, 2023
cd9f601
Merge pull request #4 from nchiasson-dgi/dgir-48
Prashant-bd Dec 8, 2023
d091d65
Merge branch 'main' of github.com:Prashant-bd/islandora_entity_status…
Prashant-bd Dec 8, 2023
a6808eb
DGIR-48 : Fix observation from QA
Prashant-bd Dec 8, 2023
8e29258
DGIR-48 : Fix linting errors
Prashant-bd Dec 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions islandora_entity_status.libraries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
confirm-popup:
version: 1.x
js:
js/confirm-popup.js: {}
dependencies:
- core/jquery
- core/jquery.ui.dialog
- core/once
171 changes: 171 additions & 0 deletions islandora_entity_status.module
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
* Hook implementations.
*/

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\islandora\IslandoraUtils;
use Drupal\media\Entity\Media;
use Drupal\media\MediaInterface;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Twig\Markup;

/**
* Implements hook_ENTITY_TYPE_presave().
Expand All @@ -24,3 +29,169 @@ function islandora_entity_status_media_presave(MediaInterface $media) {
}
}
}

/**
* Implements hook_entity_update().
*/
function islandora_entity_status_entity_update(EntityInterface $entity) {
// Check if the entity is a node with the bundle "Repository Item".
if ($entity->getEntityTypeId() == 'node' && $entity->bundle() == 'islandora_object') {
// Get the current node ID.
$nid = $entity->id();

// Query for media items that are associated with the current node.
$query = \Drupal::entityQuery('media')
->accessCheck(FALSE)
->condition(IslandoraUtils::MEDIA_OF_FIELD, $nid);
$media_ids = $query->execute();

// Load the media items and set their status to the same status as the node.
$media_items = Media::loadMultiple($media_ids);
foreach ($media_items as $media_item) {
$media_item->set('status', $entity->get('status')->value);
$media_item->save();
}
}
}

/**
* Implements hook_form_alter().
*/
function islandora_entity_status_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Check if the form is for the Repository Item content type.
if ($form_id == 'node_islandora_object_edit_form') {
// We need to remove all submit method, inorder to make sure
// we redirect user to confirmation page.
$form['actions']['submit']['#submit'] = ['islandora_entity_status_node_edit_submit'];
}
}

/**
* Submit callback for node edit form.
*/
function islandora_entity_status_node_edit_submit(array &$form, FormStateInterface $form_state) {
$node = $form_state->getFormObject()->getEntity();
$data = $form_state->cleanValues()->getValues();

// Store data in temporary storage.
$tempstore = \Drupal::service('tempstore.private')->get('islandora_entity_status');
$tempstore->set('node_edit_data', ['node_id' => $node->id(), 'data' => $data]);

// Redirect to the confirmation page.
$form_state->setRedirect('islandora_entity_status.node_edit_confirm');
}

/**
* Find related nodes.
*/
function find_collection_nodes($currentNodeId) {
$relatedNodeIds = [];

// Initial query to find nodes where the field_member_of contains
// the current node ID.
$query = \Drupal::entityQuery('node')
->condition('type', 'islandora_object')
->condition('field_member_of', $currentNodeId, '=')
->accessCheck(FALSE);

$result = $query->execute();

$relatedNodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($result);

if (!empty($relatedNodes)) {
foreach ($relatedNodes as $relatedNode) {
$relatedNodeIds[] = $relatedNode->id();
}
}

return $relatedNodeIds;
}

/**
* Implements hook_node_insert() for node entities.
*/
function islandora_entity_status_node_insert($node) {
// Check if the node type is the Repository Item content type.
if ($node->getType() == 'islandora_object') {
// Trigger the batch process.
$node_ids_to_update = find_collection_nodes($node->id());
$node_status = $node->get('status');
islandora_entity_status_trigger_batch_process($node_ids_to_update, $node_status);
}
}

/**
* Implements hook_node_update() for node entities.
*/
function islandora_entity_status_node_update($node) {
// Check if the node type is the Repository Item content type.
if ($node->getType() == 'islandora_object') {
// Trigger the batch process.
$node_ids_to_update = find_collection_nodes($node->id());
$latestStatus = $node->get('status')->value;

islandora_entity_status_trigger_batch_process($node_ids_to_update, $latestStatus);
}
}

/**
* Helper function to trigger the batch process.
*/
function islandora_entity_status_trigger_batch_process($node_ids, $node_status) {
// Create a batch operation.
$operations = [
['islandora_entity_status_batch_operation', [$node_ids, $node_status]],
];

// Create a batch.
$batch = [
'title' => t('Processing nodes'),
'operations' => $operations,
'finished' => 'islandora_entity_status_batch_finished',
];

// Add the batch to the queue.
batch_set($batch);
}

/**
* Batch operation callback.
*/
function islandora_entity_status_batch_operation($node_ids, $node_status, &$context) {
// Perform your batch processing here.
// Update the status for each related node.
foreach ($node_ids as $relatedNodeId) {
$relatedNode = Node::load($relatedNodeId);
$relatedNode->set('status', $node_status);
$relatedNode->save();

// Update the progress.
$context['results'][] = t('Node %node processed and status set to %status.',
['%node' => $relatedNodeId, '%status' => $node_status]);
}
}

/**
* Batch finished callback.
*/
function islandora_entity_status_batch_finished($success, $results, $operations) {
$messenger = \Drupal::messenger();
$message = '';

if ($success) {
if (!empty($results)) {
// Batch processing completed successfully.
// Display a message indicating success.
$messenger->addMessage(t('Batch processing completed successfully.'));

foreach ($results as $result) {
$message .= '<br>' . $result;
}
$messenger->addMessage(new Markup($message, 'html'));
}
}
else {
// Batch processing failed.
$messenger->addError(t('Batch processing failed.'));
}
}
7 changes: 7 additions & 0 deletions islandora_entity_status.routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
islandora_entity_status.node_edit_confirm:
path: '/confirm-edit/islandora_object'
defaults:
_form: '\Drupal\islandora_entity_status\Form\NodeEditConfirmForm'
_title: 'Confirm Edit'
requirements:
_permission: 'access content'
5 changes: 5 additions & 0 deletions islandora_entity_status.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
islandora_entity_status.find_update_related_nodes:
class: \Drupal\islandora_entity_status\Commands\CustomDrushCommands
tags:
- name: drush.command
59 changes: 59 additions & 0 deletions src/Commands/CustomDrushCommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Drupal\islandora_entity_status\Commands;

use Drush\Commands\DrushCommands;

class CustomDrushCommands extends DrushCommands {
nchiasson-dgi marked this conversation as resolved.
Show resolved Hide resolved

/**
* Find and update related nodes.
*
* @param string $nodes
* Comma-separated node IDs.
* @param int $status
* Status to be assigned (0 or 1).
*
* @command islandora_entity_status:find-update-related-nodes
* @aliases furnd
* @options nodes Comma-separated node IDs.
* @options status to be assigned (0 or 1).
*/
public function findUpdateRelatedNodes($nodes, $status) {
$nodeIds = explode(',', $nodes);

// Loop through each provided node ID.
foreach ($nodeIds as $nodeId) {
$this->updateRelatedNodes($nodeId, $status);
}

$this->logger()->success($this->t('Related nodes updated successfully.'));
}

/**
* Update status for related nodes.
*
* @param int $currentNodeId
* The current node ID.
* @param int $status
* Status to be assigned (0 or 1).
*/
private function updateRelatedNodes($currentNodeId, $status) {
// Use the provided function to find related nodes.
$relatedNodeIds = findCollectionNodes($currentNodeId);

// Include the provided node ID in the list of nodes to update.
$relatedNodeIds[] = $currentNodeId;

// Update the status for each related node.
foreach ($relatedNodeIds as $relatedNodeId) {
$node = \Drupal::entityTypeManager()->getStorage('node')->load($relatedNodeId);

if ($node) {
$node->set('status', $status);
$node->save();
}
}
}

}
Loading
Loading