Skip to content
This repository has been archived by the owner on May 3, 2020. It is now read-only.

Commit

Permalink
Added the ability to map a email field for Courier mail integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
dpi committed Nov 9, 2016
1 parent 620eb46 commit a435329
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 2 deletions.
3 changes: 3 additions & 0 deletions config/schema/rng_contact.schema.yml
Expand Up @@ -11,3 +11,6 @@ rng_contact.rng_contact_type.*:
description:
type: text
label: 'Description'
courier_email_field:
type: text
label: 'ID of email field to use when sending mailings via Courier.'
6 changes: 6 additions & 0 deletions rng_contact.links.menu.yml
Expand Up @@ -3,3 +3,9 @@ entity.rng_contact_type.collection:
parent: system.admin_structure
description: 'Manage RNG contact types.'
route_name: entity.rng_contact_type.collection

rng_contact.config.courier:
title: 'RNG contact settings'
parent: rng.config
description: 'Manage settings for RNG contact.'
route_name: rng_contact.config.settings
5 changes: 5 additions & 0 deletions rng_contact.permissions.yml
@@ -1,3 +1,8 @@
administer rng_contact:
title: 'Administer RNG Contact'
description: 'Manage settings for RNG Contact.'
restrict access: TRUE

administer rng_contact types:
title: 'Administer RNG contact types'
description: 'Create, and perform any action on RNG contact types.'
Expand Down
7 changes: 7 additions & 0 deletions rng_contact.routing.yml
@@ -0,0 +1,7 @@
rng_contact.config.settings:
path: '/admin/config/rng_contact/settings'
defaults:
_form: '\Drupal\rng_contact\Form\RngContactSettingsForm'
_title: 'RNG Contact settings'
requirements:
_permission: 'administer rng_contact'
58 changes: 58 additions & 0 deletions src/Entity/RngContactType.php
Expand Up @@ -3,6 +3,8 @@
namespace Drupal\rng_contact\Entity;

use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Field\FieldConfigInterface;
use Drupal\field\Entity\FieldConfig;

/**
* Defines the contact type configuration entity.
Expand Down Expand Up @@ -62,4 +64,60 @@ class RngContactType extends ConfigEntityBundleBase implements RngContactTypeInt
*/
public $description;

/**
* ID of email field to use when sending mailings via Courier.
*
* @var string
*/
protected $courier_email_field;

/**
* {@inheritdoc}
*/
public function getCourierEmailField() {
return $this->courier_email_field;
}

/**
* {@inheritdoc}
*/
public function setCourierEmailField($courier_email_field) {
$this->courier_email_field = $courier_email_field;
return $this;
}

/**
* {@inheritdoc}
*/
public function calculateDependencies() {
parent::calculateDependencies();

$email_field_id = $this->getCourierEmailField();
$email_field = FieldConfig::loadByName('rng_contact', $this->id(), $email_field_id);
if ($email_field) {
$this->addDependency('config', $email_field->getConfigDependencyName());
}

return $this;
}

/**
* {@inheritdoc}
*/
public function onDependencyRemoval(array $dependencies) {
$changed = parent::onDependencyRemoval($dependencies);

foreach ($dependencies['config'] as $entity) {
if ($entity instanceof FieldConfigInterface) {
// Courier email field is being deleted.
if ($entity->getName() === $this->getCourierEmailField()) {
$this->setCourierEmailField(NULL);
$changed = TRUE;
}
}
}

return $changed;
}

}
19 changes: 19 additions & 0 deletions src/Entity/RngContactTypeInterface.php
Expand Up @@ -9,4 +9,23 @@
*/
interface RngContactTypeInterface extends ConfigEntityInterface {

/**
* Get the email field to use for Courier email communications.
*
* @return string|NULL
* The email field to use for Courier email communications.
*/
public function getCourierEmailField();

/**
* Set the email field to use for Courier email communications.
*
* @param string|NULL $courier_email_field
* The email field to use for Courier email communications.
*
* @return $this
* Return this contact type for chaining.
*/
public function setCourierEmailField($courier_email_field);

}
131 changes: 131 additions & 0 deletions src/Form/RngContactSettingsForm.php
@@ -0,0 +1,131 @@
<?php

