Commit 0557670e authored by Adam Shepherd's avatar Adam Shepherd
Browse files

Issue #3263870 by AdamPS: Add view/edit of related policy to existing config/settings forms

parent aaaa3dc1
Loading
Loading
Loading
Loading
+24 −0
Changes for src/Controller/SymfonyMailerController.php: 24 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -3,7 +3,9 @@
namespace Drupal\symfony_mailer\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\symfony_mailer\Entity\MailerPolicy;
use Drupal\symfony_mailer\MailerTransportInterface;
use Symfony\Component\HttpFoundation\Request;

/**
 * Route controller for symfony mailer.
@@ -25,4 +27,26 @@ class SymfonyMailerController extends ControllerBase {
    return $this->redirect('entity.mailer_transport.collection');
  }

  /**
   * Creates a policy and redirects to the edit page.
   *
   * @param string $policy_id
   *   The policy ID.
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request.
   *
   * @return \Symfony\Component\HttpFoundation\RedirectResponse
   *   A redirect to the policy edit page.
   */
  public function createPolicy(string $policy_id, Request $request = NULL) {
    MailerPolicy::create(['id' => $policy_id])->save();
    $options = [];
    $query = $request->query;
    if ($query->has('destination')) {
      $options['query']['destination'] = $query->get('destination');
      $query->remove('destination');
    }
    return $this->redirect('entity.mailer_policy.edit_form', ['mailer_policy' => $policy_id], $options);
  }

}
+13 −0
Changes for src/Entity/MailerPolicy.php: 13 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -282,4 +282,17 @@ class MailerPolicy extends ConfigEntityBase implements EntityWithPluginCollectio
      strnatcasecmp($a->getEntityLabel(), $b->getEntityLabel());
  }

  /**
   * Loads a Mailer Policy, or creates a new one.
   *
   * @param string $id
   *   The id of the policy to load or create.
   *
   * @return static
   *   The policy object.
   */
  public static function loadOrCreate($id) {
    return static::load($id) ?? static::create(['id' => $id]);
  }

}

src/MailerHelper.php

0 → 100644
+124 −0
Changes for src/MailerHelper.php: 124 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\symfony_mailer;

use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\symfony_mailer\Processor\EmailAdjusterManager;
use Drupal\symfony_mailer\Processor\EmailBuilderManager;

/**
 * Provides the mailer helper service.
 */
class MailerHelper implements MailerHelperInterface {

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

  /**
   * The email adjuster manager.
   *
   * @var \Drupal\symfony_mailer\Processor\EmailAdjusterManager
   */
  protected $adjusterManager;

  /**
   * The email builder manager.
   *
   * @var \Drupal\symfony_mailer\Processor\EmailBuilderManager
   */
  protected $builderManager;

  /**
   * Constructs the MailerHelper object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\symfony_mailer\Processor\EmailAdjusterManager $email_adjuster_manager
   *   The email adjuster manager.
   * @param \Drupal\symfony_mailer\Processor\EmailBuilderManager $email_builder_manager
   *   The email builder manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, EmailAdjusterManager $email_adjuster_manager, EmailBuilderManager $email_builder_manager) {
    $this->entityTypeManager = $entity_type_manager;
    $this->adjusterManager = $email_adjuster_manager;
    $this->builderManager = $email_builder_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function renderEntityPolicy(ConfigEntityInterface $entity, string $subtype, array $common_adjusters = ['email_subject', 'email_from']) {
    $type = $entity->getEntityTypeId();
    $element = $this->renderCommon($type, $common_adjusters);
    $policy_id = "$type.$subtype";
    $entities = [$policy_id, $policy_id . '.' . $entity->id()];
    $element['listing'] = $this->entityTypeManager->getListBuilder('mailer_policy')
      ->overrideEntities($entities)
      ->hideColumns(['type', 'sub_type'])
      ->render();

    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function renderTypePolicy(string $type, array $common_adjusters = ['email_subject', 'email_from']) {
    $element = $this->renderCommon($type, $common_adjusters);
    $entities = [$type];
    foreach (array_keys($this->builderManager->getDefinition($type)['sub_types']) as $subtype) {
      $entities[] = "$type.$subtype";
    }

    $element['listing'] = $this->entityTypeManager->getListBuilder('mailer_policy')
      ->overrideEntities($entities)
      ->hideColumns(['type', 'entity'])
      ->render();

    return $element;
  }

  /**
   * Renders common parts for policy elements.
   *
   * @param string $type
   *   Type of the policies to show.
   * @param string[] $common_adjusters
   *   ID of EmailAdjusters to use as an example in the description.
   *
   * @return array
   *   The render array.
   */
  protected function renderCommon(string $type, array $common_adjusters) {
    $element = [
      '#type' => 'fieldset',
      '#title' => t('Mailer policy'),
      '#collapsible' => FALSE,
      '#description' => t('If you have made changes on this page, please save them before editing policy.'),
    ];

    foreach ($common_adjusters as $adjuster_id) {
      $adjuster_names[] = $this->adjusterManager->getDefinition($adjuster_id)['label'];
    }
    $label = $this->builderManager->getDefinition($type)['label'];

    $element['explanation'] = [
      '#prefix' => '<p>',
      '#markup' => t('Configure Mailer policy records to customise the emails sent for @label. You can set the @adjusters and more.', ['@label' => $label, '@adjusters' => implode(', ', $adjuster_names)]),
      '#suffix' => '</p>',
    ];

    return $element;
  }

}
+45 −0
Changes for src/MailerHelperInterface.php: 45 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\symfony_mailer;

