You are here

protected function ViewsUrlGenerator::processDataSet in Simple XML sitemap 4.x

Same name and namespace in other branches
  1. 8.3 modules/simple_sitemap_views/src/Plugin/simple_sitemap/UrlGenerator/ViewsUrlGenerator.php \Drupal\simple_sitemap_views\Plugin\simple_sitemap\UrlGenerator\ViewsUrlGenerator::processDataSet()

Parameters

$data_set:

Return value

mixed

Overrides UrlGeneratorBase::processDataSet

File

modules/simple_sitemap_views/src/Plugin/simple_sitemap/UrlGenerator/ViewsUrlGenerator.php, line 172

Class

ViewsUrlGenerator
Views URL generator plugin.

Namespace

Drupal\simple_sitemap_views\Plugin\simple_sitemap\UrlGenerator

Code

protected function processDataSet($data_set) : array {

  // Get information from data set.
  $view_id = $data_set['view_id'];
  $display_id = $data_set['display_id'];
  $args = $data_set['arguments'];
  try {

    // Trying to get an instance of the view.
    $view = Views::getView($view_id);
    if ($view === NULL) {
      throw new \UnexpectedValueException('Failed to get an instance of the view.');
    }

    // Trying to set the view display.
    $view
      ->initDisplay();
    if (!$view->displayHandlers
      ->has($display_id) || !$view
      ->setDisplay($display_id)) {
      throw new \UnexpectedValueException('Failed to set the view display.');
    }

    // Trying to get the sitemap settings.
    $settings = $this->sitemapViews
      ->getSitemapSettings($view, $this->sitemapVariant
      ->id());
    if (empty($settings)) {
      throw new \UnexpectedValueException('Failed to get the sitemap settings.');
    }

    // Trying to get the view URL.
    $url = $view
      ->getUrl($args);
    $url
      ->setAbsolute();
    if (is_array($args)) {
      $params = array_merge([
        $view_id,
        $display_id,
      ], $args);
      $view_result = call_user_func_array('views_get_view_result', $params);

      // Do not include paths on which the view returns an empty result.
      if (empty($view_result)) {
        throw new \UnexpectedValueException('The view returned an empty result.');
      }

      // Remove empty arguments from URL.
      $this
        ->cleanRouteParameters($url, $args);
    }
    $path = $url
      ->getInternalPath();

    // Destroy a view instance.
    $view
      ->destroy();
  } catch (\Exception $e) {

    // Delete records about arguments that are not added to the sitemap.
    if (!empty($data_set['index_id'])) {
      $condition = Database::getConnection()
        ->condition('AND');
      $condition
        ->condition('id', $data_set['index_id']);
      $this->sitemapViews
        ->removeArgumentsFromIndex($condition);
    }
    throw new SkipElementException($e
      ->getMessage());
  }
  return [
    'url' => $url,
    'lastmod' => NULL,
    'priority' => $settings['priority'] ?? NULL,
    'changefreq' => !empty($settings['changefreq']) ? $settings['changefreq'] : NULL,
    'images' => [],
    // Additional info useful in hooks.
    'meta' => [
      'path' => $path,
      'view_info' => [
        'view_id' => $view_id,
        'display_id' => $display_id,
        'arguments' => $args,
      ],
    ],
  ];
}