You are here

public function WebformProtectedDownloadsHandler::buildConfigurationForm in Webform Protected Downloads 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 WebformHandlerBase::buildConfigurationForm

File

src/Plugin/WebformHandler/WebformProtectedDownloadsHandler.php, line 85

Class

WebformProtectedDownloadsHandler
Handler for protected downloads

Namespace

Drupal\webform_protected_downloads\Plugin\WebformHandler

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {

  // Expiration
  $form['expiration'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Expiration'),
    '#open' => TRUE,
  ];
  $form['expiration']['expiration_onetime'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('One time visit link'),
    '#default_value' => $this->configuration['expiration_onetime'],
  ];
  $form['expiration']['expiration_time'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Expire after X minutes'),
    '#default_value' => $this->configuration['expiration_time'],
    '#required' => TRUE,
    '#description' => 'Set this to 0 for unlimited.',
  ];
  $options = [
    '404' => $this
      ->t('404 page'),
    'homepage' => $this
      ->t('Homepage with error message'),
    'page_reload' => $this
      ->t('Form page with error message'),
    'custom' => $this
      ->t('Custom page'),
  ];
  $form['expiration']['expiration_page'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Link expired page'),
    '#required' => TRUE,
    '#description' => t('Select a page to be routed when link expires.'),
    '#options' => $options,
    '#default_value' => $this->configuration['expiration_page'],
    '#attributes' => [
      'name' => 'field_expiration_page',
    ],
  ];
  $form['expiration']['expiration_page_custom'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Custom link page'),
    '#options' => $options,
    '#default_value' => $this->configuration['expiration_page_custom'],
    '#states' => [
      'visible' => [
        ':input[name="field_expiration_page"]' => [
          'value' => 'custom',
        ],
      ],
      'required' => [
        ':input[name="field_expiration_page"]' => [
          'value' => 'custom',
        ],
      ],
    ],
  ];
  $form['expiration']['expiration_error_message'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Error message'),
    '#description' => $this
      ->t('Error message to display.'),
    '#default_value' => $this->configuration['expiration_error_message'],
    '#states' => [
      'visible' => [
        ':input[name="field_expiration_page"]' => [
          [
            'value' => 'homepage',
          ],
          [
            'value' => 'page_reload',
          ],
        ],
      ],
      'required' => [
        ':input[name="field_expiration_page"]' => [
          [
            'value' => 'homepage',
          ],
          [
            'value' => 'page_reload',
          ],
        ],
      ],
    ],
  );

  // Tokens
  $form['token'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Tokens'),
    '#open' => TRUE,
  ];
  $form['token']['token_text'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Token text title'),
    '#description' => $this
      ->t('This title will be shown when token is replaced, default title is Download'),
    '#default_value' => $this->configuration['token_text'],
  ];

  // Files
  $form['protected'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Protected Files'),
    '#open' => TRUE,
  ];
  $form['protected']['protected_files'] = [
    '#name' => 'protected_files',
    '#type' => 'managed_file',
    '#title' => $this
      ->t('Choose file(s) for protected download'),
    '#multiple' => TRUE,
    '#upload_validators' => [
      'file_validate_extensions' => $this->configuration['protected_files_allowed_extensions'],
    ],
    '#upload_location' => 'private://webform_protected_downloads/[date:custom:Y]-[date:custom:m]',
    '#default_value' => $this->configuration['protected_files'],
  ];
  $form['protected']['protected_files_allowed_extensions'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Valid File extensions'),
    '#description' => $this
      ->t("Separate extensions with a space."),
    '#default_value' => $this->configuration['protected_files_allowed_extensions'],
    '#required' => TRUE,
  ];

  // Development.
  $form['development'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Development settings'),
    '#open' => true,
  ];
  $form['development']['debug'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable debugging'),
    '#description' => $this
      ->t('If checked, debugging will be displayed onscreen to all users.'),
    '#return_value' => true,
    '#default_value' => $this->configuration['debug'],
  ];
  return $this
    ->setSettingsParents($form);
}