You are here

public function DataExport::buildOptionsForm in Views data export 8

Same name in this branch
  1. 8 src/Plugin/views/display/DataExport.php \Drupal\views_data_export\Plugin\views\display\DataExport::buildOptionsForm()
  2. 8 src/Plugin/views/style/DataExport.php \Drupal\views_data_export\Plugin\views\style\DataExport::buildOptionsForm()

Provide a form to edit options for this plugin.

Overrides RestExport::buildOptionsForm

File

src/Plugin/views/display/DataExport.php, line 340

Class

DataExport
Provides a data export display plugin.

Namespace

Drupal\views_data_export\Plugin\views\display

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  parent::buildOptionsForm($form, $form_state);

  // Remove the 'serializer' option to avoid confusion.
  switch ($form_state
    ->get('section')) {
    case 'style':
      unset($form['style']['type']['#options']['serializer']);
      break;
    case 'export_method':
      $form['export_method'] = [
        '#type' => 'radios',
        '#title' => $this
          ->t('Export method'),
        '#default_value' => $this->options['export_method'],
        '#options' => [
          'standard' => $this
            ->t('Standard'),
          'batch' => $this
            ->t('Batch'),
        ],
        '#required' => TRUE,
      ];
      $form['export_method']['standard']['#description'] = $this
        ->t('Exports under one request. Best fit for small exports.');
      $form['export_method']['batch']['#description'] = $this
        ->t('Exports data in sequences. Should be used when large amount of data is exported (> 2000 rows).');
      $form['export_batch_size'] = [
        '#type' => 'number',
        '#title' => $this
          ->t('Batch size'),
        '#description' => $this
          ->t("The number of rows to process under a request."),
        '#default_value' => $this->options['export_batch_size'],
        '#required' => TRUE,
        '#states' => [
          'visible' => [
            ':input[name=export_method]' => [
              'value' => 'batch',
            ],
          ],
        ],
      ];
      break;
    case 'export_limit':
      $form['export_limit'] = [
        '#type' => 'number',
        '#title' => $this
          ->t('Limit'),
        '#description' => $this
          ->t("The maximum amount of rows to export. 0 means unlimited."),
        '#default_value' => $this->options['export_limit'],
        '#min' => 0,
        '#required' => TRUE,
      ];
      break;
    case 'path':
      $form['file_fieldset'] = [
        '#type' => 'fieldset',
        '#title' => $this
          ->t('File Storage/Download Settings'),
      ];
      $form['filename'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Filename'),
        '#default_value' => $this
          ->getOption('filename'),
        '#description' => $this
          ->t('The filename that will be suggested to the browser for downloading purposes. You may include replacement patterns from the list below.'),
        '#fieldset' => 'file_fieldset',
      ];
      $streamWrapperManager = \Drupal::service('stream_wrapper_manager');

      // Check if the private file system is ready to use.
      if ($streamWrapperManager
        ->isValidScheme('private')) {
        $form['store_in_public_file_directory'] = [
          '#type' => 'checkbox',
          '#title' => $this
            ->t("Store file in public files directory"),
          '#description' => $this
            ->t("Check this if you want to store the export files in the public:// files directory instead of the private:// files directory."),
          '#default_value' => $this->options['store_in_public_file_directory'],
          '#fieldset' => 'file_fieldset',
        ];
      }
      else {
        $form['store_in_public_file_directory'] = [
          '#type' => 'markup',
          '#markup' => $this
            ->t('<strong>The private:// file system is not configured so the exported files will be stored in the public:// files directory. Click <a href="@link" target="_blank">here</a> for instructions on configuring the private files in the settings.php file.</strong>', [
            '@link' => 'https://www.drupal.org/docs/8/modules/skilling/installation/set-up-a-private-file-path',
          ]),
          '#fieldset' => 'file_fieldset',
        ];
      }
      $form['automatic_download'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t("Download immediately"),
        '#description' => $this
          ->t("Check this if you want to download the file immediately after it is created. Does <strong>NOT</strong> work for JSON data exports."),
        '#default_value' => $this->options['automatic_download'],
        '#fieldset' => 'file_fieldset',
      ];
      $form['redirect_fieldset'] = [
        '#type' => 'fieldset',
        '#title' => 'Redirect Settings',
      ];
      $form['custom_redirect_path'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t("Custom redirect path"),
        '#description' => $this
          ->t("Check this if you want to configure a custom redirect path."),
        '#default_value' => $this->options['custom_redirect_path'],
        '#fieldset' => 'redirect_fieldset',
      ];
      $displays = [
        'none' => 'None',
      ];
      foreach ($this->view->storage
        ->get('display') as $display_id => $display) {

        // Get displays that accept attachments and have a path.
        if ($this->view->displayHandlers
          ->has($display_id) && $this->view->displayHandlers
          ->get($display_id)
          ->acceptAttachments() && isset($display['display_options']['path'])) {
          $displays[$display_id] = $display['display_title'];
        }
      }
      $form['redirect_to_display'] = [
        '#type' => 'select',
        '#title' => $this
          ->t("Redirect to this display"),
        '#description' => $this
          ->t("Select the display to redirect to after batch finishes. If None is selected the user will be redirected to the front page."),
        '#options' => array_map('\\Drupal\\Component\\Utility\\Html::escape', $displays),
        '#default_value' => $this
          ->getOption('redirect_to_display'),
        '#fieldset' => 'redirect_fieldset',
        '#states' => [
          'invisible' => [
            ':input[name="custom_redirect_path"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];
      $form['redirect_path'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Custom redirect path'),
        '#default_value' => $this
          ->getOption('redirect_path'),
        '#description' => $this
          ->t('Enter custom path to redirect user after batch finishes.'),
        '#fieldset' => 'redirect_fieldset',
        '#states' => [
          'visible' => [
            ':input[name="custom_redirect_path"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];
      $form['include_query_params'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t("Include query string parameters on redirect"),
        '#description' => $this
          ->t("Check this if you want to include query string parameters on redirect."),
        '#default_value' => $this
          ->getOption('include_query_params'),
        '#fieldset' => 'redirect_fieldset',
      ];

      // Support tokens.
      $this
        ->globalTokenForm($form, $form_state);
      break;
    case 'displays':
      $form['#title'] .= $this
        ->t('Attach to');
      $displays = [];
      foreach ($this->view->storage
        ->get('display') as $display_id => $display) {
        if ($this->view->displayHandlers
          ->has($display_id) && $this->view->displayHandlers
          ->get($display_id)
          ->acceptAttachments()) {
          $displays[$display_id] = $display['display_title'];
        }
      }
      $form['displays'] = [
        '#title' => $this
          ->t('Displays'),
        '#type' => 'checkboxes',
        '#description' => $this
          ->t('The data export icon will be available only to the selected displays.'),
        '#options' => array_map('\\Drupal\\Component\\Utility\\Html::escape', $displays),
        '#default_value' => $this
          ->getOption('displays'),
      ];
      break;
    case 'facet_settings':

      // Determine if the view is a Search API data source view and load facet
      // sources if facets module exists.
      $view = $form_state
        ->getStorage()['view'];
      $dependencies = $view
        ->get('storage')
        ->getDependencies();
      if (isset($dependencies['module'])) {
        $view_module_dependencies = $dependencies['module'];
        if (in_array('search_api', $view_module_dependencies)) {

          // Check if the facets module is enabled.
          if (\Drupal::service('module_handler')
            ->moduleExists('facets')) {
            $facet_source_plugin_manager = \Drupal::service('plugin.manager.facets.facet_source');
            $facet_sources = $facet_source_plugin_manager
              ->getDefinitions();
            $facet_source_list = [
              'none' => 'None',
            ];
            foreach ($facet_sources as $source_id => $source) {
              $facet_source_list[$source_id] = $source['label'];
            }
            $form['#title'] .= $this
              ->t('Facet source');
            $form['facet_settings'] = [
              '#title' => $this
                ->t('Facet source'),
              '#type' => 'select',
              '#description' => $this
                ->t('Choose the facet source used to alter data export. This should be the display that this data export is attached to.'),
              '#options' => $facet_source_list,
              '#default_value' => $this->options['facet_settings'],
            ];
          }
        }
      }
      break;
  }
}