You are here

public function EmailYamlFormHandler::buildConfigurationForm in YAML Form 8

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form: An associative array containing the initial structure of the plugin form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides YamlFormHandlerBase::buildConfigurationForm

File

src/Plugin/YamlFormHandler/EmailYamlFormHandler.php, line 181

Class

EmailYamlFormHandler
Emails a form submission.

Namespace

Drupal\yamlform\Plugin\YamlFormHandler

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $mail_element_options = [];
  $text_element_options = [];
  $elements = $this->yamlform
    ->getElementsInitializedAndFlattened();
  foreach ($elements as $key => $element) {
    $title = isset($element['#title']) ? new FormattableMarkup('@title (@key)', [
      '@title' => $element['#title'],
      '@key' => $key,
    ]) : $key;
    if (isset($element['#type']) && in_array($element['#type'], [
      'email',
      'hidden',
      'value',
      'select',
      'radios',
      'textfield',
      'yamlform_email_multiple',
      'yamlform_email_confirm',
    ])) {

      // Note: Token must use the :raw form mail elements.
      // For example a select menu's option value would be used to route an
      // email address.
      $mail_element_options["[yamlform_submission:values:{$key}:raw]"] = $title;
    }
    $text_element_options["[yamlform_submission:values:{$key}:value]"] = $title;
  }
  $default_optgroup = (string) $this
    ->t('Default');
  $elements_optgroup = (string) $this
    ->t('Elements');

  // Disable client-side HTML5 validation which is having issues with hidden
  // element validation.
  // @see http://stackoverflow.com/questions/22148080/an-invalid-form-control-with-name-is-not-focusable
  $form['#attributes']['novalidate'] = 'novalidate';

  // To.
  $form['to'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Send to'),
    '#open' => TRUE,
  ];
  $form['to']['to_mail'] = [
    '#type' => 'yamlform_select_other',
    '#title' => $this
      ->t('To email'),
    '#options' => [
      YamlFormSelectOther::OTHER_OPTION => $this
        ->t('Custom to email address...'),
      $default_optgroup => [
        'default' => $this
          ->getDefaultConfigurationValue('to_mail'),
      ],
      $elements_optgroup => $mail_element_options,
    ],
    '#other__placeholder' => $this
      ->t('Enter to email address...'),
    '#other__type' => 'yamlform_email_multiple',
    '#other__allow_tokens' => TRUE,
    '#required' => TRUE,
    '#parents' => [
      'settings',
      'to_mail',
    ],
    '#default_value' => $this->configuration['to_mail'],
  ];
  $form['to']['cc_mail'] = [
    '#type' => 'yamlform_select_other',
    '#title' => $this
      ->t('CC email'),
    '#options' => [
      '' => '',
      YamlFormSelectOther::OTHER_OPTION => $this
        ->t('Custom CC email address...'),
      $default_optgroup => [
        'default' => $this
          ->getDefaultConfigurationValue('cc_mail'),
      ],
      $elements_optgroup => $mail_element_options,
    ],
    '#other__placeholder' => $this
      ->t('Enter CC email address...'),
    '#other__type' => 'yamlform_email_multiple',
    '#parents' => [
      'settings',
      'cc_mail',
    ],
    '#other__allow_tokens' => TRUE,
    '#default_value' => $this->configuration['cc_mail'],
  ];
  $form['to']['bcc_mail'] = [
    '#type' => 'yamlform_select_other',
    '#title' => $this
      ->t('BCC email'),
    '#options' => [
      '' => '',
      YamlFormSelectOther::OTHER_OPTION => $this
        ->t('Custom BCC email address...'),
      $default_optgroup => [
        'default' => $this
          ->getDefaultConfigurationValue('bcc_mail'),
      ],
      $elements_optgroup => $mail_element_options,
    ],
    '#other__placeholder' => $this
      ->t('Enter BCC email address...'),
    '#other__type' => 'yamlform_email_multiple',
    '#other__allow_tokens' => TRUE,
    '#parents' => [
      'settings',
      'bcc_mail',
    ],
    '#default_value' => $this->configuration['bcc_mail'],
  ];

  // From.
  $form['from'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Send from'),
    '#open' => TRUE,
  ];
  $form['from']['from_mail'] = [
    '#type' => 'yamlform_select_other',
    '#title' => $this
      ->t('From email'),
    '#options' => [
      YamlFormSelectOther::OTHER_OPTION => $this
        ->t('Custom from email address...'),
      $default_optgroup => [
        'default' => $this
          ->getDefaultConfigurationValue('from_mail'),
      ],
      $elements_optgroup => $mail_element_options,
    ],
    '#other__placeholder' => $this
      ->t('Enter from email address...'),
    '#other__type' => 'yamlform_email_multiple',
    '#other__allow_tokens' => TRUE,
    '#required' => TRUE,
    '#parents' => [
      'settings',
      'from_mail',
    ],
    '#default_value' => $this->configuration['from_mail'],
  ];
  $form['from']['from_name'] = [
    '#type' => 'yamlform_select_other',
    '#title' => $this
      ->t('From name'),
    '#options' => [
      '' => '',
      YamlFormSelectOther::OTHER_OPTION => $this
        ->t('Custom from name...'),
      $default_optgroup => [
        'default' => $this
          ->getDefaultConfigurationValue('from_name'),
      ],
      $elements_optgroup => $text_element_options,
    ],
    '#other__placeholder' => $this
      ->t('Enter from name...'),
    '#parents' => [
      'settings',
      'from_name',
    ],
    '#default_value' => $this->configuration['from_name'],
  ];

  // Message.
  $form['message'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Message'),
    '#open' => TRUE,
  ];
  $form['message']['subject'] = [
    '#type' => 'yamlform_select_other',
    '#title' => $this
      ->t('Subject'),
    '#options' => [
      YamlFormSelectOther::OTHER_OPTION => $this
        ->t('Custom subject...'),
      $default_optgroup => [
        'default' => $this
          ->getDefaultConfigurationValue('subject'),
      ],
      $elements_optgroup => $text_element_options,
    ],
    '#other__placeholder' => $this
      ->t('Enter subject...'),
    '#required' => TRUE,
    '#parents' => [
      'settings',
      'subject',
    ],
    '#default_value' => $this->configuration['subject'],
  ];

  // Body.
  // Building a custom select other element that toggles between
  // HTML (CKEditor) and Plain text (CodeMirror) custom body elements.
  $body_options = [
    YamlFormSelectOther::OTHER_OPTION => $this
      ->t('Custom body...'),
    'default' => $this
      ->t('Default'),
    $elements_optgroup => $text_element_options,
  ];
  $body_default_format = $this->configuration['html'] ? 'html' : 'text';
  $body_default_values = $this
    ->getBodyDefaultValues();
  if (isset($body_options[$this->configuration['body']])) {
    $body_default_value = $this->configuration['body'];
    $body_custom_default_value = $body_default_values[$body_default_format];
  }
  else {
    $body_default_value = YamlFormSelectOther::OTHER_OPTION;
    $body_custom_default_value = $this->configuration['body'];
  }
  $form['message']['body'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Body'),
    '#options' => $body_options,
    '#required' => TRUE,
    '#parents' => [
      'settings',
      'body',
    ],
    '#default_value' => $body_default_value,
  ];
  foreach ($body_default_values as $format => $default_value) {

    // Custom body.
    $custom_default_value = $format === $body_default_format ? $body_custom_default_value : $default_value;
    if ($format == 'html') {
      $form['message']['body_custom_' . $format] = [
        '#type' => 'yamlform_html_editor',
      ];
    }
    else {
      $form['message']['body_custom_' . $format] = [
        '#type' => 'yamlform_codemirror',
        '#mode' => $format,
      ];
    }
    $form['message']['body_custom_' . $format] += [
      '#title' => $this
        ->t('Body custom value (@format)', [
        '@label' => $format,
      ]),
      '#title_display' => 'hidden',
      '#parents' => [
        'settings',
        'body_custom_' . $format,
      ],
      '#default_value' => $custom_default_value,
      '#states' => [
        'visible' => [
          ':input[name="settings[body]"]' => [
            'value' => YamlFormSelectOther::OTHER_OPTION,
          ],
          ':input[name="settings[html]"]' => [
            'checked' => $format == 'html' ? TRUE : FALSE,
          ],
        ],
        'required' => [
          ':input[name="settings[body]"]' => [
            'value' => YamlFormSelectOther::OTHER_OPTION,
          ],
          ':input[name="settings[html]"]' => [
            'checked' => $format == 'html' ? TRUE : FALSE,
          ],
        ],
      ],
    ];

    // Default body.
    $form['message']['body_default_' . $format] = [
      '#type' => 'yamlform_codemirror',
      '#mode' => $format,
      '#title' => $this
        ->t('Body default value (@format)', [
        '@label' => $format,
      ]),
      '#title_display' => 'hidden',
      '#default_value' => $default_value,
      '#attributes' => [
        'readonly' => 'readonly',
        'disabled' => 'disabled',
      ],
      '#states' => [
        'visible' => [
          ':input[name="settings[body]"]' => [
            'value' => 'default',
          ],
          ':input[name="settings[html]"]' => [
            'checked' => $format == 'html' ? TRUE : FALSE,
          ],
        ],
      ],
    ];
  }
  $form['message']['token_tree_link'] = $this->tokenManager
    ->buildTreeLink();

  // Elements.
  $form['elements'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Included email values'),
    '#open' => $this->configuration['excluded_elements'] ? TRUE : FALSE,
  ];
  $form['elements']['excluded_elements'] = [
    '#type' => 'yamlform_excluded_elements',
    '#description' => $this
      ->t('The selected elements will be included in the [yamlform_submission:values] token. Individual values may still be printed if explicitly specified as a [yamlform_submission:values:?] in the email body template.'),
    '#yamlform' => $this->yamlform,
    '#default_value' => $this->configuration['excluded_elements'],
    '#parents' => [
      'settings',
      'excluded_elements',
    ],
  ];

  // Settings.
  $form['settings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Settings'),
  ];
  $form['settings']['html'] = [
    '#type' => 'checkbox',
    '#title' => t('Send email as HTML'),
    '#return_value' => TRUE,
    '#access' => $this
      ->supportsHtml(),
    '#parents' => [
      'settings',
      'html',
    ],
    '#default_value' => $this->configuration['html'],
  ];
  $form['settings']['attachments'] = [
    '#type' => 'checkbox',
    '#title' => t('Include files as attachments'),
    '#return_value' => TRUE,
    '#access' => $this
      ->supportsAttachments(),
    '#parents' => [
      'settings',
      'attachments',
    ],
    '#default_value' => $this->configuration['attachments'],
  ];
  $form['settings']['debug'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable debugging'),
    '#description' => $this
      ->t('If checked, sent emails will be displayed onscreen to all users.'),
    '#return_value' => TRUE,
    '#parents' => [
      'settings',
      'debug',
    ],
    '#default_value' => $this->configuration['debug'],
  ];
  return $form;
}