use Drupal\Core\Config\Entity\ConfigEntityInterface;

/**
 * Provides the mailer helper service.
 */
interface MailerHelperInterface {

  /**
   * Renders an element that lists policy related to a config entity.
   *
   * The element is designed for insertion into a config edit page for an
   * entity that has a related EmailBuilder.
   *
   * @param \Drupal\Core\Config\Entity\ConfigEntityInterface $entity
   *   Config entity being edited.
   * @param string $subtype
   *   Sub-type of the policies to show.
   * @param string[] $common_adjusters
   *   ID of EmailAdjusters to use as an example in the description.
   *
   * @return array
   *   The render array.
   */
  public function renderEntityPolicy(ConfigEntityInterface $entity, string $subtype, array $common_adjusters = ['email_subject', 'email_from']);

  /**
   * Renders an element that lists policy for a specific type.
   *
   * The element is designed for insertion into a settings page for a module.
   *
   * @param string $type
   *   Type of the policies to show.
   * @param string[] $common_adjusters
   *   ID of EmailAdjusters to use as an example in the description.
   *
   * @return array
   *   The render array.
   */
  public function renderTypePolicy(string $type, array $common_adjusters = ['email_subject', 'email_from']);

}
+80 −3
Changes for src/MailerPolicyListBuilder.php: 80 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -4,13 +4,26 @@ namespace Drupal\symfony_mailer;

use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
use Drupal\symfony_mailer\Entity\MailerPolicy;

/**
 * Defines a class to build a listing of mailer policy entities.
 *
 * @todo Add filters by type and by adjuster.
 */
class MailerPolicyListBuilder extends ConfigEntityListBuilder {
class MailerPolicyListBuilder extends ConfigEntityListBuilder implements MailerPolicyListBuilderInterface {

  /**
   * Overridden list of entities.
   *
   * @var \Drupal\Core\Entity\EntityInterface[]
   */
  protected $overrideEntities;

  protected $filterType;

  protected $hideColumns = [];

  /**
   * {@inheritdoc}
@@ -22,7 +35,7 @@ class MailerPolicyListBuilder extends ConfigEntityListBuilder {
      'entity' => $this->t('Entity'),
      'summary' => $this->t('Summary'),
    ];
    return $header + parent::buildHeader();
    return array_diff_key($header, $this->hideColumns) + parent::buildHeader();
  }

  /**
@@ -35,7 +48,71 @@ class MailerPolicyListBuilder extends ConfigEntityListBuilder {
      'entity' => $entity->getEntityLabel(),
      'summary' => $entity->getSummary(),
    ];
    return $row + parent::buildRow($entity);
    return array_diff_key($row, $this->hideColumns) + parent::buildRow($entity);
  }

  /**
   * {@inheritdoc}
   */
  public function load() {
    return $this->overrideEntities ?? parent::load();
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultOperations(EntityInterface $entity) {
    if ($entity->isNew()) {
      $operations['create'] = [
        'title' => t('Create'),
        'weight' => -10,
        'url' => $this->ensureDestination(Url::fromRoute('entity.mailer_policy.add_id_form', ['policy_id' => $entity->id()])),
      ];
    }
    else {
      $operations = parent::getDefaultOperations($entity);
    }

    return $operations;
  }

  /**
   * {@inheritdoc}
   */
  protected function getEntityIds() {
    $query = $this->getStorage()->getQuery();

    if ($this->filterType) {
      $query->condition('id', "$this->filterType.", 'STARTS_WITH');
    }

    return $query->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function overrideEntities(array $entity_ids) {
    foreach ($entity_ids as $policy_id) {
      $this->overrideEntities[] = MailerPolicy::loadOrCreate($policy_id);
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function filterType(string $type) {
    $this->filterType = $type;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function hideColumns(array $columns) {
    $this->hideColumns = array_flip($columns);
    return $this;
  }

}
Loading