Commit 4a8cd535 authored by Adam Shepherd's avatar Adam Shepherd
Browse files

Issue #3271804 by AdamPS: Improvements to policy embedding

parent 4d230bb8
Loading
Loading
Loading
Loading
+36 −7
Changes for src/Entity/MailerPolicy.php: 36 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -41,6 +41,13 @@ class MailerPolicy extends ConfigEntityBase implements EntityWithPluginCollectio

  use StringTranslationTrait;

  /**
   * Maximum length of a summary for a adjuster.
   *
   * @var int
   */
  protected const MAX_SUMMARY = 50;

  /**
   * The unique ID of the policy record.
   *
@@ -246,19 +253,41 @@ class MailerPolicy extends ConfigEntityBase implements EntityWithPluginCollectio
  /**
   * Gets a short human-readable summary of the configured policy.
   *
   * @param bool $expanded
   *   (Optional) If FALSE return just the labels. If TRUE include a short
   *   summary of each element.
   *
   * @return string
   *   Summary text.
   */
  public function getSummary() {
  public function getSummary($expanded = FALSE) {
    $summary = [];
    $plugin_ids = array_keys($this->getConfiguration());
    asort($plugin_ids);
    foreach ($plugin_ids as $plugin_id) {
      if ($definition = $this->emailAdjusterManager->getDefinition($plugin_id, FALSE)) {
        $summary[] = $definition['label'];
    $separator = ', ';

    foreach ($this->adjusters()->sort() as $adjuster) {
      $element = $adjuster->getLabel();
      if ($expanded && ($element_summary = $adjuster->getSummary())) {
        if (strlen($element_summary) > static::MAX_SUMMARY) {
          $element_summary = substr($element_summary, 0, static::MAX_SUMMARY) . '…';
        }
        $element .= ": $element_summary";
        $separator = '<br>';
      }
      $summary[] = $element;

    }
    return implode(', ', $summary);

    return implode($separator, $summary);
  }

  /**
   * Returns the common adjusters for this policy.
   *
   * @return array
   *   An array of common adjuster IDs.
   */
  public function getCommonAdjusters() {
    return $this->builderDefinition ? $this->builderDefinition['common_adjusters'] : [];
  }

  /**
+10 −0
Changes for src/Form/PolicyEditForm.php: 10 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -52,12 +52,22 @@ class PolicyEditForm extends EntityForm {
      '#attributes' => ['class' => ['container-inline']],
    ];

    // Put the common adjusters first.
    $common_adjusters = array_flip($this->entity->getCommonAdjusters());
    $options = $options2 = [];
    foreach ($this->entity->adjusterDefinitions() as $name => $definition) {
      if (!$adjusters->has($name)) {
        if (isset($common_adjusters[$name])) {
          $options[$name] = $definition['label'];
        }
        else {
          $options2[$name] = $definition['label'];
        }
      }
    }
    asort($options);
    asort($options2);
    $options += $options2;

    $form['add_actions']['add_select'] = [
      '#type' => 'select',
+2 −1
Changes for src/MailerPolicyListBuilder.php: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -42,11 +42,12 @@ class MailerPolicyListBuilder extends ConfigEntityListBuilder implements MailerP
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {
    $summary['data']['#markup'] = $entity->getSummary(!empty($this->overrideEntities));
    $row = [
      'type' => $entity->getTypeLabel(),
      'sub_type' => $entity->getSubTypeLabel(),
      'entity' => $entity->getEntityLabel(),
      'summary' => $entity->getSummary(),
      'summary' => $summary,
    ];
    return array_diff_key($row, $this->hideColumns) + parent::buildRow($entity);
  }
+22 −0
Changes for src/Plugin/EmailAdjuster/AddressAdjusterBase.php: 22 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -100,6 +100,28 @@ abstract class AddressAdjusterBase extends EmailAdjusterBase {
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function getSummary() {
    foreach ($this->configuration['addresses'] as $item) {
      $value = $item['value'];
      $display = $item['display'];

      if ($value === '<site>') {
        $summary[] = $this->t('Site email address');
      }
      elseif ((strpos($value, '@') === FALSE) && ($user = User::load($value))) {
        $summary[] = $user->getDisplayName();
      }
      else {
        $summary[] = $value;
      }
    }

    return implode(', ', $summary);
  }

  /**
   * Ajax callback to update the form.
   */
+1 −1
Changes for src/Plugin/EmailAdjuster/BccEmailAdjuster.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@ use Drupal\symfony_mailer\EmailInterface;
 *
 * @EmailAdjuster(
 *   id = "email_bcc",
 *   label = @Translation("Bcc header"),
 *   label = @Translation("Bcc"),
 *   description = @Translation("Sets the email bcc header."),
 * )
 */
Loading