Skip to content

Commit

Permalink
DGIR-48 : Confirmation screen and drush command
Browse files Browse the repository at this point in the history
  • Loading branch information
Prashant-bd committed Nov 21, 2023
1 parent 8c384be commit db19c88
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 91 deletions.
44 changes: 20 additions & 24 deletions islandora_entity_status.module
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use Drupal\node\NodeInterface;
use Drupal\media\Entity\Media;
use Drupal\node\Entity\Node;
use Twig\Markup;
use Drupal\Core\TempStore\PrivateTempStoreFactory;


/**
* Implements hook_ENTITY_TYPE_presave().
Expand Down Expand Up @@ -58,30 +60,23 @@ function islandora_entity_status_entity_update(Drupal\Core\Entity\EntityInterfac
function islandora_entity_status_form_alter(&$form, \Drupal\Core\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') {
$relatedNode = [];

// Get the current node ID from the route parameters.
$currentNodeId = NULL;

// Check if the current route is a node edit page and if we have a valid node.
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
$currentNodeId = $node->id();

// Load the nodes.
$relatedNodes = findCollectionNodes($currentNodeId);
$form['actions']['submit']['#submit'] = ['islandoraEntityStatusNodeEditSubmit'];
}
}

if (!empty($relatedNodes)) {
$message = "If you are changing status of this node, same will reflect to other collection nodes";
/**
* Submit callback for node edit form.
*/
function islandoraEntityStatusNodeEditSubmit(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
$node = $form_state->getFormObject()->getEntity();
$data = $form_state->cleanValues()->getValues();

// Attach the custom library.
$form['#attached']['library'][] = 'islandora_entity_status/confirm-popup';
// 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]);

// Pass the confirmation message to JavaScript.
$form['#attached']['drupalSettings']['custom_confirm_popup']['message'] = $message;
}
}
}
// Redirect to the confirmation page
$form_state->setRedirect('islandora_entity_status.node_edit_confirm');
}

/**
Expand Down Expand Up @@ -183,10 +178,11 @@ function islandora_entity_status_batch_finished($success, $results, $operations)
$message = '';

if ($success) {
// Batch processing completed successfully.
// Display a message indicating success.
$messenger->addMessage(t('Batch processing completed successfully.'));
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;
}
Expand Down
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
67 changes: 0 additions & 67 deletions js/confirm-popup.js

This file was deleted.

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 {

/**
* 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(dt('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();
}
}
}

}
126 changes: 126 additions & 0 deletions src/Form/NodeEditConfirmForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace Drupal\islandora_entity_status\Form;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;

/**
* Confirmation for editing a node.
*/
class NodeEditConfirmForm extends ConfirmFormBase {

/**
* Temporary storage for the 'node_edit_confirm'.
*
* @var \Drupal\Core\TempStore\PrivateTempStore
*/
protected $tempStore;

/**
* Entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* {@inheritdoc}
*/
public function __construct(PrivateTempStoreFactory $private_temp_store, EntityTypeManagerInterface $entity_type_manager) {
$this->tempStore = $private_temp_store->get('islandora_entity_status');
$this->entityTypeManager = $entity_type_manager;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('tempstore.private'),
$container->get('entity_type.manager'),
);
}

/**
* {@inheritdoc}
*/
public function getFormId() {
return 'node_edit_confirm_form';
}

/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to edit this node? The status of its associated collection items will be changed to the same.<br><br>');
}

/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('system.admin_content');
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Retrieve data from temporary storage
$nodeEditData = $this->tempStore->get('node_edit_data');

if (!$nodeEditData['node_id']) {
$form_state->setRedirect('entity.node.edit_form', ['node' => $nodeEditData['node_id']]);
}

$form['#title'] = 'Confirm Edit';

$form['question'] = [
'#markup' => $this->getQuestion(),
];

return parent::buildForm($form, $form_state, 'node');
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Perform any necessary logic before redirecting.
// Trigger node save with data from session.
$data = $this->tempStore->get('node_edit_data');

if (!empty($data) && is_array($data)) {
// Load the node.
$node = \Drupal\node\Entity\Node::load($data['node_id']);

if ($node instanceof \Drupal\node\NodeInterface) {

// Update the node fields with the latest data.
foreach ($data['data'] as $field_name => $field_value) {
// Need to skip node created data, not required for update.
if ($node->hasField($field_name) && $field_name != 'created') {
// For other fields, set values directly.
$node->set($field_name, $field_value);
}
}

// Save the updated node.
$node->save();
}
}

// Delete the data stored.
$this->tempStore->delete('node_edit_data');

// Redirect to the node detail page.
$url = Url::fromRoute('entity.node.canonical', ['node' => $data['node_id']]);
$form_state->setRedirectUrl($url);
}

}

0 comments on commit db19c88

Please sign in to comment.