You are here

public function JsonFeedSerializer::attachTo in JSON Feed 8

File

src/Plugin/views/style/JsonFeedSerializer.php, line 39

Class

JsonFeedSerializer
Default style plugin to render a JSON feed.

Namespace

Drupal\json_feed\Plugin\views\style

Code

public function attachTo(array &$build, $display_id, Url $feed_url, $title) {
  $url_options = [];
  $input = $this->view
    ->getExposedInput();
  if ($input) {
    $url_options['query'] = $input;
  }
  $url_options['absolute'] = TRUE;
  $url = $feed_url
    ->setOptions($url_options)
    ->toString();
  $build['#attached']['library'][] = 'json_feed/json-feed';

  // Add the feed icon to the view.
  $this->view->feedIcons[] = [
    '#theme' => 'json_feed_icon',
    '#url' => $url,
    '#title' => $title,
  ];

  // Attach a link to the JSON feed, which is an alternate representation.
  $build['#attached']['html_head_link'][][] = [
    'rel' => 'alternate',
    'type' => 'application/json',
    'title' => $title,
    'href' => $url,
  ];
}