Commit 894727fc authored by Michael Strelan's avatar Michael Strelan Committed by Lee Rowlands
Browse files

Issue #3006343 by mstrelan, larowlan: Unable to delete email template without...

Issue #3006343 by mstrelan, larowlan: Unable to delete email template without also deleting workflow if it has been associated
parent dbd5f258
Loading
Loading
Loading
Loading
+9 −14
Original line number Diff line number Diff line
workflows.workflow.*.third_party.workbench_email:
  type: mapping
  label: 'Workbench email settings'
  mapping:
    workbench_email_templates:
      type: sequence
      label: 'Transition templates'
      sequence:
        type: sequence
        label: 'Transition'
        sequence:
          type: string
          label: 'Template'

workbench_email.workbench_email_template.*:
  type: config_entity
  label: 'Email Template config'
@@ -57,6 +43,15 @@ workbench_email.workbench_email_template.*:
    dependencies:
      type: config_dependencies
      label: 'Dependencies'
    transitions:
      type: sequence
      label: 'Transitions'
      sequence:
        type: sequence
        label: 'Workflows'
        sequence:
          label: 'Transition'
          type: string

workbench_email_recipient_type:
  type: mapping
+29 −1
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\workbench_email\Plugin\RecipientTypeInterface;
use Drupal\workbench_email\RecipientTypePluginCollection;
use Drupal\workbench_email\TemplateInterface;
use Drupal\workflows\Entity\Workflow;

/**
 * Defines the Email Template entity.
@@ -49,6 +50,7 @@ use Drupal\workbench_email\TemplateInterface;
 *     "bundles",
 *     "recipient_types",
 *     "replyTo",
 *     "transitions"
 *   }
 * )
 */
