I know its bad form to post an answer to your own question, but it may help others who wish to add the email to the address.
Add di.xml in modules app/code/myvendorname/mymodulename/etc directory:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Customer\Block\Address\Renderer\DefaultRenderer"
type="myvendorname\mymodulename\Block\Address\Renderer\DefaultRenderer"/>
</config>
Add module.xml to the same directory:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="mymodulename" setup_version="1.0.0">
</module>
</config>
Add registration.php to the app/code/myvendorname/mymodulename directory:
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'mymodulename',
__DIR__
);
Add DefaultRenderer.php to app/code/myvendorname/mymodulename/Block/Address/Renderer/
<?php
namespace myvendorname/mymodulename\Block\Address\Renderer;
use Magento\Customer\Model\Address\Mapper; use
Magento\Customer\Model\Metadata\ElementFactory;
class DefaultRenderer extends
\Magento\Customer\Block\Address\Renderer\DefaultRenderer {
protected $_customerMetadataService;
public function __construct(
\Magento\Framework\View\Element\Context $context,
ElementFactory $elementFactory,
\Magento\Directory\Model\CountryFactory $countryFactory,
\Magento\Customer\Api\AddressMetadataInterface $metadataService,
Mapper $addressMapper,
\Magento\Customer\Api\CustomerMetadataInterface $customerMetadataService,
array $data = []
)
{
parent::__construct($context, $elementFactory, $countryFactory,
$metadataService, $addressMapper, $data);
$this->_customerMetadataService = $customerMetadataService;
}
/**
- {@inheritdoc}
- @SuppressWarnings(PHPMD.CyclomaticComplexity)
- @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function renderArray($addressAttributes, $format = null)
{
switch ($this->getType()->getCode()) {
case 'html':
$dataFormat = ElementFactory::OUTPUT_FORMAT_HTML;
break;
case 'pdf':
$dataFormat = ElementFactory::OUTPUT_FORMAT_PDF;
break;
case 'oneline':
$dataFormat = ElementFactory::OUTPUT_FORMAT_ONELINE;
break;
default:
$dataFormat = ElementFactory::OUTPUT_FORMAT_TEXT;
break;
}
$attributesMetadata = $this->_addressMetadataService->getAllAttributesMetadata();
if (null !== $this->_customerMetadataService->getAttributeMetadata('email'))
{
$attributesMetadata[] = $this->_customerMetadataService->getAttributeMetadata('email');
}
$data = [];
foreach ($attributesMetadata as $attributeMetadata) {
if (!$attributeMetadata->isVisible()) {
continue;
}
$attributeCode = $attributeMetadata->getAttributeCode();
if ($attributeCode == 'country_id' && isset($addressAttributes['country_id'])) {
$data['country'] = $this->_countryFactory->create()->loadByCode(
$addressAttributes['country_id']
)->getName();
} elseif ($attributeCode == 'region' && isset($addressAttributes['region'])) {
$data['region'] = __($addressAttributes['region']);
} elseif (isset($addressAttributes[$attributeCode])) {
$value = $addressAttributes[$attributeCode];
$dataModel = $this->_elementFactory->create($attributeMetadata, $value, 'customer_address');
$value = $dataModel->outputValue($dataFormat);
if ($attributeMetadata->getFrontendInput() == 'multiline') {
$values = $dataModel->outputValue(ElementFactory::OUTPUT_FORMAT_ARRAY);
// explode lines
foreach ($values as $k => $v) {
$key = sprintf('%s%d', $attributeCode, $k + 1);
$data[$key] = $v;
}
}
$data[$attributeCode] = $value;
}
}
if ($this->getType()->getEscapeHtml()) {
foreach ($data as $key => $value) {
$data[$key] = $this->escapeHtml($value);
}
}
$format = $format !== null ? $format : $this-
>getFormatArray($addressAttributes);
return $this->filterManager->template($format, ['variables' => $data]);
}
}
After the usual upgrade process you can then add email to your pdf address template in the Magento backend:
{{depend email}}Email: {{var email}}{{/depend}}