You are here

public function Email::buildConfigurationForm in Search API Saved Searches 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 PluginFormInterface::buildConfigurationForm

File

src/Plugin/search_api_saved_searches/notification/Email.php, line 175

Class

Email
Provides e-mails as a notification mechanism.

Namespace

Drupal\search_api_saved_searches\Plugin\search_api_saved_searches\notification

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form['registered_choose_mail'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Let logged-in users also enter a different mail address'),
    '#default_value' => $this->configuration['registered_choose_mail'],
  ];
  $form['activate'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Activation mail'),
    '#open' => !$this->configuration['activate']['title'],
  ];
  $form['activate']['send'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Use activation mail for anonymous users'),
    '#description' => $this
      ->t("Will require that saved searches created by anonymous users, or by normal users with an e-mail address that isn't their own, are activated by clicking a link in an e-mail."),
    '#default_value' => $this->configuration['activate']['send'],
  ];
  $states = [
    'visible' => [
      ':input[name="activate[send]"]' => [
        'checked' => TRUE,
      ],
    ],
  ];
  $args = [
    '@site_name' => '[site:name]',
  ];
  $default_title = $this->configuration['activate']['title'] ?: $this
    ->t('Activate your saved search at @site_name', $args);
  $args['@user_name'] = '[user:display-name]';
  $args['@activation_link'] = '[search-api-saved-search:activate-url]';
  $default_body = $this->configuration['activate']['body'] ?: $this
    ->t("@user_name,\n\nA saved search on @site_name with this e-mail address was created.\nTo activate this saved search, click the following link:\n\n@activation_link\n\nIf you didn't create this saved search, just ignore this mail and the saved search will be deleted.\n\n--  @site_name team", $args);
  $form['activate']['title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Subject'),
    '#description' => $this
      ->t("Enter the mail's subject.") . ' ' . $this
      ->t('See below for available replacements.'),
    '#default_value' => $default_title,
    '#required' => TRUE,
    '#states' => $states,
  ];
  $form['activate']['body'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Body'),
    '#description' => $this
      ->t("Enter the mail's body.") . ' ' . $this
      ->t('See below for available replacements.'),
    '#default_value' => $default_body,
    '#rows' => 12,
    '#required' => TRUE,
    '#states' => $states,
  ];
  $types = [
    'site',
    'user',
    'search-api-saved-search',
  ];
  $available_tokens = $this
    ->getAvailableTokensList($types);
  $form['activate']['available_tokens'] = $available_tokens;
  $form['notification'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Notification mail'),
    '#open' => !$this->configuration['notification']['title'],
  ];
  $args = [
    '@site_name' => '[site:name]',
  ];
  $default_title = $this->configuration['notification']['title'] ?: $this
    ->t('New results for your saved search at @site_name', $args);
  $args['@user_name'] = '[user:display-name]';
  $args['@search_label'] = '[search-api-saved-search:label]';
  $args['@results_links'] = '[search-api-saved-search-results:links]';
  $default_body = $this->configuration['notification']['body'] ?: $this
    ->t('@user_name,

There are new results for your saved search "@search_label":

@results_links

--  @site_name team', $args);
  $form['notification']['title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Subject'),
    '#description' => $this
      ->t("Enter the mail's subject.") . ' ' . $this
      ->t('See below for available replacements.'),
    '#default_value' => $default_title,
    '#required' => TRUE,
  ];
  $form['notification']['body'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Body'),
    '#description' => $this
      ->t("Enter the mail's body.") . ' ' . $this
      ->t('See below for available replacements.'),
    '#default_value' => $default_body,
    '#rows' => 12,
    '#required' => TRUE,
  ];
  $types[] = 'search-api-saved-search-results';
  $available_tokens = $this
    ->getAvailableTokensList($types);
  $form['notification']['available_tokens'] = $available_tokens;
  return $form;
}