You are here

public function DataExport::attachTo 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::attachTo()
  2. 8 src/Plugin/views/style/DataExport.php \Drupal\views_data_export\Plugin\views\style\DataExport::attachTo()

@todo This should implement AttachableStyleInterface once https://www.drupal.org/node/2779205 lands.

File

src/Plugin/views/style/DataExport.php, line 160

Class

DataExport
A style plugin for data export views.

Namespace

Drupal\views_data_export\Plugin\views\style

Code

public function attachTo(array &$build, $display_id, Url $url, $title) {

  // @todo This mostly hard-codes CSV handling. Figure out how to abstract.
  $url_options = [];
  $input = $this->view
    ->getExposedInput();
  if ($input) {
    $url_options['query'] = $input;
  }
  if ($pager = $this->view
    ->getPager()) {
    $url_options['query']['page'] = $pager
      ->getCurrentPage();
  }
  $url_options['absolute'] = TRUE;
  if (!empty($this->options['formats'])) {
    $url_options['query']['_format'] = reset($this->options['formats']);
  }
  $url = $url
    ->setOptions($url_options)
    ->toString();

  // Add the icon to the view.
  $format = $this->displayHandler
    ->getContentType();
  $this->view->feedIcons[] = [
    '#theme' => 'export_icon',
    '#url' => $url,
    '#format' => mb_strtoupper($format),
    '#theme_wrappers' => [
      'container' => [
        '#attributes' => [
          'class' => [
            Html::cleanCssIdentifier($format) . '-feed',
            'views-data-export-feed',
          ],
        ],
      ],
    ],
    '#attached' => [
      'library' => [
        'views_data_export/views_data_export',
      ],
    ],
  ];

  // Attach a link to the CSV feed, which is an alternate representation.
  $build['#attached']['html_head_link'][][] = [
    'rel' => 'alternate',
    'type' => $this->displayHandler
      ->getMimeType(),
    'title' => $title,
    'href' => $url,
  ];
}