Commit a99315ec authored by Jonathan Smith's avatar Jonathan Smith
Browse files

Issue #3276637 by jonathan1055: Make Rules Integration compatible with php8

parent ce66decb
Loading
Loading
Loading
Loading
+47 −0
Changes for scheduler.install: 47 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -170,3 +170,50 @@ function scheduler_update_8203() {
  }
  return $output ? implode('<br>', $output) : t('Nothing requires updating for Taxonomy Terms.');
}

/**
 * Update Rules actions and conditions to use 'entity' context.
 */
function scheduler_update_8204() {
  // The entity context names need to be 'entity' for all entity types, not
  // 'node', 'media', 'commerce_product' or 'taxonomy_term'. This is for PHP8
  // compatibility, fixing "Unknown named parameter in call_user_func_array()"
  // See https://www.drupal.org/project/scheduler/issues/3276637
  $rules = \Drupal::configFactory()->listAll('rules.reaction');
  $rules_updated = [];
  foreach ($rules as $config_id) {
    $rule = \Drupal::configFactory()->getEditable($config_id);
    $changed = FALSE;

    // The expression array has 'conditions' and 'actions' elements which have
    // the same structure, so can be fixed using the same loop process.
    $expression = $rule->get('expression');
    foreach (['condition_id' => 'conditions', 'action_id' => 'actions'] as $idx => $group) {
      foreach ($expression[$group][$group] as $key => $cond_act) {
        if (substr($cond_act[$idx], 0, 10) == 'scheduler_' && !empty($cond_act['context_mapping'])) {
          foreach ($cond_act['context_mapping'] as $name => $value) {
            if (in_array($name, ['node', 'media', 'commerce_product', 'taxonomy_term'])) {
              // Replace the node/media/commerce_product key with 'entity'.
              unset($expression[$group][$group][$key]['context_mapping'][$name]);
              $expression[$group][$group][$key]['context_mapping']['entity'] = $value;
              // Only add the rule label once.
              $changed ?: $rules_updated[] = $rule->get('label');
              $changed = TRUE;
            }
          }
        }
      }
    }

    // Replace the config value with the updated expression array.
    if ($changed) {
      $rule->set('expression', $expression);
      $rule->save();
    }
  }

  $output = empty($rules_updated) ? t('No reaction rules required updating with entity context.') :
    \Drupal::translation()->formatPlural(count($rules_updated), '1 reaction rule updated with entity context', '@count reaction rules updated with entity context')
    . '<br>' . implode('<br>', $rules_updated);
  return $output;
}
+5 −1
Changes for scheduler_rules_integration/src/Plugin/Condition/ConditionDeriver.php: 5 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -126,7 +126,11 @@ class ConditionDeriver extends DeriverBase implements ContainerDeriverInterface
        'label' => $label,
        'entity_type_id' => $entity_type_id,
        'category' => $entity_type->getLabel() . ' (' . $this->t('Scheduler') . ')',
        'context_definitions' => [$entity_type_id => $entity_context_definition],
        // The context parameter names have to be consistent across all entity
        // types (we cannot use $entity_type_id). This avoids PHP8 failing with
        // 'unknown named parameter' in call_user_func_array()
        // @see https://www.drupal.org/project/scheduler/issues/3276637
        'context_definitions' => ['entity' => $entity_context_definition],
      ];

      // Add the full definition to the derivatives array.
+1 −1
Changes for scheduler_rules_integration/src/Plugin/Condition/Legacy/LegacyPublishingIsEnabled.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ use Drupal\scheduler_rules_integration\Plugin\Condition\PublishingIsEnabled;
 *   label = @Translation("Node type is enabled for scheduled publishing"),
 *   category = @Translation("Content (Scheduler)"),
 *   context_definitions = {
 *     "node" = @ContextDefinition("entity:node",
 *     "entity" = @ContextDefinition("entity:node",
 *       label = @Translation("Node"),
 *       description = @Translation("The node to check for the type being enabled for scheduled publishing."),
 *       assignment_restriction = "selector",
+1 −1
Changes for scheduler_rules_integration/src/Plugin/Condition/Legacy/LegacyScheduledForPublishing.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ use Drupal\scheduler_rules_integration\Plugin\Condition\ScheduledForPublishing;
 *   label = @Translation("Node is scheduled for publishing"),
 *   category = @Translation("Content (Scheduler)"),
 *   context_definitions = {
 *     "node" = @ContextDefinition("entity:node",
 *     "entity" = @ContextDefinition("entity:node",
 *       label = @Translation("Node"),
 *       description = @Translation("The node to check for having a scheduled publishing date."),
 *       assignment_restriction = "selector",
+1 −1
Changes for scheduler_rules_integration/src/Plugin/Condition/Legacy/LegacyScheduledForUnpublishing.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ use Drupal\scheduler_rules_integration\Plugin\Condition\ScheduledForUnpublishing
 *   label = @Translation("Node is scheduled for unpublishing"),
 *   category = @Translation("Content (Scheduler)"),
 *   context_definitions = {
 *     "node" = @ContextDefinition("entity:node",
 *     "entity" = @ContextDefinition("entity:node",
 *       label = @Translation("Node"),
 *       description = @Translation("The node to check for having a scheduled unpublishing date."),
 *       assignment_restriction = "selector",
Loading