protected function EmailWebformHandler::buildElement in Webform 6.x
Same name and namespace in other branches
- 8.5 src/Plugin/WebformHandler/EmailWebformHandler.php \Drupal\webform\Plugin\WebformHandler\EmailWebformHandler::buildElement()
Build A select other element for email address and names.
Parameters
string $name: The element's key.
string $title: The element's title.
string $label: The element's label.
bool $required: TRUE if the element is required.
array $element_options: The element options.
array $options_options: The options options.
array $role_options: The (user) role options.
array $other_options: The other options.
Return value
array A select other element.
1 call to EmailWebformHandler::buildElement()
- EmailWebformHandler::buildConfigurationForm in src/
Plugin/ WebformHandler/ EmailWebformHandler.php - Form constructor.
File
- src/
Plugin/ WebformHandler/ EmailWebformHandler.php, line 1496
Class
- EmailWebformHandler
- Emails a webform submission.
Namespace
Drupal\webform\Plugin\WebformHandlerCode
protected function buildElement($name, $title, $label, $required = FALSE, array $element_options = [], array $options_options = NULL, array $role_options = NULL, array $other_options = NULL) {
list($element_name, $element_type) = strpos($name, '_') !== FALSE ? explode('_', $name) : [
$name,
'text',
];
$default_option = $this
->getDefaultConfigurationValue($name);
$options = [];
$options[WebformSelectOther::OTHER_OPTION] = $this
->t('Custom @label…', [
'@label' => $label,
]);
if ($default_option) {
$options[(string) $this
->t('Default')] = [
static::DEFAULT_VALUE => $default_option,
];
}
if (!empty($element_options)) {
$options[(string) $this
->t('Elements')] = $element_options;
}
if ($options_options) {
$options[(string) $this
->t('Options')] = $options_options;
}
if ($role_options) {
$options[(string) $this
->t('Roles')] = $role_options;
}
if ($other_options) {
$options[(string) $this
->t('Other')] = $other_options;
}
$element = [];
$element[$name] = [
'#type' => 'webform_select_other',
'#title' => $title,
'#options' => $options,
'#empty_option' => !$required ? $this
->t('- None -') : NULL,
'#other__title' => $title,
'#other__title_display' => 'invisible',
'#other__placeholder' => $this
->t('Enter @label…', [
'@label' => $label,
]),
'#other__type' => $element_type === 'mail' ? 'webform_email_multiple' : 'textfield',
'#other__allow_tokens' => TRUE,
'#required' => $required,
'#default_value' => $this->configuration[$name],
];
// Set empty option.
if (in_array($name, [
'reply_to',
'return_path',
'sender_mail',
'sender_name',
])) {
$element[$name]['#empty_option'] = $this
->t('- Default -');
}
// Remove maxlength.
if (in_array($name, [
'subject',
])) {
$element[$name]['#other__maxlength'] = NULL;
}
// Tweak elements.
switch ($name) {
case 'from_mail':
$element[$name]['#other__description'] = $this
->t('Multiple email addresses may be separated by commas.') . ' ' . $this
->t("If multiple email addresses are entered the '@name' will be not included in the email.", [
'@name' => $this
->t('From name'),
]);
break;
case 'reply_to':
$element[$name]['#description'] = $this
->t('The email address that a recipient will see when they replying to an email.');
break;
case 'return_path':
$element[$name]['#description'] = $this
->t('The email address to which bounce messages are delivered.');
break;
case 'sender_mail':
$element[$name]['#description'] = $this
->t('The email address submitting the message, if other than shown by the From header');
break;
}
// Use multiple email for reply_to, return_path, and sender_mail because
// it supports tokens.
if (in_array($name, [
'reply_to',
'return_path',
'sender_mail',
])) {
$element[$name]['#other__type'] = 'webform_email_multiple';
$element[$name]['#other__cardinality'] = 1;
$element[$name]['#other__description'] = '';
$t_args = [
'@title' => $title,
];
if ($default_email = $this
->getDefaultConfigurationValue($name)) {
$t_args['%email'] = $default_email;
$element[$name]['#description'] .= ' ' . $this
->t("Leave blank to use %email as the '@title' email.", $t_args);
}
else {
$element[$name]['#description'] .= ' ' . $this
->t("Leave blank to automatically use the 'From' address.", $t_args);
}
}
// If no options options are defined return the element.
if (!$options_options) {
return $element;
}
$ajax_id = 'webform-email-handler-' . $name;
$this
->buildAjaxElementTrigger($ajax_id, $element[$name]);
$this
->buildAjaxElementUpdate($ajax_id, $element);
// Get options name.
$options_name = $element_name . '_options';
if (isset($options_options[$this->configuration[$name]]) && ($token_element_name = $this
->getElementKeyFromToken($this->configuration[$name]))) {
// Get options name and element.
$options_element = $this->webform
->getElement($token_element_name);
// Set mapping options.
$mapping_options = $options_element['#options'];
array_walk($mapping_options, function (&$value, $key) {
$value = '<b>' . $value . '</b>';
});
if (preg_match('/_other$/', $options_element['#type'])) {
$mapping_options[static::OTHER_OPTION] = $this
->t("Other (Used when 'other' value is entered)");
}
if (empty($options_element['#required'])) {
$mapping_options[static::EMPTY_OPTION] = $this
->t('Empty (Used when no option is selected)');
}
$mapping_options[static::DEFAULT_OPTION] = $this
->t('Default (This email address will always be included)');
// Set placeholder emails.
$destination_placeholder_emails = [
'example@example.com',
'[site:mail]',
];
if ($role_options) {
$role_names = array_keys($role_options);
$destination_placeholder_emails[] = $role_names[0] === '[webform_role:authenticated]' && isset($role_names[1]) ? $role_names[1] : $role_names[0];
}
$element[$options_name] = [
'#type' => 'webform_mapping',
'#title' => $this
->t('@title options', [
'@title' => $title,
]),
'#description' => $this
->t('The selected element has multiple options. You may enter email addresses for each choice. When that choice is selected, an email will be sent to the corresponding addresses. If a field is left blank, no email will be sent for that option. You may use tokens.') . '<br /><br />',
'#description_display' => 'before',
'#required' => TRUE,
'#default_value' => WebformOptionsHelper::decodeConfig($this->configuration[$options_name]),
'#source' => $mapping_options,
'#source__title' => $this
->t('Option'),
'#destination__type' => 'webform_email_multiple',
'#destination__allow_tokens' => TRUE,
'#destination__title' => $this
->t('Email addresses'),
'#destination__description' => NULL,
'#destination__placeholder' => implode(', ', $destination_placeholder_emails),
];
}
else {
$element[$options_name] = [
'#type' => 'value',
'#value' => [],
];
}
$this
->buildAjaxElementWrapper($ajax_id, $element[$options_name]);
return $element;
}