You are here

public function ExcelExport::attachTo in Excel Serialization 8

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

File

src/Plugin/views/style/ExcelExport.php, line 164

Class

ExcelExport
A style plugin for Excel export views.

Namespace

Drupal\xls_serialization\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 CSV icon to the view.
  $type = $this->displayHandler
    ->getContentType();
  $this->view->feedIcons[] = [
    '#theme' => 'export_icon',
    '#url' => $url,
    '#type' => mb_strtoupper($type),
    '#theme_wrappers' => [
      'container' => [
        '#attributes' => [
          'class' => [
            Html::cleanCssIdentifier($type) . '-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,
  ];
}