namespace Drupal\rng_contact\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\rng_contact\Entity\RngContactType;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;

/**
* Configure RNG Contact settings.
*/
class RngContactSettingsForm extends ConfigFormBase {

/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;

/**
* Constructs a RngContactSettingsForm object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager.
*/
public function __construct(ConfigFactoryInterface $config_factory, EntityFieldManagerInterface $entity_field_manager) {
parent::__construct($config_factory);
$this->entityFieldManager = $entity_field_manager;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('entity_field.manager')
);
}

/**
* {@inheritdoc}
*/
public function getFormId() {
return 'rng_contact_settings';
}

/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'rng_contact.settings',
];
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);

$form['table_help'] = [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $this->t('Select email fields Courier should use when communicating with contacts.'),
];

$form['table'] = [
'#type' => 'table',
'#header' => [
$this->t('Contact type'),
$this->t('Email field'),
],
'#empty' => $this->t('There are no people types available.'),
];

foreach (RngContactType::loadMultiple() as $contact_type) {
$row = [];
$row['label'] = [
'#markup' => $contact_type->label()
];

/** @var \Drupal\rng_contact\Entity\RngContactTypeInterface $contact_type */
$field_definitions = $this->entityFieldManager
->getFieldDefinitions('rng_contact', $contact_type->id());

$field_options = [];
foreach ($field_definitions as $field_definition) {
$field_type = $field_definition->getType();
if ($field_type == 'email') {
$field_options[$field_definition->getName()] = $field_definition->getLabel();
}
}

$row['email_field'] = [
'#type' => 'select',
'#title_display' => 'invisible',
'#empty_option' => $this->t('- Disable -'),
'#empty_value' => NULL,
'#options' => $field_options,
'#default_value' => $contact_type->getCourierEmailField(),
];

$form['table'][$contact_type->id()] = $row;
}

return $form;
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
foreach ($form_state->getValue('table') as $contact_type_id => $row) {
/** @var \Drupal\rng_contact\Entity\RngContactTypeInterface $contact_type */
$contact_type = RngContactType::load($contact_type_id);
$email_field = !empty($row['email_field']) ? $row['email_field'] : NULL;
$contact_type->setCourierEmailField($email_field);
$contact_type->save();
}
drupal_set_message($this->t('Settings updated.'));
}

}
19 changes: 17 additions & 2 deletions src/Plugin/IdentityChannel/CourierEmail/RngContact.php
Expand Up @@ -2,9 +2,11 @@

namespace Drupal\rng_contact\Plugin\IdentityChannel\CourierEmail;

use Drupal\courier\Exception\IdentityException;
use Drupal\courier\Plugin\IdentityChannel\IdentityChannelPluginInterface;
use Drupal\courier\ChannelInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\rng_contact\Entity\RngContactType;

/**
* Supports rng_contact entities.
Expand All @@ -25,8 +27,21 @@ class RngContact implements IdentityChannelPluginInterface {
public function applyIdentity(ChannelInterface &$message, EntityInterface $identity) {
/** @var \Drupal\rng_contact\Entity\RngContactInterface $identity */
/** @var \Drupal\courier\EmailInterface $message */
$message->setRecipientName($identity->label());
// $message->setEmailAddress($identity->mail->value);
$contact_type = RngContactType::load($identity->bundle());
$email_field = $contact_type->getCourierEmailField();
if ($email_field && isset($identity->{$email_field})) {
$email = $identity->{$email_field};
if (!empty($email->value)) {
$message->setRecipientName($identity->label());
$message->setEmailAddress($email->value);
}
else {
throw new IdentityException('Contact missing email address.');
}
}
else {
throw new IdentityException('Contact type email field not configured.');
}
}

}

0 comments on commit a435329

Please sign in to comment.