Skip to content

Commit bf89c71

Browse files
authored
Merge pull request #117 from localgovdrupal/fix/1.x/101-undefined-array-key
fix: Warning: Undefined array key 0 in localgov_review_date_node_update()
2 parents f667b03 + ece0b0d commit bf89c71

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

modules/localgov_review_date/localgov_review_date.module

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -61,39 +61,41 @@ function localgov_review_date_node_update(NodeInterface $node) {
6161

6262
// Compare moderation state between original and saved node.
6363
$original = $node->original;
64-
$previous_state = $original->get('moderation_state')->getValue()[0]['value'];
65-
$new_state = $node->get('moderation_state')->getValue()[0]['value'];
64+
if ($original->hasField('moderation_state') && !$original->get('moderation_state')->isEmpty() && $original->get('moderation_state')->value) {
65+
$previous_state = $original->get('moderation_state')->getValue()[0]['value'];
66+
$new_state = $node->get('moderation_state')->getValue()[0]['value'];
6667

67-
// If node is being moved into the archvied state,
68-
// Find any scheduled transitions to review and delete them.
69-
if ($previous_state != $new_state && $new_state == 'archived') {
70-
$st_storage = \Drupal::entityTypeManager()->getStorage('scheduled_transition');
71-
$st_ids = $st_storage->getQuery()
72-
->condition('workflow', 'localgov_editorial')
73-
->condition('entity__target_type', 'node')
74-
->condition('entity__target_id', $node->id())
75-
->accessCheck(FALSE)
76-
->execute();
77-
$scheduled_transitions = $st_storage->loadMultiple($st_ids);
78-
foreach ($scheduled_transitions as $scheduled_transition) {
79-
// Using condition('moderation_state', 'review') above does not bring
80-
// in any results, so do an if check here if the transition is in review
81-
// and delete it.
82-
if ($scheduled_transition->get('moderation_state')->value == 'review') {
83-
$scheduled_transition->delete();
68+
// If node is being moved into the archvied state,
69+
// Find any scheduled transitions to review and delete them.
70+
if ($previous_state != $new_state && $new_state == 'archived') {
71+
$st_storage = \Drupal::entityTypeManager()->getStorage('scheduled_transition');
72+
$st_ids = $st_storage->getQuery()
73+
->condition('workflow', 'localgov_editorial')
74+
->condition('entity__target_type', 'node')
75+
->condition('entity__target_id', $node->id())
76+
->accessCheck(FALSE)
77+
->execute();
78+
$scheduled_transitions = $st_storage->loadMultiple($st_ids);
79+
foreach ($scheduled_transitions as $scheduled_transition) {
80+
// Using condition('moderation_state', 'review') above does not bring
81+
// in any results, so do an if check here if the transition is in review
82+
// and delete it.
83+
if ($scheduled_transition->get('moderation_state')->value == 'review') {
84+
$scheduled_transition->delete();
85+
}
8486
}
85-
}
8687

87-
// Also delete active review entities of this node.
88-
$review_storage = \Drupal::entityTypeManager()->getStorage('review_date');
89-
$review_ids = $review_storage->getQuery()
90-
->condition('entity', $node->id())
91-
->condition('active', 1)
92-
->accessCheck(FALSE)
93-
->execute();
94-
$review_entities = $review_storage->loadMultiple($review_ids);
95-
foreach ($review_entities as $review) {
96-
$review->delete();
88+
// Also delete active review entities of this node.
89+
$review_storage = \Drupal::entityTypeManager()->getStorage('review_date');
90+
$review_ids = $review_storage->getQuery()
91+
->condition('entity', $node->id())
92+
->condition('active', 1)
93+
->accessCheck(FALSE)
94+
->execute();
95+
$review_entities = $review_storage->loadMultiple($review_ids);
96+
foreach ($review_entities as $review) {
97+
$review->delete();
98+
}
9799
}
98100
}
99101
}

0 commit comments

Comments
 (0)