-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DGIR-48 : Confirmation screen and drush command
- Loading branch information
1 parent
8c384be
commit db19c88
Showing
6 changed files
with
217 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |