Repository URL to install this package:
|
Version:
1.1.5 ▾
|
<?php
namespace Drupal\dds_sendgrid_template_list\Element;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\Select;
/**
* Provides a form element for a drop-down menu or scrolling selection box.
*
* Properties:
* - #options: An associative array, where the keys are the values for each
* option, and the values are the option labels to be shown in the drop-down
* list. If a value is an array, it will be rendered similarly, but as an
* optgroup. The key of the sub-array will be used as the label for the
* optgroup. Nesting optgroups is not allowed.
* The SendGrid templates will automatically get merged into the options so
* that you can add custom values alongside the templates.
* - #empty_option: (optional) The label to show for the first default option.
* By default, the label is automatically set to "- Select -" for a required
* field and "- None -" for an optional field.
* - #empty_value: (optional) The value for the first default option, which is
* used to determine whether the user submitted a value or not.
* - If #required is TRUE, this defaults to '' (an empty string).
* - If #required is not TRUE and this value isn't set, then no extra option
* is added to the select control, leaving the control in a slightly
* illogical state, because there's no way for the user to select nothing,
* since all user agents automatically preselect the first available
* option. But people are used to this being the behavior of select
* controls.
* @todo Address the above issue in Drupal 8.
* - If #required is not TRUE and this value is set (most commonly to an
* empty string), then an extra option (see #empty_option above)
* representing a "non-selection" is added with this as its value.
* - #multiple: (optional) Indicates whether one or more options can be
* selected. Defaults to FALSE.
* - #default_value: Must be NULL or not set in case there is no value for the
* element yet, in which case a first default option is inserted by default.
* Whether this first option is a valid option depends on whether the field
* is #required or not.
* - #required: (optional) Whether the user needs to select an option (TRUE)
* or not (FALSE). Defaults to FALSE.
* - #size: The size of the input element in characters.
*
* Usage example:
* @code
* $form['example_select'] = [
* '#type' => 'sendgrid_template_select',
* '#title' => $this->t('Select element'),
* '#options' => [
* '1' => $this->t('One'),
* '2' => [
* '2.1' => $this->t('Two point one'),
* '2.2' => $this->t('Two point two'),
* ],
* '3' => $this->t('Three'),
* ],
* ];
* @endcode
*
* @FormElement("sendgrid_template_select")
*/
class SendGridSelect extends Select {
/**
* {@inheritdoc}
*/
public static function processSelect(&$element, FormStateInterface $form_state, &$complete_form) {
$element = parent::processSelect($element, $form_state, $complete_form);
/** @var \Drupal\dds_sendgrid_template_list\Controller\SendGridTemplateService $template_service */
$template_service = \Drupal::service('dds_sendgrid.templates');
$templates = $template_service->getTemplates();
$formatted_templates = [];
if (!empty($templates)) {
foreach ($templates as $id => $template) {
$formatted_templates[ucfirst($template['generation'])][$id] = $template['name'];
}
}
// Sort the templates by name.
_asort_recursive($formatted_templates);
ksort($formatted_templates);
// Add the SendGrid Templates into the options.
$element['#options'] += $formatted_templates;
return $element;
}
}