@@ -127,6 +129,13 @@ class Template extends ConfigEntityBase implements TemplateInterface {
   */
  protected $bundles = [];

  /**
   * Configured transitions this template.
   *
   * @var array[]
   */
  protected $transitions = [];

  /**
   * {@inheritdoc}
   */
@@ -155,6 +164,13 @@ class Template extends ConfigEntityBase implements TemplateInterface {
    return $this->replyTo;
  }

  /**
   * {@inheritdoc}
   */
  public function getTransitions(): array {
    return $this->transitions;
  }

  /**
   * {@inheritdoc}
   */
@@ -179,6 +195,14 @@ class Template extends ConfigEntityBase implements TemplateInterface {
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setTransitions(array $transitions): static {
    $this->transitions = $transitions;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
@@ -206,11 +230,15 @@ class Template extends ConfigEntityBase implements TemplateInterface {
  public function calculateDependencies() {
    parent::calculateDependencies();
    foreach ($this->bundles as $bundle) {
      list($entity_type_id, $bundle_id) = explode(':', $bundle, 2);
      [$entity_type_id, $bundle_id] = explode(':', $bundle, 2);
      $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
      $bundle_config_dependency = $entity_type->getBundleConfigDependency($bundle_id);
      $this->addDependency($bundle_config_dependency['type'], $bundle_config_dependency['name']);
    }
    $workflows = Workflow::loadMultiple(array_keys($this->getTransitions()));
    foreach ($workflows as $workflow) {
      $this->addDependency($workflow->getConfigDependencyKey(), $workflow->getConfigDependencyName());
    }
    return $this;
  }

+24 −9
Original line number Diff line number Diff line
@@ -9,6 +9,8 @@ use Drupal\Core\Queue\QueueFactory;
use Drupal\Core\Session\AccountInterface;
use Drupal\workbench_email\QueuedEmail;
use Drupal\workbench_email\TemplateInterface;
use Drupal\workflows\TransitionInterface;
use Drupal\workflows\WorkflowInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
@@ -65,7 +67,6 @@ class WorkbenchTransitionEventSubscriber implements EventSubscriberInterface {
      $from = $event->getOriginalState();
    }
    $to = $event->getNewState();
    $templates = $workflow->getThirdPartySetting('workbench_email', 'workbench_email_templates', []);

    try {
      $transition = $type_plugin->getTransitionFromStateToState($from, $to);
@@ -75,15 +76,8 @@ class WorkbenchTransitionEventSubscriber implements EventSubscriberInterface {
      return;
    }

    // Check the transition has a template
    if (!isset($templates[$transition->id()])) {
      return;
    }
    /** @var \Drupal\Core\Queue\QueueInterface $queue */
    $queue = $this->queueFactory->get('workbench_email_send' . PluginBase::DERIVATIVE_SEPARATOR . $entity->getEntityTypeId());

    /** @var \Drupal\workbench_email\TemplateInterface $template */
    foreach ($this->entityTypeManager->getStorage('workbench_email_template')->loadMultiple($templates[$transition->id()]) as $template) {
    foreach ($this->getTemplatesForTransition($workflow, $transition) as $template) {
      if ($template->getBundles() && !in_array($entity->getEntityTypeId() . ':' . $entity->bundle(), $template->getBundles(), TRUE)) {
        // Continue, invalid bundle.
        continue;
@@ -118,4 +112,25 @@ class WorkbenchTransitionEventSubscriber implements EventSubscriberInterface {
    return $template->getRecipients($entity);
  }

  /**
   * Gets all templates for a transition.
   *
   * @param \Drupal\workflows\WorkflowInterface $workflow
   *   The workflow.
   * @param \Drupal\workflows\TransitionInterface $transition
   *   The transition.
   *
   * @return \Drupal\workbench_email\TemplateInterface[]
   *   Templates for the transition.
   */
  protected function getTemplatesForTransition(WorkflowInterface $workflow, TransitionInterface $transition): array {
    $storage = $this->entityTypeManager->getStorage('workbench_email_template');
    return array_reduce($storage->loadMultiple(), function (array $carry, TemplateInterface $template) use ($workflow, $transition): array {
      if (array_key_exists($transition->id(), $template->getTransitions()[$workflow->id()] ?? [])) {
        $carry[] = $template;
      }
      return $carry;
    }, []);
  }

}
+24 −1
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\workbench_email\Form;

use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityInterface;
@@ -10,10 +11,12 @@ use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformState;
use Drupal\Core\Link;
use Drupal\Core\Messenger\Messenger;
use Drupal\Core\Form\SubformState;
use Drupal\Core\Url;
use Drupal\workbench_email\TemplateInterface;
use Drupal\workflows\Entity\Workflow;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
@@ -272,6 +275,25 @@ class TemplateForm extends EntityForm {
      '#title' => $this->t('Bundles'),
      '#description' => $this->t('Limit to the following bundles. Select none to include all bundles.'),
    ];

    $form['transitions'] = [
      '#type' => 'details',
      '#title' => $this->t('Transitions'),
      '#open' => TRUE,
      '#tree' => TRUE,
    ];
    $workflows = Workflow::loadMultiple();
    foreach ($workflows as $workflow) {
      $form['transitions'][$workflow->id()] = [
        '#title' => $workflow->label(),
        '#type' => 'checkboxes',
        '#options' => array_map(function ($transition) {
          return $transition->label();
        }, $workflow->getTypePlugin()->getTransitions()),
      ];
      $form['transitions'][$workflow->id()]['#default_value'] = $workbench_email_template->getTransitions()[$workflow->id()] ?? [];
    }

    return $form;
  }

@@ -342,6 +364,7 @@ class TemplateForm extends EntityForm {
    }
    $entity->set('recipient_types', $types);
    $entity->set('bundles', array_filter($entity->get('bundles')));
    $entity->set('transitions', NestedArray::filter($entity->get('transitions')));
  }

  /**
+19 −0
Original line number Diff line number Diff line
@@ -43,6 +43,14 @@ interface TemplateInterface extends ConfigEntityInterface, EntityWithPluginColle
   */
  public function getReplyTo();

  /**
   * Gets the template transitions.
   *
   * @return array[]
   *   Template transitions.
   */
  public function getTransitions(): array;

  /**
   * Sets the body.
   *
@@ -76,6 +84,17 @@ interface TemplateInterface extends ConfigEntityInterface, EntityWithPluginColle
   */
  public function setReplyTo($replyTo);

  /**
   * Sets the transitions.
   *
   * @param array[] $transitions
   *   Template transitions.
   *
   * @return self
   *   Called instance.
   */
  public function setTransitions(array $transitions): static;

  /**
   * Returns the ordered collection of recipient type plugin instances or an individual plugin instance.
   *
Loading